terms-query.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. [[query-dsl-terms-query]]
  2. === Terms query
  3. ++++
  4. <titleabbrev>Terms</titleabbrev>
  5. ++++
  6. Returns documents that contain one or more *exact* terms in a provided field.
  7. The `terms` query is the same as the <<query-dsl-term-query, `term` query>>,
  8. except you can search for multiple values.
  9. [[terms-query-ex-request]]
  10. ==== Example request
  11. The following search returns documents where the `user.id` field contains `kimchy`
  12. or `elkbee`.
  13. [source,console]
  14. ----
  15. GET /_search
  16. {
  17. "query": {
  18. "terms": {
  19. "user.id": [ "kimchy", "elkbee" ],
  20. "boost": 1.0
  21. }
  22. }
  23. }
  24. ----
  25. [[terms-top-level-params]]
  26. ==== Top-level parameters for `terms`
  27. `<field>`::
  28. +
  29. --
  30. (Optional, object) Field you wish to search.
  31. The value of this parameter is an array of terms you wish to find in the
  32. provided field. To return a document, one or more terms must exactly match a
  33. field value, including whitespace and capitalization.
  34. By default, {es} limits the `terms` query to a maximum of 65,536
  35. terms. You can change this limit using the <<index-max-terms-count,
  36. `index.max_terms_count`>> setting.
  37. [NOTE]
  38. To use the field values of an existing document as search terms, use the
  39. <<query-dsl-terms-lookup, terms lookup>> parameters.
  40. --
  41. `boost`::
  42. +
  43. --
  44. (Optional, float) Floating point number used to decrease or increase the
  45. <<relevance-scores,relevance scores>> of a query. Defaults to `1.0`.
  46. You can use the `boost` parameter to adjust relevance scores for searches
  47. containing two or more queries.
  48. Boost values are relative to the default value of `1.0`. A boost value between
  49. `0` and `1.0` decreases the relevance score. A value greater than `1.0`
  50. increases the relevance score.
  51. --
  52. [[terms-query-notes]]
  53. ==== Notes
  54. [[query-dsl-terms-query-highlighting]]
  55. ===== Highlighting `terms` queries
  56. <<highlighting,Highlighting>> is best-effort only. {es} may not
  57. return highlight results for `terms` queries depending on:
  58. * Highlighter type
  59. * Number of terms in the query
  60. [[query-dsl-terms-lookup]]
  61. ===== Terms lookup
  62. Terms lookup fetches the field values of an existing document. {es} then uses
  63. those values as search terms. This can be helpful when searching for a large set
  64. of terms.
  65. To run a terms lookup, the field's <<mapping-source-field,`_source`>> must be
  66. enabled. You cannot use {ccs} to run a terms lookup on a remote index.
  67. [NOTE]
  68. By default, {es} limits the `terms` query to a maximum of 65,536
  69. terms. This includes terms fetched using terms lookup. You can change
  70. this limit using the <<index-max-terms-count, `index.max_terms_count`>> setting.
  71. To reduce network traffic, a terms lookup will fetch the document's values from
  72. a shard on a local data node if possible. If the your terms data is not large,
  73. consider using an index with a single primary shard that's fully replicated
  74. across all applicable data nodes to minimize network traffic.
  75. To perform a terms lookup, use the following parameters.
  76. [[query-dsl-terms-lookup-params]]
  77. ====== Terms lookup parameters
  78. `index`::
  79. (Required, string) Name of the index from which to fetch field values.
  80. `id`::
  81. (Required, string) <<mapping-id-field,ID>> of the document from which to fetch
  82. field values.
  83. `path`::
  84. +
  85. --
  86. (Required, string) Name of the field from which to fetch field values. {es} uses
  87. these values as search terms for the query.
  88. If the field values include an array of nested inner objects, you can access
  89. those objects using dot notation syntax.
  90. --
  91. `routing`::
  92. (Optional, string) Custom <<mapping-routing-field, routing value>> of the
  93. document from which to fetch term values. If a custom routing value was provided
  94. when the document was indexed, this parameter is required.
  95. [[query-dsl-terms-lookup-example]]
  96. ====== Terms lookup example
  97. To see how terms lookup works, try the following example.
  98. . Create an index with a `keyword` field named `color`.
  99. +
  100. --
  101. [source,console]
  102. ----
  103. PUT my-index-000001
  104. {
  105. "mappings": {
  106. "properties": {
  107. "color": { "type": "keyword" }
  108. }
  109. }
  110. }
  111. ----
  112. --
  113. . Index a document with an ID of 1 and values of `["blue", "green"]` in the
  114. `color` field.
  115. +
  116. --
  117. [source,console]
  118. ----
  119. PUT my-index-000001/_doc/1
  120. {
  121. "color": ["blue", "green"]
  122. }
  123. ----
  124. // TEST[continued]
  125. --
  126. . Index another document with an ID of 2 and value of `blue` in the `color`
  127. field.
  128. +
  129. --
  130. [source,console]
  131. ----
  132. PUT my-index-000001/_doc/2
  133. {
  134. "color": "blue"
  135. }
  136. ----
  137. // TEST[continued]
  138. --
  139. . Use the `terms` query with terms lookup parameters to find documents
  140. containing one or more of the same terms as document 2. Include the `pretty`
  141. parameter so the response is more readable.
  142. +
  143. --
  144. ////
  145. [source,console]
  146. ----
  147. POST my-index-000001/_refresh
  148. ----
  149. // TEST[continued]
  150. ////
  151. [source,console]
  152. ----
  153. GET my-index-000001/_search?pretty
  154. {
  155. "query": {
  156. "terms": {
  157. "color" : {
  158. "index" : "my-index-000001",
  159. "id" : "2",
  160. "path" : "color"
  161. }
  162. }
  163. }
  164. }
  165. ----
  166. // TEST[continued]
  167. Because document 2 and document 1 both contain `blue` as a value in the `color`
  168. field, {es} returns both documents.
  169. [source,console-result]
  170. ----
  171. {
  172. "took" : 17,
  173. "timed_out" : false,
  174. "_shards" : {
  175. "total" : 1,
  176. "successful" : 1,
  177. "skipped" : 0,
  178. "failed" : 0
  179. },
  180. "hits" : {
  181. "total" : {
  182. "value" : 2,
  183. "relation" : "eq"
  184. },
  185. "max_score" : 1.0,
  186. "hits" : [
  187. {
  188. "_index" : "my-index-000001",
  189. "_id" : "1",
  190. "_score" : 1.0,
  191. "_source" : {
  192. "color" : [
  193. "blue",
  194. "green"
  195. ]
  196. }
  197. },
  198. {
  199. "_index" : "my-index-000001",
  200. "_id" : "2",
  201. "_score" : 1.0,
  202. "_source" : {
  203. "color" : "blue"
  204. }
  205. }
  206. ]
  207. }
  208. }
  209. ----
  210. // TESTRESPONSE[s/"took" : 17/"took" : $body.took/]
  211. --