term-query.asciidoc 4.7 KB

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