pub struct Knn { /* private fields */ }
Expand description
Performs a k-nearest neighbor (kNN) search and returns the matching documents.
The kNN search API performs a k-nearest neighbor (kNN) search on a dense_vector
field. Given a query vector, it
finds the k closest vectors and returns those documents as search hits.
Elasticsearch uses the HNSW algorithm to support efficient kNN search. Like most kNN algorithms, HNSW is an approximate method that sacrifices result accuracy for improved search speed. This means the results returned are not always the true k closest neighbors.
The kNN search API supports restricting the search using a filter. The search will return the top k
documents
that also match the filter query.
To create a knn search with a query vector or query vector builder:
Search::new()
.knn(Knn::query_vector("test1", vec![1.0, 2.0, 3.0]))
.knn(Knn::query_vector_builder("test3", TextEmbedding::new("my-text-embedding-model", "The opposite of pink")));
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-knn-query.html
Implementations§
Source§impl Knn
impl Knn
Sourcepub fn query_vector<T>(field: T, query_vector: Vec<f32>) -> Selfwhere
T: ToString,
pub fn query_vector<T>(field: T, query_vector: Vec<f32>) -> Selfwhere
T: ToString,
Creates an instance of Knn
search with query vector
field
- The name of the vector field to search against. Must be a dense_vector field with indexing enabled.query_vector
- Query vector. Must have the same number of dimensions as the vector field you are searching against.
Sourcepub fn query_vector_builder<T, U>(field: T, query_vector_builder: U) -> Self
pub fn query_vector_builder<T, U>(field: T, query_vector_builder: U) -> Self
Creates an instance of Knn
search with query vector builder
field
- The name of the vector field to search against. Must be a dense_vector field with indexing enabled.query_vector_builder
- A configuration object indicating how to build a query_vector before executing the request.
Sourcepub fn k(self, k: u32) -> Self
pub fn k(self, k: u32) -> Self
Number of nearest neighbors to return as top hits. This value must be less than num_candidates
.
Defaults to size
.
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.