Run a knn search
- POST also GET /{index}/_knn_search
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:
- The document
_scoreis determined by the similarity between the query and document vector. - The
hits.totalobject contains the total number of nearest neighbor candidates considered, which isnum_candidates * num_shards. Thehits.total.relationwill always beeq, indicating an exact value.
Path Parameters
-
indexIndexName | IndexNamerequired -
A comma-separated list of index names to search; use
_allor to perform the operation on all indices.
Query Parameters
-
routingstring Routing A comma-separated list of specific routing values.
Request
-
_sourceSourceConfig · default: true -
Indicates which source fields are returned for matching documents. These fields are returned in the
hits._sourceproperty of the search response. -
docvalue_fieldsFieldAndFormat -
The request returns doc values for field names matching these patterns in the
hits.fieldsproperty of the response. It accepts wildcard (*) patterns. -
fieldstring Fieldrequired -
A wildcard pattern. The request returns values for field names matching this pattern.
-
formatstring -
The format in which the values are returned.
-
include_unmappedboolean -
stored_fieldsstring Fields -
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
_sourceparameter defaults tofalse. You can pass_source: trueto return both source fields and stored fields in the search response. -
fieldsstring Fields -
The request returns values for field names matching these patterns in the
hits.fieldsproperty of the response. It accepts wildcard (*) patterns. -
filteroneOf -
A query to filter the documents that can match. The kNN search will return the top
kdocuments that also match this filter. The value can be a single query or a list of queries. Iffilterisn't provided, all documents are allowed to match. -
knnQueryrequired -
The kNN query to run.
- Reference
-
fieldstring Fieldrequired -
The name of the vector field to search against
-
query_vectorQueryVectorrequired -
The query vector
-
knumberrequired -
The final number of nearest neighbors to return as top hits
-
num_candidatesnumberrequired -
The number of nearest neighbor candidates to consider per shard
Response
-
tooknumberrequired -
The milliseconds it took Elasticsearch to run the request.
-
timed_outbooleanrequired -
If true, the request timed out before completion; returned results may be partial or empty.
-
_shardsShardStatisticsrequired -
A count of shards used for the request.
-
failednumber uintrequired -
The number of shards the operation or search attempted to run on but failed.
-
successfulnumber uintrequired -
The number of shards the operation or search succeeded on.
-
totalnumber uintrequired -
The number of shards the operation or search will run on overall.
-
failuresShardFailure -
indexstring IndexName -
nodestring -
reasonErrorCauserequired -
typestringrequired -
The type of error
-
reasonstringnullable -
A human-readable explanation of the error, in English.
-
stack_tracestring -
The server stack trace. Present only if the
error_trace=trueparameter was sent with the request. -
caused_byErrorCause -
root_causeErrorCause -
suppressedErrorCause -
shardnumber -
statusstring -
primaryboolean -
skippednumber uint -
hitsHitsMetadatarequired -
The returned documents and metadata.
-
totaloneOf -
Total hit count information, present only if
track_total_hitswasn'tfalsein the search request. -
hitsHitrequired -
_indexstring IndexNamerequired -
_idstring Id -
_scorenumber | stringnullable -
_explanationExplanation -
descriptionstringrequired -
detailsExplanationDetailrequired -
descriptionstringrequired -
detailsExplanationDetail -
valuenumberrequired -
valuenumberrequired -
fieldsmap string to object -
highlightmap string to string -
inner_hitsmap string to InnerHitsResult -
<string>InnerHitsResult -
hitsHitsMetadatarequired -
matched_queriesoneOf -
_ignoredstring -
ignored_field_valuesmap string to FieldValue -
_shardstring -
_nodestring -
_routingstring -
_sourceobject -
_ranknumber -
_seq_nonumber SequenceNumber -
_primary_termnumber -
_versionnumber VersionNumber -
sortSortResults -
max_scorenumber | stringnullable -
fieldsmap string to object -
The field values for the documents. These fields must be specified in the request using the
fieldsparameter. -
max_scorenumber -
The highest returned document score. This value is null for requests that do not sort by score.