Method

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.
  • filter: Metadata filtering of the vector is used to query your data based on the filters and narrow down the query results.

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).
If you wanna learn more about filtering check: Metadata Filtering

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
filter = "genre = 'fantasy' and title = 'Lord of the Rings'"

# Execute the query
query_result = index.query(
    vector=query_vector,
    include_metadata=include_metadata,
    include_vector=include_vector,
    top_k=top_k,
    filter=filter
)

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