Skip to main content

Filters

Filters can be used to precisely refine search results. You can filter by properties as well as metadata, and you can combine multiple filters with and or or conditions to further narrow down the results.

Code

This example finds entries in "Movie" based on their similarity to the query "dystopian future", only from those released after 2020. It 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")

// Perform query
response = await movies.query.nearText('dystopian future', {
limit: 5,
returnMetadata: ['distance'],
filters: movies.filter.byProperty('release_date').greaterThan(new Date('December 17, 1995'))
})

// 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}`)
}
client.close()

Explain the code

This query is identical to that shown earlier for semantic search, but with the addition of a filter. The filters parameter here takes an instance of the Filter class to set the filter conditions. The current query filters the results to only include those with a release year after 2010.

Example results
Dune 2021
Distance to query: 0.199

Tenet 2020
Distance to query: 0.200

Mission: Impossible - Dead Reckoning Part One 2023
Distance to query: 0.207

Onward 2020
Distance to query: 0.214

Jurassic World Dominion 2022
Distance to query: 0.216

Questions and feedback

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