terms-set-query.asciidoc 5.8 KB

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