semantic-query.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. [[query-dsl-semantic-query]]
  2. === Semantic query
  3. ++++
  4. <titleabbrev>Semantic</titleabbrev>
  5. ++++
  6. beta[]
  7. The `semantic` query type enables you to perform <<semantic-search,semantic search>> on data stored in a <<semantic-text,`semantic_text`>> field.
  8. [discrete]
  9. [[semantic-query-example]]
  10. ==== Example request
  11. [source,console]
  12. ------------------------------------------------------------
  13. GET my-index-000001/_search
  14. {
  15. "query": {
  16. "semantic": {
  17. "field": "inference_field",
  18. "query": "Best surfing places"
  19. }
  20. }
  21. }
  22. ------------------------------------------------------------
  23. // TEST[skip:TBD]
  24. [discrete]
  25. [[semantic-query-params]]
  26. ==== Top-level parameters for `semantic`
  27. field::
  28. (Required, string)
  29. The `semantic_text` field to perform the query on.
  30. query::
  31. (Required, string)
  32. The query text to be searched for on the field.
  33. Refer to <<semantic-search-semantic-text,this tutorial>> to learn more about semantic search using `semantic_text` and `semantic` query.
  34. [discrete]
  35. [[hybrid-search-semantic]]
  36. ==== Hybrid search with the `semantic` query
  37. The `semantic` query can be used as a part of a hybrid search where the `semantic` query is combined with lexical queries.
  38. For example, the query below finds documents with the `title` field matching "mountain lake", and combines them with results from a semantic search on the field `title_semantic`, that is a `semantic_text` field.
  39. The combined documents are then scored, and the top 3 top scored documents are returned.
  40. [source,console]
  41. ------------------------------------------------------------
  42. POST my-index/_search
  43. {
  44. "size" : 3,
  45. "query": {
  46. "bool": {
  47. "should": [
  48. {
  49. "match": {
  50. "title": {
  51. "query": "mountain lake",
  52. "boost": 1
  53. }
  54. }
  55. },
  56. {
  57. "semantic": {
  58. "field": "title_semantic",
  59. "query": "mountain lake",
  60. "boost": 2
  61. }
  62. }
  63. ]
  64. }
  65. }
  66. }
  67. ------------------------------------------------------------
  68. // TEST[skip:TBD]
  69. You can also use semantic_text as part of <<rrf,Reciprocal Rank Fusion>> to make ranking relevant results easier:
  70. [source,console]
  71. ------------------------------------------------------------
  72. GET my-index/_search
  73. {
  74. "retriever": {
  75. "rrf": {
  76. "retrievers": [
  77. {
  78. "standard": {
  79. "query": {
  80. "term": {
  81. "text": "shoes"
  82. }
  83. }
  84. }
  85. },
  86. {
  87. "semantic": {
  88. "field": "semantic_field",
  89. "query": "shoes"
  90. }
  91. }
  92. ],
  93. "rank_window_size": 50,
  94. "rank_constant": 20
  95. }
  96. }
  97. }
  98. ------------------------------------------------------------
  99. // TEST[skip:TBD]
  100. [discrete]
  101. [[advanced-search]]
  102. === Advanced search on `semantic_text` fields
  103. The `semantic` query uses default settings for searching on `semantic_text` fields for ease of use.
  104. If you want to fine-tune a search on a `semantic_text` field, you need to know the task type used by the `inference_id` configured in `semantic_text`.
  105. You can find the task type using the <<get-inference-api>>, and check the `task_type` associated with the {infer} service.
  106. Depending on the `task_type`, use either the <<query-dsl-sparse-vector-query,`sparse_vector`>> or the <<query-dsl-knn-query,`knn`>> query for greater flexibility and customization.
  107. [discrete]
  108. [[search-sparse-inference]]
  109. ==== Search with `sparse_embedding` inference
  110. When the {infer} endpoint uses a `sparse_embedding` model, you can use a <<query-dsl-sparse-vector-query,`sparse_vector` query>> on a <<semantic-text,`semantic_text`>> field in the following way:
  111. [source,console]
  112. ------------------------------------------------------------
  113. GET test-index/_search
  114. {
  115. "query": {
  116. "nested": {
  117. "path": "inference_field.inference.chunks",
  118. "query": {
  119. "sparse_vector": {
  120. "field": "inference_field.inference.chunks.embeddings",
  121. "inference_id": "my-inference-id",
  122. "query": "mountain lake"
  123. }
  124. }
  125. }
  126. }
  127. }
  128. ------------------------------------------------------------
  129. // TEST[skip:TBD]
  130. You can customize the `sparse_vector` query to include specific settings, like <<sparse-vector-query-with-pruning-config-and-rescore-example,pruning configuration>>.
  131. [discrete]
  132. [[search-text-inferece]]
  133. ==== Search with `text_embedding` inference
  134. When the {infer} endpoint uses a `text_embedding` model, you can use a <<query-dsl-knn-query,`knn` query>> on a `semantic_text` field in the following way:
  135. [source,console]
  136. ------------------------------------------------------------
  137. GET test-index/_search
  138. {
  139. "query": {
  140. "nested": {
  141. "path": "inference_field.inference.chunks",
  142. "query": {
  143. "knn": {
  144. "field": "inference_field.inference.chunks.embeddings",
  145. "query_vector_builder": {
  146. "text_embedding": {
  147. "model_id": "my_inference_id",
  148. "model_text": "mountain lake"
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. ------------------------------------------------------------
  157. // TEST[skip:TBD]
  158. You can customize the `knn` query to include specific settings, like `num_candidates` and `k`.