pub struct KnnQuery { /* private fields */ }
Expand description
Finds the k nearest vectors to a query vector, as measured by a similarity metric. knn query finds nearest vectors through approximate search on indexed dense_vectors. The preferred way to do approximate kNN search is through the top level knn section of a search request. knn query is reserved for expert cases, where there is a need to combine this query with other queries.
knn
query doesn’t have a separatek
parameter.k
is defined bysize
parameter of a search request similar to other queries.knn
query collectsnum_candidates
results from each shard, then merges them to get the topsize
results.
To create a knn query:
Query::knn("test", vec![1.0, 2.0, 3.0]);
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-knn-query.html
Implementations§
Source§impl KnnQuery
impl KnnQuery
Sourcepub fn num_candidates(self, num_candidates: u32) -> Self
pub fn num_candidates(self, num_candidates: u32) -> Self
The number of nearest neighbor candidates to consider per shard. Cannot exceed 10,000. Elasticsearch collects
num_candidates
results from each shard, then merges them to find the top results. Increasing num_candidates
tends to improve the accuracy of the final results. Defaults to Math.min(1.5 * size, 10_000)
.
Sourcepub fn filter<T>(self, filter: T) -> Self
pub fn filter<T>(self, filter: T) -> Self
Query to filter the documents that can match. The kNN search will return the top documents that also match
this filter. The value can be a single query or a list of queries. If filter
is not provided, all documents
are allowed to match.
The filter is a pre-filter, meaning that it is applied during the approximate kNN search to ensure that
num_candidates
matching documents are returned.
Sourcepub fn similarity(self, similarity: f32) -> Self
pub fn similarity(self, similarity: f32) -> Self
The minimum similarity required for a document to be considered a match. The similarity value calculated relates to the raw similarity used. Not the document score. The matched documents are then scored according to similarity and the provided boost is applied.
Sourcepub fn boost<T>(self, boost: T) -> Selfwhere
T: AsPrimitive<f32>,
pub fn boost<T>(self, boost: T) -> Selfwhere
T: AsPrimitive<f32>,
Floating point number used to decrease or increase the
relevance scores
of a query. Defaults to 1.0
.
You can use the boost parameter to adjust relevance scores for searches containing two or more queries.
Boost values are relative to the default value of 1.0
.
A boost value between 0 and 1.0
decreases the relevance score.
A value greater than 1.0
increases the relevance score.