terms-query.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. [[query-dsl-terms-query]]
  2. === Terms Query
  3. Returns documents that contain one or more *exact* terms in a provided field.
  4. The `terms` query is the same as the <<query-dsl-term-query, `term` query>>,
  5. except you can search for multiple values.
  6. [[terms-query-ex-request]]
  7. ==== Example request
  8. The following search returns documents where the `user` field contains `kimchy`
  9. or `elasticsearch`.
  10. [source,js]
  11. ----
  12. GET /_search
  13. {
  14. "query" : {
  15. "terms" : {
  16. "user" : ["kimchy", "elasticsearch"],
  17. "boost" : 1.0
  18. }
  19. }
  20. }
  21. ----
  22. // CONSOLE
  23. [[terms-top-level-params]]
  24. ==== Top-level parameters for `terms`
  25. `<field>`::
  26. +
  27. --
  28. Field you wish to search.
  29. The value of this parameter is an array of terms you wish to find in the
  30. provided field. To return a document, one or more terms must exactly match a
  31. field value, including whitespace and capitalization.
  32. By default, {es} limits the `terms` query to a maximum of 65,536
  33. terms. You can change this limit using the <<index-max-terms-count,
  34. `index.max_terms_count`>> setting.
  35. [NOTE]
  36. To use the field values of an existing document as search terms, use the
  37. <<query-dsl-terms-lookup, terms lookup>> parameters.
  38. --
  39. `boost`::
  40. +
  41. --
  42. Floating point number used to decrease or increase the
  43. <<query-filter-context, relevance scores>> of a query. Default is `1.0`.
  44. Optional.
  45. You can use the `boost` parameter to adjust relevance scores for searches
  46. containing two or more queries.
  47. Boost values are relative to the default value of `1.0`. A boost value between
  48. `0` and `1.0` decreases the relevance score. A value greater than `1.0`
  49. increases the relevance score.
  50. --
  51. [[terms-query-notes]]
  52. ==== Notes
  53. [[query-dsl-terms-query-highlighting]]
  54. ===== Highlighting `terms` queries
  55. <<search-request-highlighting,Highlighting>> is best-effort only. {es} may not
  56. return highlight results for `terms` queries depending on:
  57. * Highlighter type
  58. * Number of terms in the query
  59. [[query-dsl-terms-lookup]]
  60. ===== Terms lookup
  61. Terms lookup fetches the field values of an existing document. {es} then uses
  62. those values as search terms. This can be helpful when searching for a large set
  63. of terms.
  64. Because terms lookup fetches values from a document, the <<mapping-source-field,
  65. `_source`>> mapping field must be enabled to use terms lookup. The `_source`
  66. field is enabled by default.
  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 perform a terms lookup, use the following parameters.
  72. [[query-dsl-terms-lookup-params]]
  73. ====== Terms lookup parameters
  74. `index`::
  75. Name of the index from which to fetch field values.
  76. `id`::
  77. <<mapping-id-field,ID>> of the document from which to fetch field values.
  78. `path`::
  79. +
  80. --
  81. Name of the field from which to fetch field values. {es} uses
  82. these values as search terms for the query.
  83. If the field values include an array of nested inner objects, you can access
  84. those objects using dot notation syntax.
  85. --
  86. `routing`::
  87. Custom <<mapping-routing-field, routing value>> of the document from which to
  88. fetch term values. If a custom routing value was provided when the document was
  89. indexed, this parameter is required.
  90. [[query-dsl-terms-lookup-example]]
  91. ====== Terms lookup example
  92. To see how terms lookup works, try the following example.
  93. . Create an index with a `keyword` field named `color`.
  94. +
  95. --
  96. [source,js]
  97. ----
  98. PUT my_index
  99. {
  100. "mappings" : {
  101. "properties" : {
  102. "color" : { "type" : "keyword" }
  103. }
  104. }
  105. }
  106. ----
  107. // CONSOLE
  108. --
  109. . Index a document with an ID of 1 and values of `["blue", "green"]` in the
  110. `color` field.
  111. +
  112. --
  113. [source,js]
  114. ----
  115. PUT my_index/_doc/1
  116. {
  117. "color": ["blue", "green"]
  118. }
  119. ----
  120. // CONSOLE
  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,js]
  128. ----
  129. PUT my_index/_doc/2
  130. {
  131. "color": "blue"
  132. }
  133. ----
  134. // CONSOLE
  135. // TEST[continued]
  136. --
  137. . Use the `terms` query with terms lookup parameters to find documents
  138. containing one or more of the same terms as document 2. Include the `pretty`
  139. parameter so the response is more readable.
  140. +
  141. --
  142. ////
  143. [source,js]
  144. ----
  145. POST my_index/_refresh
  146. ----
  147. // CONSOLE
  148. // TEST[continued]
  149. ////
  150. [source,js]
  151. ----
  152. GET my_index/_search?pretty
  153. {
  154. "query": {
  155. "terms": {
  156. "color" : {
  157. "index" : "my_index",
  158. "id" : "2",
  159. "path" : "color"
  160. }
  161. }
  162. }
  163. }
  164. ----
  165. // CONSOLE
  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,js]
  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",
  189. "_type" : "_doc",
  190. "_id" : "1",
  191. "_score" : 1.0,
  192. "_source" : {
  193. "color" : [
  194. "blue",
  195. "green"
  196. ]
  197. }
  198. },
  199. {
  200. "_index" : "my_index",
  201. "_type" : "_doc",
  202. "_id" : "2",
  203. "_score" : 1.0,
  204. "_source" : {
  205. "color" : "blue"
  206. }
  207. }
  208. ]
  209. }
  210. }
  211. ----
  212. // TESTRESPONSE[s/"took" : 17/"took" : $body.took/]
  213. --