text-expansion-query.asciidoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. [[query-dsl-text-expansion-query]]
  2. == Text expansion query
  3. ++++
  4. <titleabbrev>Text expansion</titleabbrev>
  5. ++++
  6. The text expansion query uses a {nlp} model to convert the query text into a
  7. list of token-weight pairs which are then used in a query against a
  8. <<sparse-vector,sparse vector>> or <<rank-features,rank features>> field.
  9. [discrete]
  10. [[text-expansion-query-ex-request]]
  11. === Example request
  12. [source,console]
  13. ----
  14. GET _search
  15. {
  16. "query":{
  17. "text_expansion":{
  18. "<sparse_vector_field>":{
  19. "model_id":"the model to produce the token weights",
  20. "model_text":"the query string"
  21. }
  22. }
  23. }
  24. }
  25. ----
  26. // TEST[skip: TBD]
  27. [discrete]
  28. [[text-expansion-query-params]]
  29. === Top level parameters for `text_expansion`
  30. `<sparse_vector_field>`:::
  31. (Required, object)
  32. The name of the field that contains the token-weight pairs the NLP model created
  33. based on the input text.
  34. [discrete]
  35. [[text-expansion-rank-feature-field-params]]
  36. === Top level parameters for `<sparse_vector_field>`
  37. `model_id`::::
  38. (Required, string)
  39. The ID of the model to use to convert the query text into token-weight pairs. It
  40. must be the same model ID that was used to create the tokens from the input
  41. text.
  42. `model_text`::::
  43. (Required, string)
  44. The query text you want to use for search.
  45. `pruning_config` ::::
  46. (Optional, object)
  47. preview:[]
  48. Optional pruning configuration. If enabled, this will omit non-significant tokens from the query in order to improve query performance.
  49. Default: Disabled.
  50. +
  51. --
  52. Parameters for `<pruning_config>` are:
  53. `tokens_freq_ratio_threshold`::
  54. (Optional, integer)
  55. preview:[]
  56. Tokens whose frequency is more than `tokens_freq_ratio_threshold` times the average frequency of all tokens in the specified field are considered outliers and pruned.
  57. This value must between 1 and 100.
  58. Default: `5`.
  59. `tokens_weight_threshold`::
  60. (Optional, float)
  61. preview:[]
  62. Tokens whose weight is less than `tokens_weight_threshold` are considered nonsignificant and pruned.
  63. This value must be between 0 and 1.
  64. Default: `0.4`.
  65. `only_score_pruned_tokens`::
  66. (Optional, boolean)
  67. preview:[]
  68. If `true` we only input pruned tokens into scoring, and discard non-pruned tokens.
  69. It is strongly recommended to set this to `false` for the main query, but this can be set to `true` for a rescore query to get more relevant results.
  70. Default: `false`.
  71. NOTE: The default values for `tokens_freq_ratio_threshold` and `tokens_weight_threshold` were chosen based on tests using ELSER that provided the most optimal results.
  72. --
  73. [discrete]
  74. [[text-expansion-query-example]]
  75. === Example ELSER query
  76. The following is an example of the `text_expansion` query that references the
  77. ELSER model to perform semantic search. For a more detailed description of how
  78. to perform semantic search by using ELSER and the `text_expansion` query, refer
  79. to <<semantic-search-elser,this tutorial>>.
  80. [source,console]
  81. ----
  82. GET my-index/_search
  83. {
  84. "query":{
  85. "text_expansion":{
  86. "ml.tokens":{
  87. "model_id":".elser_model_2",
  88. "model_text":"How is the weather in Jamaica?"
  89. }
  90. }
  91. }
  92. }
  93. ----
  94. // TEST[skip: TBD]
  95. Multiple `text_expansion` queries can be combined with each other or other query types.
  96. This can be achieved by wrapping them in <<query-dsl-bool-query, boolean query clauses>> and using linear boosting:
  97. [source,console]
  98. ----
  99. GET my-index/_search
  100. {
  101. "query": {
  102. "bool": {
  103. "should": [
  104. {
  105. "text_expansion": {
  106. "ml.inference.title_expanded.predicted_value": {
  107. "model_id": ".elser_model_2",
  108. "model_text": "How is the weather in Jamaica?",
  109. "boost": 1
  110. }
  111. }
  112. },
  113. {
  114. "text_expansion": {
  115. "ml.inference.description_expanded.predicted_value": {
  116. "model_id": ".elser_model_2",
  117. "model_text": "How is the weather in Jamaica?",
  118. "boost": 1
  119. }
  120. }
  121. },
  122. {
  123. "multi_match": {
  124. "query": "How is the weather in Jamaica?",
  125. "fields": [
  126. "title",
  127. "description"
  128. ],
  129. "boost": 4
  130. }
  131. }
  132. ]
  133. }
  134. }
  135. }
  136. ----
  137. // TEST[skip: TBD]
  138. This can also be achieved using <<rrf, reciprocal rank fusion (RRF)>>,
  139. through an <<rrf-retriever, `rrf` retriever>> with multiple
  140. <<standard-retriever, `standard` retrievers>>.
  141. [source,console]
  142. ----
  143. GET my-index/_search
  144. {
  145. "retriever": {
  146. "rrf": {
  147. "retrievers": [
  148. {
  149. "standard": {
  150. "query": {
  151. "multi_match": {
  152. "query": "How is the weather in Jamaica?",
  153. "fields": [
  154. "title",
  155. "description"
  156. ]
  157. }
  158. }
  159. }
  160. },
  161. {
  162. "standard": {
  163. "query": {
  164. "text_expansion": {
  165. "ml.inference.title_expanded.predicted_value": {
  166. "model_id": ".elser_model_2",
  167. "model_text": "How is the weather in Jamaica?"
  168. }
  169. }
  170. }
  171. }
  172. },
  173. {
  174. "standard": {
  175. "query": {
  176. "text_expansion": {
  177. "ml.inference.description_expanded.predicted_value": {
  178. "model_id": ".elser_model_2",
  179. "model_text": "How is the weather in Jamaica?"
  180. }
  181. }
  182. }
  183. }
  184. }
  185. ],
  186. "window_size": 10,
  187. "rank_constant": 20
  188. }
  189. }
  190. }
  191. ----
  192. // TEST[skip: TBD]
  193. [discrete]
  194. [[text-expansion-query-with-pruning-config-and-rescore-example]]
  195. === Example ELSER query with pruning configuration and rescore
  196. The following is an extension to the above example that adds a preview:[] pruning configuration to the `text_expansion` query.
  197. The pruning configuration identifies non-significant tokens to prune from the query in order to improve query performance.
  198. Token pruning happens at the shard level.
  199. While this should result in the same tokens being labeled as insignificant across shards, this is not guaranteed based on the composition of each shard.
  200. Therefore, if you are running `text_expansion` with a `pruning_config` on a multi-shard index, we strongly recommend adding a <<rescore>> function with the tokens that were originally pruned from the query.
  201. This will help mitigate any shard-level inconsistency with pruned tokens and provide better relevance overall.
  202. [source,console]
  203. ----
  204. GET my-index/_search
  205. {
  206. "query":{
  207. "text_expansion":{
  208. "ml.tokens":{
  209. "model_id":".elser_model_2",
  210. "model_text":"How is the weather in Jamaica?"
  211. },
  212. "pruning_config": {
  213. "tokens_freq_ratio_threshold": 5,
  214. "tokens_weight_threshold": 0.4,
  215. "only_score_pruned_tokens": false
  216. }
  217. }
  218. },
  219. "rescore": {
  220. "window_size": 100,
  221. "query": {
  222. "rescore_query": {
  223. "text_expansion": {
  224. "ml.tokens": {
  225. "model_id": ".elser_model_2",
  226. "model_text": "How is the weather in Jamaica?"
  227. },
  228. "pruning_config": {
  229. "tokens_freq_ratio_threshold": 5,
  230. "tokens_weight_threshold": 0.4,
  231. "only_score_pruned_tokens": false
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. ----
  239. //TEST[skip: TBD]
  240. [NOTE]
  241. ====
  242. Depending on your data, the text expansion query may be faster with `track_total_hits: false`.
  243. ====