Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create method in Index class for fetching IDs of all vectors in index #190

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ query_response = index.query(
)
```

## Get All Vector IDs

The following example retrieves the ID for every vector in the Index.
```python
import pinecone

pinecone.init(api_key="YOUR_API_KEY", environment="us-west1-gcp")
index = pinecone.Index("example-index")

all_ids = index.get_all_ids_from_index(num_dimensions=1536, namespace="")
print(all_ids)
```

## Delete vectors

The following example deletes vectors by ID.
Expand Down
33 changes: 33 additions & 0 deletions pinecone/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,39 @@ def __init__(self, index_name: str, pool_threads=1):
self.user_agent = get_user_agent()
self._vector_api = VectorOperationsApi(self)

def get_ids_from_query(self,input_vector):
"Helper function for get_all_ids_from_index()"
print("searching pinecone...")
results = self.query(vector=input_vector, top_k=10000,include_values=False)
ids = set()
print(type(results))
for result in results['matches']:
ids.add(result['id'])
return ids

def get_all_ids_from_index(self, num_dimensions, namespace=""):
"""Get all ids for all vectors in the index.

Example usage:

all_ids = get_all_ids_from_index(self, num_dimensions=1536, namespace="")
print(all_ids)"""

num_vectors = self.describe_index_stats()["namespaces"][namespace]['vector_count']
all_ids = set()
while len(all_ids) < num_vectors:
print("Length of ids list is shorter than the number of total vectors...")
input_vector = np.random.rand(num_dimensions).tolist()
print("creating random vector...")
ids = get_ids_from_query(self, input_vector)
print("getting ids from a vector query...")
all_ids.update(ids)
print("updating ids set...")
print(f"Collected {len(all_ids)} ids out of {num_vectors}.")

return all_ids


@validate_and_convert_errors
def upsert(self,
vectors: Union[List[Vector], List[tuple], List[dict]],
Expand Down