terms-query.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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` field contains `kimchy`
  12. or `elasticsearch`.
  13. [source,console]
  14. ----
  15. GET /_search
  16. {
  17. "query" : {
  18. "terms" : {
  19. "user" : ["kimchy", "elasticsearch"],
  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. Because terms lookup fetches values from a document, the <<mapping-source-field,
  66. `_source`>> mapping field must be enabled to use terms lookup. The `_source`
  67. field is enabled by default.
  68. [NOTE]
  69. By default, {es} limits the `terms` query to a maximum of 65,536
  70. terms. This includes terms fetched using terms lookup. You can change
  71. this limit using the <<index-max-terms-count, `index.max_terms_count`>> setting.
  72. To perform a terms lookup, use the following parameters.
  73. [[query-dsl-terms-lookup-params]]
  74. ====== Terms lookup parameters
  75. `index`::
  76. (Required, string) Name of the index from which to fetch field values.
  77. `id`::
  78. (Required, string) <<mapping-id-field,ID>> of the document from which to fetch
  79. field values.
  80. `path`::
  81. +
  82. --
  83. (Required, string) Name of the field from which to fetch field values. {es} uses
  84. these values as search terms for the query.
  85. If the field values include an array of nested inner objects, you can access
  86. those objects using dot notation syntax.
  87. --
  88. `routing`::
  89. (Optional, string) Custom <<mapping-routing-field, routing value>> of the
  90. document from which to fetch term values. If a custom routing value was provided
  91. when the document was indexed, this parameter is required.
  92. [[query-dsl-terms-lookup-example]]
  93. ====== Terms lookup example
  94. To see how terms lookup works, try the following example.
  95. . Create an index with a `keyword` field named `color`.
  96. +
  97. --
  98. [source,console]
  99. ----
  100. PUT my_index
  101. {
  102. "mappings" : {
  103. "properties" : {
  104. "color" : { "type" : "keyword" }
  105. }
  106. }
  107. }
  108. ----
  109. --
  110. . Index a document with an ID of 1 and values of `["blue", "green"]` in the
  111. `color` field.
  112. +
  113. --
  114. [source,console]
  115. ----
  116. PUT my_index/_doc/1
  117. {
  118. "color": ["blue", "green"]
  119. }
  120. ----
  121. // TEST[continued]
  122. --
  123. . Index another document with an ID of 2 and value of `blue` in the `color`
  124. field.
  125. +
  126. --
  127. [source,console]
  128. ----
  129. PUT my_index/_doc/2
  130. {
  131. "color": "blue"
  132. }
  133. ----
  134. // TEST[continued]
  135. --
  136. . Use the `terms` query with terms lookup parameters to find documents
  137. containing one or more of the same terms as document 2. Include the `pretty`
  138. parameter so the response is more readable.
  139. +
  140. --
  141. ////
  142. [source,console]
  143. ----
  144. POST my_index/_refresh
  145. ----
  146. // TEST[continued]
  147. ////
  148. [source,console]
  149. ----
  150. GET my_index/_search?pretty
  151. {
  152. "query": {
  153. "terms": {
  154. "color" : {
  155. "index" : "my_index",
  156. "id" : "2",
  157. "path" : "color"
  158. }
  159. }
  160. }
  161. }
  162. ----
  163. // TEST[continued]
  164. Because document 2 and document 1 both contain `blue` as a value in the `color`
  165. field, {es} returns both documents.
  166. [source,console-result]
  167. ----
  168. {
  169. "took" : 17,
  170. "timed_out" : false,
  171. "_shards" : {
  172. "total" : 1,
  173. "successful" : 1,
  174. "skipped" : 0,
  175. "failed" : 0
  176. },
  177. "hits" : {
  178. "total" : {
  179. "value" : 2,
  180. "relation" : "eq"
  181. },
  182. "max_score" : 1.0,
  183. "hits" : [
  184. {
  185. "_index" : "my_index",
  186. "_id" : "1",
  187. "_score" : 1.0,
  188. "_source" : {
  189. "color" : [
  190. "blue",
  191. "green"
  192. ]
  193. }
  194. },
  195. {
  196. "_index" : "my_index",
  197. "_id" : "2",
  198. "_score" : 1.0,
  199. "_source" : {
  200. "color" : "blue"
  201. }
  202. }
  203. ]
  204. }
  205. }
  206. ----
  207. // TESTRESPONSE[s/"took" : 17/"took" : $body.took/]
  208. --