Skip to main content

Vector similarity search

Vector search returns the objects with most similar vectors to that of the query.

Named vectors

Added in v1.24

Any vector-based search on collections with named vectors configured must include a target vector name in the query. This allows Weaviate to find the correct vector to compare with the query vector.

from weaviate.classes.query import MetadataQuery

reviews = client.collections.get("WineReviewNV")
response = reviews.query.near_text(
query="a sweet German white wine",
limit=2,
target_vector="title_country", # Specify the target vector for named vector collections
return_metadata=MetadataQuery(distance=True)
)

for o in response.objects:
print(o.properties)
print(o.metadata.distance)
Example response

The output is like this:

{
"WineReviewNV": [
{
"country": "Austria",
"review_body": "With notions of cherry and cinnamon on the nose and just slight fizz, this is a refreshing, fruit-driven sparkling ros\u00e9 that's full of strawberry and cherry notes\u2014it might just be the very definition of easy summer wine. It ends dry, yet refreshing.",
"title": "Gebeshuber 2013 Frizzante Ros\u00e9 Pinot Noir (\u00d6sterreichischer Perlwein)"
},
{
"country": "Austria",
"review_body": "Beautifully perfumed, with acidity, white fruits and a mineral context. The wine is layered with citrus and lime, hints of fresh pineapple acidity. Screw cap.",
"title": "Stadt Krems 2009 Steinterrassen Riesling (Kremstal)"
}
]
}

Search with text

Use the Near Text operator to find objects with the nearest vector to an input text.

from weaviate.classes.query import MetadataQuery

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.near_text(
query="animals in movies",
limit=2,
return_metadata=MetadataQuery(distance=True)
)

for o in response.objects:
print(o.properties)
print(o.metadata.distance)
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "meerkats",
"question": "Group of mammals seen <a href=\"http://www.j-archive.com/media/1998-06-01_J_28.jpg\" target=\"_blank\">here</a>: [like Timon in <i>The Lion King</i>]",
"_additional": { "distance": 0.17602634 }
},
{
"answer": "dogs",
"question": "Scooby-Doo, Goofy & Pluto are cartoon versions",
"_additional": { "distance": 0.17842108 }
}
]
}
}
}

Search with image

Use the Near Image operator to find objects with the nearest vector to an image.
This example uses a base64 representation of an image.

base64_string="SOME_BASE_64_REPRESENTATION"

# Get the collection containing images
dogs = client.collections.get("Dog")

# Perform query
response = dogs.query.near_image(
near_image=base64_string,
return_properties=["breed"],
limit=1
)

print(response.objects[0])

client.close()

See Image search for more information.

Search with an existing object

If you have an object ID, use the Near Object operator to find similar objects to that object.

from weaviate.classes.query import MetadataQuery

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.near_object(
near_object="56b9449e-65db-5df4-887b-0a4773f52aa7",
limit=2,
return_metadata=MetadataQuery(distance=True)
)

for o in response.objects:
print(o.properties)
print(o.metadata.distance)
Additional information
To get the object ID, see [Retrieve the object ID](/developers/weaviate/search/basics#retrieve-the-object-id).

Search with a vector

If you have an input vector, use the Near Vector operator to find objects with similar vectors

from weaviate.classes.query import MetadataQuery

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.near_vector(
near_vector=query_vector, # your query vector goes here
limit=2,
return_metadata=MetadataQuery(distance=True)
)

for o in response.objects:
print(o.properties)
print(o.metadata.distance)