term-query.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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": {
  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. [[term-query-notes]]
  55. ==== Notes
  56. [[avoid-term-query-text-fields]]
  57. ===== Avoid using the `term` query for `text` fields
  58. By default, {es} changes the values of `text` fields during analysis. For
  59. example, the default <<analysis-standard-analyzer, standard analyzer>> changes
  60. `text` field values as follows:
  61. * Removes most punctuation
  62. * Divides the remaining content into individual words, called
  63. <<analysis-tokenizers, tokens>>
  64. * Lowercases the tokens
  65. To better search `text` fields, the `match` query also analyzes your provided
  66. search term before performing a search. This means the `match` query can search
  67. `text` fields for analyzed tokens rather than an exact term.
  68. The `term` query does *not* analyze the search term. The `term` query only
  69. searches for the *exact* term you provide. This means the `term` query may
  70. return poor or no results when searching `text` fields.
  71. To see the difference in search results, try the following example.
  72. . Create an index with a `text` field called `full_text`.
  73. +
  74. --
  75. [source,console]
  76. ----
  77. PUT my_index
  78. {
  79. "mappings" : {
  80. "properties" : {
  81. "full_text" : { "type" : "text" }
  82. }
  83. }
  84. }
  85. ----
  86. --
  87. . Index a document with a value of `Quick Brown Foxes!` in the `full_text`
  88. field.
  89. +
  90. --
  91. [source,console]
  92. ----
  93. PUT my_index/_doc/1
  94. {
  95. "full_text": "Quick Brown Foxes!"
  96. }
  97. ----
  98. // TEST[continued]
  99. Because `full_text` is a `text` field, {es} changes `Quick Brown Foxes!` to
  100. `[quick, brown, fox]` during analysis.
  101. --
  102. . Use the `term` query to search for `Quick Brown Foxes!` in the `full_text`
  103. field. Include the `pretty` parameter so the response is more readable.
  104. +
  105. --
  106. [source,console]
  107. ----
  108. GET my_index/_search?pretty
  109. {
  110. "query": {
  111. "term": {
  112. "full_text": "Quick Brown Foxes!"
  113. }
  114. }
  115. }
  116. ----
  117. // TEST[continued]
  118. Because the `full_text` field no longer contains the *exact* term `Quick Brown
  119. Foxes!`, the `term` query search returns no results.
  120. --
  121. . Use the `match` query to search for `Quick Brown Foxes!` in the `full_text`
  122. field.
  123. +
  124. --
  125. ////
  126. [source,console]
  127. ----
  128. POST my_index/_refresh
  129. ----
  130. // TEST[continued]
  131. ////
  132. [source,console]
  133. ----
  134. GET my_index/_search?pretty
  135. {
  136. "query": {
  137. "match": {
  138. "full_text": "Quick Brown Foxes!"
  139. }
  140. }
  141. }
  142. ----
  143. // TEST[continued]
  144. Unlike the `term` query, the `match` query analyzes your provided search term,
  145. `Quick Brown Foxes!`, before performing a search. The `match` query then returns
  146. any documents containing the `quick`, `brown`, or `fox` tokens in the
  147. `full_text` field.
  148. Here's the response for the `match` query search containing the indexed document
  149. in the results.
  150. [source,console-result]
  151. ----
  152. {
  153. "took" : 1,
  154. "timed_out" : false,
  155. "_shards" : {
  156. "total" : 1,
  157. "successful" : 1,
  158. "skipped" : 0,
  159. "failed" : 0
  160. },
  161. "hits" : {
  162. "total" : {
  163. "value" : 1,
  164. "relation" : "eq"
  165. },
  166. "max_score" : 0.8630463,
  167. "hits" : [
  168. {
  169. "_index" : "my_index",
  170. "_type" : "_doc",
  171. "_id" : "1",
  172. "_score" : 0.8630463,
  173. "_source" : {
  174. "full_text" : "Quick Brown Foxes!"
  175. }
  176. }
  177. ]
  178. }
  179. }
  180. ----
  181. // TESTRESPONSE[s/"took" : 1/"took" : $body.took/]
  182. --