Method

The range method allows you to retrieve vectors from the index within a specified range. The function accepts the following parameters:

  • cursor: A cursor to start the range query.
  • limit: The maximum number of vectors to retrieve in a single query.
  • include_vectors: A boolean flag indicating whether to include vectors in the query results.
  • include_metadata: A boolean flag indicating whether to include metadata in the query results.

As response, the object has the following fields:

  • next_cursor: A cursor indicating the position to start the next range query. If "", there are no more results.
  • vectors: A list containing information for each vector, including id, vector, and metadata.

Range Example

from upstash_vector import Index

index = Index.from_env()

# Set parameters for the range query
cursor = ""  # Start from the beginning
limit = 10 # Range from cursor until 10 vectors are retrieved
include_vectors = True
include_metadata = True

# Execute the range query
range_result = index.range(
    cursor=cursor,
    limit=limit,
    include_vectors=include_vectors,
    include_metadata=include_metadata
)

# Print the range result
print("Next Cursor:", range_result["next_cursor"])
for vector_info in range_result.vectors:
    print("ID:", vector_info.id)
    print("Vector:", vector_info.vector)
    print("Metadata:", vector_info.metadata)
    print()

Scanning Whole Index

For scanning the entire index, you can use a similar loop as shown below:

res = index.range(cursor="", limit=5)
print(res.vectors)

while res.next_cursor != "":
    res = index.range(cursor=res.next_cursor, limiti=10)
    print(res.vectors)