To retrieve vectors from the index based on specific criteria, you can use the query method, which accepts the following parameters:

  • vector: The reference vector for similarity comparison.
  • include_metadata: A boolean flag indicating whether to include metadata in the query results.
  • include_vector: A boolean flag indicating whether to include vectors in the query results.
  • top_k: The number of top matching vectors to retrieve.

As response, the object has the following fields:

  • id: The identifier associated with the matching vector.
  • metadata: Additional information or attributes linked to the matching vector.
  • score: A measure of similarity indicating how closely the vector matches the query vector. The score is normalized to the range [0, 1], where 1 indicates a perfect match.
  • vector: The vector itself (included only if include_vector is set to True).

Query Example

from upstash_vector import Index
import random

index = Index.from_env()

# Generate a random vector for similarity comparison
dimension = 128  # Adjust based on your index's dimension
query_vector = [random.random() for _ in range(dimension)]

# Set parameters for the query
include_metadata = True
include_vector = False
top_k = 5

# Execute the query
query_result = index.query(

# Print the query result
for result in query_result:
    print("Score:", result.score)
    print("Vector:", result.vector)
    print("Metadata:", result.metadata)