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.
  • prefix: A string prefix to match vector IDs. All vectors with IDs that start with this prefix will be retrieved.
  • 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.
  • include_data: A boolean flag indicating whether to include data in the fetch results.
  • namespace: The namespace to use. When not specified, the default namespace is used.

As a response, following field is returned:

  • vectors: A list containing information for each fetched vector, including id, vector, sparse_vector, metadata, and data.

Fetch Example

from upstash_vector import Index

index = Index.from_env()

# Specify the identifiers of vectors to be fetched
ids_to_fetch = ["id-1", "id-2", "id-3"]

# Fetch the specified vectors with vectors and metadata included
fetch_result = index.fetch(
    ids=ids_to_fetch,
    include_vectors=True,
    include_metadata=True,
    include_data=True,
)

# Display the fetched vectors
for vector_info in fetch_result:
    print("ID:", vector_info.id)
    print("Vector:", vector_info.vector)
    print("Metadata:", vector_info.metadata)
    print("Data:", vector_info.data)

Alternatively, you can fetch a singular vector:

index.fetch("id-4")

Also, you can specify a namespace to operate on. When no namespace is provided, the default namespace will be used.

index.fetch("id-4", namespace="ns")

Fetch with id prefix

This will fetch all vectors with IDs that start with the prefix.

For fetching larger datasets with id prefix, prefer using the paginated range command to prevent timeouts.

index.fetch(prefix="id-")