term-query.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. [[query-dsl-term-query]]
  2. === Term query
  3. ++++
  4. <titleabbrev>Term</titleabbrev>
  5. ++++
  6. Returns documents that contain an *exact* term in a provided field.
  7. You can use the `term` query to find documents based on a precise value such as
  8. a price, a product ID, or a username.
  9. [WARNING]
  10. ====
  11. Avoid using the `term` query for <<text, `text`>> fields.
  12. By default, {es} changes the values of `text` fields as part of <<analysis,
  13. analysis>>. This can make finding exact matches for `text` field values
  14. difficult.
  15. To search `text` field values, use the <<query-dsl-match-query,`match`>> query
  16. instead.
  17. ====
  18. [[term-query-ex-request]]
  19. ==== Example request
  20. [source,console]
  21. ----
  22. GET /_search
  23. {
  24. "query": {
  25. "term": {
  26. "user.id": {
  27. "value": "kimchy",
  28. "boost": 1.0
  29. }
  30. }
  31. }
  32. }
  33. ----
  34. [[term-top-level-params]]
  35. ==== Top-level parameters for `term`
  36. `<field>`::
  37. (Required, object) Field you wish to search.
  38. [[term-field-params]]
  39. ==== Parameters for `<field>`
  40. `value`::
  41. (Required, string) Term you wish to find in the provided `<field>`. To return a
  42. document, the term must exactly match the field value, including whitespace and
  43. capitalization.
  44. `boost`::
  45. (Optional, float) Floating point number used to decrease or increase the
  46. <<relevance-scores,relevance scores>> of a query. Defaults to `1.0`.
  47. +
  48. You can use the `boost` parameter to adjust relevance scores for searches
  49. containing two or more queries.
  50. +
  51. Boost values are relative to the default value of `1.0`. A boost value between
  52. `0` and `1.0` decreases the relevance score. A value greater than `1.0`
  53. increases the relevance score.
  54. `case_insensitive`::
  55. (Optional, boolean) allows ASCII case insensitive matching of the
  56. value with the indexed field values when set to true. Setting to false is disallowed.
  57. [[term-query-notes]]
  58. ==== Notes
  59. [[avoid-term-query-text-fields]]
  60. ===== Avoid using the `term` query for `text` fields
  61. By default, {es} changes the values of `text` fields during analysis. For
  62. example, the default <<analysis-standard-analyzer, standard analyzer>> changes
  63. `text` field values as follows:
  64. * Removes most punctuation
  65. * Divides the remaining content into individual words, called
  66. <<analysis-tokenizers, tokens>>
  67. * Lowercases the tokens
  68. To better search `text` fields, the `match` query also analyzes your provided
  69. search term before performing a search. This means the `match` query can search
  70. `text` fields for analyzed tokens rather than an exact term.
  71. The `term` query does *not* analyze the search term. The `term` query only
  72. searches for the *exact* term you provide. This means the `term` query may
  73. return poor or no results when searching `text` fields.
  74. To see the difference in search results, try the following example.
  75. . Create an index with a `text` field called `full_text`.
  76. +
  77. --
  78. [source,console]
  79. ----
  80. PUT my-index-000001
  81. {
  82. "mappings": {
  83. "properties": {
  84. "full_text": { "type": "text" }
  85. }
  86. }
  87. }
  88. ----
  89. --
  90. . Index a document with a value of `Quick Brown Foxes!` in the `full_text`
  91. field.
  92. +
  93. --
  94. [source,console]
  95. ----
  96. PUT my-index-000001/_doc/1
  97. {
  98. "full_text": "Quick Brown Foxes!"
  99. }
  100. ----
  101. // TEST[continued]
  102. Because `full_text` is a `text` field, {es} changes `Quick Brown Foxes!` to
  103. `[quick, brown, fox]` during analysis.
  104. --
  105. . Use the `term` query to search for `Quick Brown Foxes!` in the `full_text`
  106. field. Include the `pretty` parameter so the response is more readable.
  107. +
  108. --
  109. [source,console]
  110. ----
  111. GET my-index-000001/_search?pretty
  112. {
  113. "query": {
  114. "term": {
  115. "full_text": "Quick Brown Foxes!"
  116. }
  117. }
  118. }
  119. ----
  120. // TEST[continued]
  121. Because the `full_text` field no longer contains the *exact* term `Quick Brown
  122. Foxes!`, the `term` query search returns no results.
  123. --
  124. . Use the `match` query to search for `Quick Brown Foxes!` in the `full_text`
  125. field.
  126. +
  127. --
  128. ////
  129. [source,console]
  130. ----
  131. POST my-index-000001/_refresh
  132. ----
  133. // TEST[continued]
  134. ////
  135. [source,console]
  136. ----
  137. GET my-index-000001/_search?pretty
  138. {
  139. "query": {
  140. "match": {
  141. "full_text": "Quick Brown Foxes!"
  142. }
  143. }
  144. }
  145. ----
  146. // TEST[continued]
  147. Unlike the `term` query, the `match` query analyzes your provided search term,
  148. `Quick Brown Foxes!`, before performing a search. The `match` query then returns
  149. any documents containing the `quick`, `brown`, or `fox` tokens in the
  150. `full_text` field.
  151. Here's the response for the `match` query search containing the indexed document
  152. in the results.
  153. [source,console-result]
  154. ----
  155. {
  156. "took" : 1,
  157. "timed_out" : false,
  158. "_shards" : {
  159. "total" : 1,
  160. "successful" : 1,
  161. "skipped" : 0,
  162. "failed" : 0
  163. },
  164. "hits" : {
  165. "total" : {
  166. "value" : 1,
  167. "relation" : "eq"
  168. },
  169. "max_score" : 0.8630463,
  170. "hits" : [
  171. {
  172. "_index" : "my-index-000001",
  173. "_id" : "1",
  174. "_score" : 0.8630463,
  175. "_source" : {
  176. "full_text" : "Quick Brown Foxes!"
  177. }
  178. }
  179. ]
  180. }
  181. }
  182. ----
  183. // TESTRESPONSE[s/"took" : 1/"took" : $body.took/]
  184. --