All methods and paths for this operation:
NOTE: The kNN search API has been replaced by the knn option in the search API.
Perform a k-nearest neighbor (kNN) search on a dense_vector field and return the matching documents. Given a query vector, the API 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.
A kNN search response has the exact same structure as a search API response. However, certain sections have a meaning specific to kNN search:
_score is determined by the similarity between the query and document vector.hits.total object contains the total number of nearest neighbor candidates considered, which is num_candidates * num_shards. The hits.total.relation will always be eq, indicating an exact value.A comma-separated list of index names to search;
use _all or to perform the operation on all indices.
Indicates which source fields are returned for matching documents. These
fields are returned in the hits._source property of the search response.
The request returns doc values for field names matching these patterns
in the hits.fields property of the response.
It accepts wildcard (*) patterns.
A reference to a field with formatting instructions on how to return the value
A list of stored fields to return as part of a hit. If no fields are specified,
no stored fields are included in the response. If this field is specified, the _source
parameter defaults to false. You can pass _source: true to return both source fields
and stored fields in the search response.
The request returns values for field names matching these patterns
in the hits.fields property of the response.
It accepts wildcard (*) patterns.
A query to filter the documents that can match. The kNN search will return the top
k documents that also match this filter. The value can be a single query or a
list of queries. If filter isn't provided, all documents are allowed to match.
An Elasticsearch Query DSL (Domain Specific Language) object that defines a query.
An Elasticsearch Query DSL (Domain Specific Language) object that defines a query.
An Elasticsearch Query DSL (Domain Specific Language) object that defines a query.
The kNN query to run.
curl \
--request POST 'http://api.example.com/{index}/_knn_search' \
--header "Authorization: $API_KEY" \
--header "Content-Type: application/json" \
--data '{"_source":true,"docvalue_fields":[{"field":"string","format":"string","include_unmapped":true}],"stored_fields":"string","fields":"string","filter":{},"knn":{"field":"string","query_vector":[42.0],"k":42.0,"num_candidates":42.0}}'