terms-set-query.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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,js]
  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. // CONSOLE
  56. // TESTSETUP
  57. --
  58. . Index a document with an ID of `1` and the following values:
  59. +
  60. --
  61. * `Jane Smith` in the `name` field.
  62. * `["c++", "java"]` in the `programming_languages` field.
  63. * `2` in the `required_matches` field.
  64. Include the `?refresh` parameter so the document is immediately available for
  65. search.
  66. [source,js]
  67. ----
  68. PUT /job-candidates/_doc/1?refresh
  69. {
  70. "name": "Jane Smith",
  71. "programming_languages": ["c++", "java"],
  72. "required_matches": 2
  73. }
  74. ----
  75. // CONSOLE
  76. --
  77. . Index another document with an ID of `2` and the following values:
  78. +
  79. --
  80. * `Jason Response` in the `name` field.
  81. * `["java", "php"]` in the `programming_languages` field.
  82. * `2` in the `required_matches` field.
  83. [source,js]
  84. ----
  85. PUT /job-candidates/_doc/2?refresh
  86. {
  87. "name": "Jason Response",
  88. "programming_languages": ["java", "php"],
  89. "required_matches": 2
  90. }
  91. ----
  92. // CONSOLE
  93. --
  94. You can now use the `required_matches` field value as the number of
  95. matching terms required to return a document in the `terms_set` query.
  96. [[terms-set-query-ex-request-query]]
  97. ===== Example query
  98. The following search returns documents where the `programming_languages` field
  99. contains at least two of the following terms:
  100. * `c++`
  101. * `java`
  102. * `php`
  103. The `minimum_should_match_field` is `required_matches`. This means the
  104. number of matching terms required is `2`, the value of the `required_matches`
  105. field.
  106. [source,js]
  107. ----
  108. GET /job-candidates/_search
  109. {
  110. "query": {
  111. "terms_set": {
  112. "programming_languages": {
  113. "terms": ["c++", "java", "php"],
  114. "minimum_should_match_field": "required_matches"
  115. }
  116. }
  117. }
  118. }
  119. ----
  120. // CONSOLE
  121. [[terms-set-top-level-params]]
  122. ==== Top-level parameters for `terms_set`
  123. `<field>`::
  124. (Required, object) Field you wish to search.
  125. [[terms-set-field-params]]
  126. ==== Parameters for `<field>`
  127. `terms`::
  128. +
  129. --
  130. (Required, array of strings) Array of terms you wish to find in the provided
  131. `<field>`. To return a document, a required number of terms must exactly match
  132. the field values, including whitespace and capitalization.
  133. The required number of matching terms is defined in the
  134. `minimum_should_match_field` or `minimum_should_match_script` parameter.
  135. --
  136. `minimum_should_match_field`::
  137. (Optional, string) <<number, Numeric>> field containing the number of matching
  138. terms required to return a document.
  139. `minimum_should_match_script`::
  140. +
  141. --
  142. (Optional, string) Custom script containing the number of matching terms
  143. required to return a document.
  144. For parameters and valid values, see <<modules-scripting, Scripting>>.
  145. For an example query using the `minimum_should_match_script` parameter, see
  146. <<terms-set-query-script, How to use the `minimum_should_match_script`
  147. parameter>>.
  148. --
  149. [[terms-set-query-notes]]
  150. ==== Notes
  151. [[terms-set-query-script]]
  152. ===== How to use the `minimum_should_match_script` parameter
  153. You can use `minimum_should_match_script` to define the required number of
  154. matching terms using a script. This is useful if you need to set the number of
  155. required terms dynamically.
  156. [[terms-set-query-script-ex]]
  157. ====== Example query using `minimum_should_match_script`
  158. The following search returns documents where the `programming_languages` field
  159. contains at least two of the following terms:
  160. * `c++`
  161. * `java`
  162. * `php`
  163. The `source` parameter of this query indicates:
  164. * The required number of terms to match cannot exceed `params.num_terms`, the
  165. number of terms provided in the `terms` field.
  166. * The required number of terms to match is `2`, the value of the
  167. `required_matches` field.
  168. [source,js]
  169. ----
  170. GET /job-candidates/_search
  171. {
  172. "query": {
  173. "terms_set": {
  174. "programming_languages": {
  175. "terms": ["c++", "java", "php"],
  176. "minimum_should_match_script": {
  177. "source": "Math.min(params.num_terms, doc['required_matches'].value)"
  178. },
  179. "boost": 1.0
  180. }
  181. }
  182. }
  183. }
  184. ----
  185. // CONSOLE