Get total number of objects in an index?

I was wondering if there’s a way to to get the total number of objects stored in the index through the python client library?

Right now, I get the collection, run through the entire iterator and count instances?

1 Like

hi @ujjwalm29 !! Welcome to our community! :hugs:

iterating thru all the objects is not the optimal way.

You can aggregate over all your objects and get the top count:

aggregation = questions.aggregate.over_all(total_count=True)
print(aggregation.total_count)

Check here for more options on that:

1 Like

How about when using the JS/TS Client v3? I can’t locate documentation in v3 on the same sort of aggregate functionality found in v2.

hi @thortek !!

Welcome to our community!! :hugs:

You are right! We are missing some examples there :see_no_evil:

here is how to do it using all the bells and whistles:

  await client.collections.delete("Test");
  const collection = await client.collections.create({
    name: "Test", 
  });
  await collection.data.insert({"text": "aaa"})
  await collection.data.insert({"text": "bbb"})

  const r = await collection.aggregate.overAll({
    filters: collection.filter.byProperty('text').equal("aaa"),
    returnMetrics: collection.metrics
    .aggregate('text')
    .text(['count', 'topOccurrencesOccurs', 'topOccurrencesValue'])
  })
  console.log("Total Count", r.totalCount)
  console.log("text property", r.properties.text)

this will output:

Total Count 1
text property { count: 1, topOccurrences: [ { occurs: 1, value: ‘aaa’ } ] }

Let me know if this helps!

Thanks!

Yes, that is what I needed. Thanks for providing the example code!

1 Like