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 v3 Weaviate Typescript client. Connect to the pre-configured demo instance of Weaviate with the following code, and try the examples below.

let client: WeaviateClient


client = await weaviate.connectToWeaviateCloud(process.env.WCD_URL as string, {
authCredentials: new weaviate.ApiKey(process.env.WCD_API_KEY as string),
headers: {
'X-OpenAI-Api-Key': process.env.OPENAI_APIKEY as string, // Replace with your inference API key
}
}
)

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):

type NonGenericReturn = WeaviateReturn<undefined>
let response: NonGenericReturn

const questions = client.collections.get("JeopardyQuestion")

response = questions.query.nearText("space travel", // Your query string
{
limit: 2
}
)

for (const item of response.objects) {
console.log(item.uuid)
console.log(item.properties)
}

With a vector:

type NonGenericReturn = WeaviateReturn<undefined>
let response: NonGenericReturn

const questions = client.collections.get("JeopardyQuestion")

response = questions.query.nearVector(vectorInput, // Your vectors
{
limit: 2
}
)

for (const item of response.objects) {
console.log(item.uuid)
console.log(item.properties)
}

With an existing Weaviate object:

type NonGenericReturn = WeaviateReturn<undefined>
let response: NonGenericReturn

const questions = client.collections.get("JeopardyQuestion")

response = questions.query.nearObject(objectInput, // Your object UUID
{
limit: 2
}
)

for (const item of response.objects) {
console.log(item.uuid)
console.log(item.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.

type NonGenericReturn = WeaviateReturn<undefined>
let response: NonGenericReturn

const questions = client.collections.get("JeopardyQuestion")

response = questions.query.bm25("space travel", // Your query string
{
limit: 2
}
)

for (const item of response.objects) {
console.log(item.uuid)
console.log(item.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.

type NonGenericReturn = WeaviateReturn<undefined>
let response: NonGenericReturn

const questions = client.collections.get("JeopardyQuestion")

response = questions.query.hybrid("space travel", // Your query string
{
limit: 2
}
)

for (const item of response.objects) {
console.log(item.uuid)
console.log(item.properties)
}