[[reference-knn_search]]

////////
===========================================================================================================================
||                                                                                                                       ||
||                                                                                                                       ||
||                                                                                                                       ||
||        ██████╗ ███████╗ █████╗ ██████╗ ███╗   ███╗███████╗                                                            ||
||        ██╔══██╗██╔════╝██╔══██╗██╔══██╗████╗ ████║██╔════╝                                                            ||
||        ██████╔╝█████╗  ███████║██║  ██║██╔████╔██║█████╗                                                              ||
||        ██╔══██╗██╔══╝  ██╔══██║██║  ██║██║╚██╔╝██║██╔══╝                                                              ||
||        ██║  ██║███████╗██║  ██║██████╔╝██║ ╚═╝ ██║███████╗                                                            ||
||        ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝╚═════╝ ╚═╝     ╚═╝╚══════╝                                                            ||
||                                                                                                                       ||
||                                                                                                                       ||
||    This file is autogenerated, DO NOT send pull requests that changes this file directly.                             ||
||    You should update the script that does the generation, which can be found in:                                      ||
||    https://github.com/elastic/elastic-client-generator-js                                                             ||
||                                                                                                                       ||
||    You can run the script with the following command:                                                                 ||
||       npm run elasticsearch -- --version <version>                                                                    ||
||                                                                                                                       ||
||                                                                                                                       ||
||                                                                                                                       ||
===========================================================================================================================
////////

[discrete]
=== client.knnSearch

Run a 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.

{ref}/search-search.html[{es} documentation]

[discrete]
==== Function signature

[source,ts]
----
(KnnSearchRequest, options?): Promise<KnnSearchResponse>
----

[discrete]
===== `KnnSearchRequest`

[source,ts]
----
interface KnnSearchRequest extends <<shared-type-request-base, RequestBase>> {
  index: <<shared-type-indices, Indices>>
  routing?: <<shared-type-routing, Routing>>
  _source?: SearchSourceConfig
  docvalue_fields?: (QueryDslFieldAndFormat | <<shared-type-field, Field>>)[]
  stored_fields?: <<shared-type-fields, Fields>>
  fields?: <<shared-type-fields, Fields>>
  filter?: QueryDslQueryContainer | QueryDslQueryContainer[]
  knn: KnnSearchQuery
}
----

[discrete]
===== `KnnSearchResponse`

[source,ts]
----
interface KnnSearchResponse<TDocument = unknown> {
  took: <<shared-type-long, long>>
  timed_out: boolean
  _shards: <<shared-type-shard-statistics, ShardStatistics>>
  hits: SearchHitsMetadata<TDocument>
  fields?: Record<string, any>
  max_score?: <<shared-type-double, double>>
}
----

