terms-set-query.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. [[query-dsl-terms-set-query]]
  2. === Terms Set Query
  3. Returns documents that contain a minimum number of *exact* terms in a provided
  4. field.
  5. The `terms_set` query is the same as the <<query-dsl-terms-query, `terms`
  6. query>>, except you can define the number of matching terms required to
  7. return a document. For example:
  8. * A field, `programming_languages`, contains a list of known programming
  9. languages, such as `c++`, `java`, or `php` for job candidates. You can use the
  10. `terms_set` query to return documents that match at least two of these
  11. languages.
  12. * A field, `permissions`, contains a list of possible user permissions for an
  13. application. You can use the `terms_set` query to return documents that
  14. match a subset of these permissions.
  15. [[terms-set-query-ex-request]]
  16. ==== Example request
  17. [[terms-set-query-ex-request-index-setup]]
  18. ===== Index setup
  19. In most cases, you'll need to include a <<number, numeric>> field mapping in
  20. your index to use the `terms_set` query. This numeric field contains the
  21. number of matching terms required to return a document.
  22. To see how you can set up an index for the `terms_set` query, try the
  23. following example.
  24. . Create an index, `job-candidates`, with the following field mappings:
  25. +
  26. --
  27. * `name`, a <<keyword, `keyword`>> field. This field contains the name of the
  28. job candidate.
  29. * `programming_languages`, a <<keyword, `keyword`>> field. This field contains
  30. programming languages known by the job candidate.
  31. * `required_matches`, a <<number, numeric>> `long` field. This field contains
  32. the number of matching terms required to return a document.
  33. [source,js]
  34. ----
  35. PUT /job-candidates
  36. {
  37. "mappings": {
  38. "properties": {
  39. "name": {
  40. "type": "keyword"
  41. },
  42. "programming_languages": {
  43. "type": "keyword"
  44. },
  45. "required_matches": {
  46. "type": "long"
  47. }
  48. }
  49. }
  50. }
  51. ----
  52. // CONSOLE
  53. // TESTSETUP
  54. --
  55. . Index a document with an ID of `1` and the following values:
  56. +
  57. --
  58. * `Jane Smith` in the `name` field.
  59. * `["c++", "java"]` in the `programming_languages` field.
  60. * `2` in the `required_matches` field.
  61. Include the `?refresh` parameter so the document is immediately available for
  62. search.
  63. [source,js]
  64. ----
  65. PUT /job-candidates/_doc/1?refresh
  66. {
  67. "name": "Jane Smith",
  68. "programming_languages": ["c++", "java"],
  69. "required_matches": 2
  70. }
  71. ----
  72. // CONSOLE
  73. --
  74. . Index another document with an ID of `2` and the following values:
  75. +
  76. --
  77. * `Jason Response` in the `name` field.
  78. * `["java", "php"]` in the `programming_languages` field.
  79. * `2` in the `required_matches` field.
  80. [source,js]
  81. ----
  82. PUT /job-candidates/_doc/2?refresh
  83. {
  84. "name": "Jason Response",
  85. "programming_languages": ["java", "php"],
  86. "required_matches": 2
  87. }
  88. ----
  89. // CONSOLE
  90. --
  91. You can now use the `required_matches` field value as the number of
  92. matching terms required to return a document in the `terms_set` query.
  93. [[terms-set-query-ex-request-query]]
  94. ===== Example query
  95. The following search returns documents where the `programming_languages` field
  96. contains at least two of the following terms:
  97. * `c++`
  98. * `java`
  99. * `php`
  100. The `minimum_should_match_field` is `required_matches`. This means the
  101. number of matching terms required is `2`, the value of the `required_matches`
  102. field.
  103. [source,js]
  104. ----
  105. GET /job-candidates/_search
  106. {
  107. "query": {
  108. "terms_set": {
  109. "programming_languages": {
  110. "terms": ["c++", "java", "php"],
  111. "minimum_should_match_field": "required_matches"
  112. }
  113. }
  114. }
  115. }
  116. ----
  117. // CONSOLE
  118. [[terms-set-top-level-params]]
  119. ==== Top-level parameters for `terms_set`
  120. `<field>`::
  121. Field you wish to search.
  122. [[terms-set-field-params]]
  123. ==== Parameters for `<field>`
  124. `terms`::
  125. +
  126. --
  127. Array of terms you wish to find in the provided `<field>`. To return a document,
  128. a required number of terms must exactly match the field values, including
  129. whitespace and capitalization.
  130. The required number of matching terms is defined in the
  131. `minimum_should_match_field` or `minimum_should_match_script` parameter.
  132. --
  133. `minimum_should_match_field`::
  134. <<number, Numeric>> field containing the number of matching terms
  135. required to return a document.
  136. `minimum_should_match_script`::
  137. +
  138. --
  139. Custom script containing the number of matching terms required to return a
  140. document.
  141. For parameters and valid values, see <<modules-scripting, Scripting>>.
  142. For an example query using the `minimum_should_match_script` parameter, see
  143. <<terms-set-query-script, How to use the `minimum_should_match_script`
  144. parameter>>.
  145. --
  146. [[terms-set-query-notes]]
  147. ==== Notes
  148. [[terms-set-query-script]]
  149. ===== How to use the `minimum_should_match_script` parameter
  150. You can use `minimum_should_match_script` to define the required number of
  151. matching terms using a script. This is useful if you need to set the number of
  152. required terms dynamically.
  153. [[terms-set-query-script-ex]]
  154. ====== Example query using `minimum_should_match_script`
  155. The following search returns documents where the `programming_languages` field
  156. contains at least two of the following terms:
  157. * `c++`
  158. * `java`
  159. * `php`
  160. The `source` parameter of this query indicates:
  161. * The required number of terms to match cannot exceed `params.num_terms`, the
  162. number of terms provided in the `terms` field.
  163. * The required number of terms to match is `2`, the value of the
  164. `required_matches` field.
  165. [source,js]
  166. ----
  167. GET /job-candidates/_search
  168. {
  169. "query": {
  170. "terms_set": {
  171. "programming_languages": {
  172. "terms": ["c++", "java", "php"],
  173. "minimum_should_match_script": {
  174. "source": "Math.min(params.num_terms, doc['required_matches'].value)"
  175. },
  176. "boost": 1.0
  177. }
  178. }
  179. }
  180. }
  181. ----
  182. // CONSOLE