from topk_sdk.query import select, field, fn
docs = client.collection("books").query(
select(
"title",
published_year=field("published_year"),
# Compute vector similarity between the vector embedding of the string "epic fantasy adventure"
# and the embedding stored in the `title_embedding` field.
title_similarity=fn.vector_distance("title_embedding", [0.1, 0.2, 0.3, ...]),
)
# Return top 10 results
# sort: smaller euclidean distance = closer; larger cosine similarity = closer
# if using euclidean distance, sort in ascending order(asc=True)
.topk(field("title_similarity"), 10)
)
# Example results:
[
{
"_id": "2",
"title": "Lord of the Rings",
"title_similarity": 0.8150404095649719
},
{
"_id": "1",
"title": "The Catcher in the Rye",
"title_similarity": 0.7825378179550171,
}
]