knn-search.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. [[knn-search-api]]
  2. === kNN search API
  3. ++++
  4. <titleabbrev>kNN search</titleabbrev>
  5. ++++
  6. deprecated::[8.4.0,"The kNN search API has been replaced by the <<search-api-knn, `knn` option>> in the search API."]
  7. Performs a k-nearest neighbor (kNN) search and returns the matching documents.
  8. ////
  9. [source,console]
  10. ----
  11. PUT my-index
  12. {
  13. "mappings": {
  14. "properties": {
  15. "image_vector": {
  16. "type": "dense_vector",
  17. "dims": 3,
  18. "index": true,
  19. "similarity": "l2_norm"
  20. }
  21. }
  22. }
  23. }
  24. PUT my-index/_doc/1?refresh
  25. {
  26. "image_vector" : [0.5, 10, 6]
  27. }
  28. ----
  29. ////
  30. [source,console]
  31. ----
  32. GET my-index/_knn_search
  33. {
  34. "knn": {
  35. "field": "image_vector",
  36. "query_vector": [0.3, 0.1, 1.2],
  37. "k": 10,
  38. "num_candidates": 100
  39. },
  40. "_source": ["name", "file_type"]
  41. }
  42. ----
  43. // TEST[continued]
  44. // TEST[warning:The kNN search API has been replaced by the `knn` option in the search API.]
  45. [[knn-search-api-request]]
  46. ==== {api-request-title}
  47. `GET <target>/_knn_search`
  48. `POST <target>/_knn_search`
  49. [[knn-search-api-prereqs]]
  50. ==== {api-prereq-title}
  51. * If the {es} {security-features} are enabled, you must have the `read`
  52. <<privileges-list-indices,index privilege>> for the target data stream, index,
  53. or alias.
  54. [[knn-search-api-desc]]
  55. ==== {api-description-title}
  56. The kNN search API performs a k-nearest neighbor (kNN) search on a
  57. <<dense-vector,`dense_vector`>> field. Given a query vector, it finds the _k_
  58. closest vectors and returns those documents as search hits.
  59. //tag::hnsw-algorithm[]
  60. {es} uses the https://arxiv.org/abs/1603.09320[HNSW algorithm] to support
  61. efficient kNN search. Like most kNN algorithms, HNSW is an approximate method
  62. that sacrifices result accuracy for improved search speed. This means the
  63. results returned are not always the true _k_ closest neighbors.
  64. //end::hnsw-algorithm[]
  65. The kNN search API supports restricting the search using a filter. The search
  66. will return the top `k` documents that also match the filter query.
  67. [[knn-search-api-path-params]]
  68. ==== {api-path-parms-title}
  69. `<target>`::
  70. (Optional, string) Comma-separated list of data streams, indices, and aliases
  71. to search. Supports wildcards (`*`). To search all data streams and indices,
  72. use `*` or `_all`.
  73. [role="child_attributes"]
  74. [[knn-search-api-query-params]]
  75. ==== {api-query-parms-title}
  76. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  77. [role="child_attributes"]
  78. [[knn-search-api-request-body]]
  79. ==== {api-request-body-title}
  80. `knn`::
  81. (Required, object)
  82. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=knn]
  83. include::{es-repo-dir}/search/search.asciidoc[tag=docvalue-fields-def]
  84. include::{es-repo-dir}/search/search.asciidoc[tag=fields-param-def]
  85. include::{es-repo-dir}/search/search.asciidoc[tag=source-filtering-def]
  86. include::{es-repo-dir}/search/search.asciidoc[tag=stored-fields-def]
  87. [role="child_attributes"]
  88. [[knn-search-api-response-body]]
  89. ==== {api-response-body-title}
  90. A kNN search response has the exact same structure as a
  91. <<search-api-response-body, search API response>>. However, certain sections
  92. have a meaning specific to kNN search:
  93. * The <<search-api-response-body-score,document `_score`>> is determined by
  94. the similarity between the query and document vector. See
  95. <<dense-vector-similarity, `similarity`>>.
  96. * The `hits.total` object contains the total number of nearest neighbor
  97. candidates considered, which is `num_candidates * num_shards`. The
  98. `hits.total.relation` will always be `eq`, indicating an exact value.