Skip to main content

Keyword & Hybrid search

You can also perform keyword (BM25) searches to find items based on their keyword similarity, or hybrid searches that combine BM25 and semantic/vector searches.

Code

This example finds entries in "Movie" with the highest keyword search scores for the term "history", and prints out the title and release year of the top 5 matches.

import weaviate, { WeaviateClient } from 'weaviate-client'

let client: WeaviateClient
let response

// Instantiate your client (not shown). e.g.:
// const requestHeaders = {'X-OpenAI-Api-Key': process.env.OPENAI_APIKEY as string,}
// client = weaviate.connectToWeaviateCloud(..., headers: requestHeaders) or
// client = weaviate.connectToLocal(..., headers: requestHeaders)

const movies = client.collections.get("Movie")

response = await movies.query.bm25('history', {
limit: 5,
returnMetadata: ['score']
})

// Inspect the response
for (let item of response.objects) {
// Print the title and release year
console.log(`${item.properties.title}: ${item.properties.release_date.getUTCFullYear()} `)
// Print the BM25 score of the object from the query
console.log(`BM25 score: ${item.metadata.score}`)
}

Explain the code

The results are based on a keyword search score using what's called the BM25f algorithm.

The limit parameter here sets the maximum number of results to return.

The returnMetadata parameter takes an instance of the MetadataQuery class to set metadata to return in the search results. The current query returns the score, which is the BM25 score of the result.

Example results
American History X 1998
BM25 score: 2.707

A Beautiful Mind 2001
BM25 score: 1.896

Legends of the Fall 1994
BM25 score: 1.663

Hacksaw Ridge 2016
BM25 score: 1.554

Night at the Museum 2006
BM25 score: 1.529

Code

This example finds entries in "Movie" with the highest hybrid search scores for the term "history", and prints out the title and release year of the top 5 matches.

import weaviate, { WeaviateClient } from 'weaviate-client'

let client: WeaviateClient
let response

// Instantiate your client (not shown). e.g.:
// const requestHeaders = {'X-OpenAI-Api-Key': process.env.OPENAI_APIKEY as string,}
// client = weaviate.connectToWeaviateCloud(..., headers: requestHeaders) or
// client = weaviate.connectToLocal(..., headers: requestHeaders)

const movies = client.collections.get("Movie")

response = await movies.query.hybrid('history', {
limit: 5,
returnMetadata: ['score']
})

// Inspect the response
for (let item of response.objects) {
// Print the title and release year

console.log(`${item.properties.title}: ${item.properties.release_date.getUTCFullYear()} `)
// Print the hybrid search score of the object from the query

console.log(`Hybrid score: ${item.metadata.score}`)
}

Explain the code

The results are based on a hybrid search score. A hybrid search blends results of BM25 and semantic/vector searches.

The limit parameter here sets the maximum number of results to return.

The returnMetadata parameter takes an instance of the metadataQuery class to set metadata to return in the search results. The current query returns the score, which is the hybrid score of the result.

Example results
Legends of the Fall 1994
Hybrid score: 0.016

Hacksaw Ridge 2016
Hybrid score: 0.016

A Beautiful Mind 2001
Hybrid score: 0.015

The Butterfly Effect 2004
Hybrid score: 0.015

Night at the Museum 2006
Hybrid score: 0.012

Questions and feedback

If you have any questions or feedback, let us know in the user forum.