semantic-search.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. [[semantic-search-api]]
  2. === Semantic search API
  3. ++++
  4. <titleabbrev>Semantic search</titleabbrev>
  5. ++++
  6. experimental::[]
  7. Semantic search uses a text embedding NLP model to generate a dense vector from the input query string.
  8. The resulting dense vector is then used in a <<knn-search,k-nearest neighbor (knn) search>> against an index containing dense vectors
  9. created with the same text embedding model. The search results are semantically similar as learned
  10. by the model.
  11. ////
  12. [source,console]
  13. ----
  14. PUT my-index
  15. {
  16. "mappings": {
  17. "properties": {
  18. "text_embedding": {
  19. "type": "dense_vector",
  20. "dims": 512,
  21. "index": true,
  22. "similarity": "cosine"
  23. }
  24. }
  25. }
  26. }
  27. ----
  28. ////
  29. [source,console]
  30. ----
  31. GET my-index/_semantic_search
  32. {
  33. "query_string": "A picture of a snow capped mountain",
  34. "model_id": "my-text-embedding-model",
  35. "knn": {
  36. "field": "text_embedding",
  37. "k": 10,
  38. "num_candidates": 100
  39. }
  40. }
  41. ----
  42. // TEST[skip:TBD]
  43. [[semantic-search-api-request]]
  44. ==== {api-request-title}
  45. `GET <target>/_semantic_search`
  46. `POST <target>/_semantic_search`
  47. [[semantic-search-api-prereqs]]
  48. ==== {api-prereq-title}
  49. * If the {es} {security-features} are enabled, you must have the `read`
  50. <<privileges-list-indices,index privilege>> for the target data stream, index,
  51. or alias.
  52. [[semantic-search-api-desc]]
  53. ==== {api-description-title}
  54. The semantic search API uses a text embedding model to create a dense vector
  55. representation of the query string.
  56. [[semantic-search-api-path-params]]
  57. ==== {api-path-parms-title}
  58. `<target>`::
  59. (Optional, string) Comma-separated list of data streams, indices, and aliases
  60. to search. Supports wildcards (`*`). To search all data streams and indices,
  61. use `*` or `_all`.
  62. [role="child_attributes"]
  63. [[semantic-search-api-query-params]]
  64. ==== {api-query-parms-title}
  65. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  66. [role="child_attributes"]
  67. [[semantic-search-api-request-body]]
  68. ==== {api-request-body-title}
  69. `model_id`::
  70. (Required, string)
  71. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=model-id]
  72. `query_string`::
  73. (Required, string) The input text to embed.
  74. `knn`::
  75. (Required, object) Defines the kNN query to run.
  76. +
  77. .Properties of `knn` object
  78. [%collapsible%open]
  79. ====
  80. `field`::
  81. (Required, string) The name of the vector field to search against. Must be a
  82. <<index-vectors-knn-search, `dense_vector` field with indexing enabled>>.
  83. `k`::
  84. (Required, integer) Number of nearest neighbors to return as top hits. This
  85. value must be less than `num_candidates`.
  86. `num_candidates`::
  87. (Required, integer) The number of nearest neighbor candidates to consider per
  88. shard. Cannot exceed 10,000. {es} collects `num_candidates` results from each
  89. shard, then merges them to find the top `k` results. Increasing
  90. `num_candidates` tends to improve the accuracy of the final `k` results.
  91. ====
  92. `filter`::
  93. (Optional, <<query-dsl,Query DSL object>>) Query to filter the documents that
  94. can match. The kNN search will return the top `k` documents that also match
  95. this filter. The value can be a single query or a list of queries. If `filter`
  96. is not provided, all documents are allowed to match.
  97. include::{es-repo-dir}/search/search.asciidoc[tag=docvalue-fields-def]
  98. include::{es-repo-dir}/search/search.asciidoc[tag=fields-param-def]
  99. include::{es-repo-dir}/search/search.asciidoc[tag=source-filtering-def]
  100. include::{es-repo-dir}/search/search.asciidoc[tag=stored-fields-def]
  101. [role="child_attributes"]
  102. [[semantic-search-api-response-body]]
  103. ==== {api-response-body-title}
  104. A sementic search response has the same structure as a kNN search response.