Skip to main content

Review of search types

Overview

Weaviate offers three primary search types - namely vector, keyword, and hybrid searches. Let's briefly recap what they are, and how they work.

Code examples

These code examples are runnable, with the v4 Weaviate Python client. Connect to the pre-configured demo instance of Weaviate with the following code, and try the examples below.

import weaviate
import weaviate.classes as wvc

client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url,
auth_credentials=wvc.init.Auth.api_key(weaviate_key),
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") # Replace with your OpenAI API key (for vector and hybrid searches)
}
)

A vector search finds objects with the most similar vectors to the query vector.

Because each vector is a numerical representation of the underlying object, a vector similarity can be thought of as a similarity in meaning. Therefore a vector search is also called "semantic search".

In Weaviate, you can search for objects with similar vectors in any of the following ways:

With a source medium (e.g. text or image):

questions = client.collections.get("JeopardyQuestion")
response = questions.query.near_text(
query="space travel", # Your query string
limit=2
)

for o in response.objects:
print(o.uuid)
print(o.properties)

With a vector:

questions = client.collections.get("JeopardyQuestion")
response = questions.query.near_vector(
near_vector=vector_input, # Your vector object
limit=2
)

for o in response.objects:
print(o.uuid)
print(o.properties)

With an existing Weaviate object:

questions = client.collections.get("JeopardyQuestion")
response = questions.query.near_object(
near_object=object_input, # Your object UUID
limit=2
)

for o in response.objects:
print(o.uuid)
print(o.properties)

A keyword search finds objects whose keywords (i.e. tokens) are the most relevant to the keywords (i.e. tokens) of the query. The relevance is determined by the BM25F algorithm.

Intuitively, the BM25F algorithm determines "relevance" by considering how often a keyword appears in each field of the object, relative to how commonly the keyword appears in the entire dataset.

questions = client.collections.get("JeopardyQuestion")
response = questions.query.bm25(
query="space travel", # Your query string
limit=2
)

for o in response.objects:
print(o.uuid)
print(o.properties)

A hybrid search combines the results of a vector search and a keyword search. This is done by performing both searches, and them combining the two search results with a "fusion" algorithm.

questions = client.collections.get("JeopardyQuestion")
response = questions.query.hybrid(
query="space travel", # Your query string
limit=2
)

for o in response.objects:
print(o.uuid)
print(o.properties)