Skip to main content

Semantic search

With Weaviate, you can perform semantic searches to find similar items based on their meaning. This is done by comparing the vector embeddings of the items in the database.

Code

This example finds entries in "Movie" based on their similarity to the query "dystopian future", 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)

// Get the collection
const movies = client.collections.get("Movie")

// Perform query
response = await movies.query.nearText('dystopian future', {
limit: 5,
returnMetadata: ['distance']
})

// 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 distance of the object from the query
console.log(`Distance to query: ${item.metadata.distance}`)
}

Explain the code

The results are based on similarity of the vector embeddings between the query and the database object text. In this case, the embeddings are generated by the vectorizer module.

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 vector distance to the query.

Example results
In Time 2011
Distance to query: 0.179

Gattaca 1997
Distance to query: 0.180

I, Robot 2004
Distance to query: 0.182

Mad Max: Fury Road 2015
Distance to query: 0.190

The Maze Runner 2014
Distance to query: 0.193

Response object

The returned object is an instance of a custom class. Its objects attribute is a list of search results, each object being an instance of another custom class.

Each returned object will:

  • Include all properties and its UUID by default except those with blob data types.
  • Not include any other information (e.g. references, metadata, vectors.) by default.

Questions and feedback

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