terms-query.asciidoc 5.4 KB

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