ID prefixes count

I tried to follow the instructions below to create index hierarchy by ID prefix

How can I get the count of each layer of the Hierarchy? Thanks

For example, if I have below IDs

doc1

doc2

doc1#chunk1

doc1#chunk2

the first layer contains doc1 and doc2, and the count is 2

the second layer contains doc1#chunk1 and doc1#chunk2, and the count is 2

Hi @yzhou and welcome to the Pinecone forums!

Thanks for your question.

If I’m understanding your question correctly, you may want to use manual pagination, which allows you to provide the prefix of records to list:

from pinecone import Pinecone

pc = Pinecone(api_key='YOUR_API_KEY')
index = pc.Index("pinecone-index")

namespace = 'ns1'

# For manual control over pagination
results = index.list_paginated(
    prefix='pref',
    limit=3,
    namespace='ns1'
)
print(results.namespace)
print([v.id for v in results.vectors])
print(results.pagination.next)
print(results.usage)

You would want to maintain a local variable in your code to track each prefix’s count.

So, one variable to track the count for prefix1 and one for prefix2.

Then, paginate through the list of vectors retrieved with each prefix until there are no more, and that’s your count for that prefix.

You can also see examples in our Pinecone Python client docs.

Hope this helps!

Best,
Zack

2 Likes