Example Calls
Fetch
Method
The fetch
method allows you to retrieve vectors from the index based on their identifiers. It takes the following input parameters:
ids
: A string or a list of strings representing the identifiers of the vectors to be fetched.include_vectors
: A boolean flag indicating whether to include vectors in the fetch results.include_metadata
: A boolean flag indicating whether to include metadata in the fetch results.
As a response, following field is returned:
vectors
: A list containing information for each fetched vector, includingid
,vector
, andmetadata
.
Fetch Example
from upstash_vector import Index
index = Index.from_env()
# Specify the identifiers of vectors to be fetched
ids_to_fetch = ["id1", "id2", "id3"]
# Fetch the specified vectors with vectors and metadata included
fetch_result = index.fetch(ids=ids_to_fetch, include_vectors=True, include_metadata=True)
# Display the fetched vectors
for vector_info in fetch_result.vectors:
print("ID:", vector_info.id)
print("Vector:", vector_info.vector)
print("Metadata:", vector_info.metadata)
print()
Alternatively, you can fetch a singular vector:
index.fetch("id-4")