term-query.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. [[query-dsl-term-query]]
  2. === Term Query
  3. Returns documents that contain an *exact* term in a provided field.
  4. You can use the `term` query to find documents based on a precise value such as
  5. a price, a product ID, or a username.
  6. [WARNING]
  7. ====
  8. Avoid using the `term` query for <<text, `text`>> fields.
  9. By default, {es} changes the values of `text` fields as part of <<analysis,
  10. analysis>>. This can make finding exact matches for `text` field values
  11. difficult.
  12. To search `text` field values, use the <<query-dsl-match-query,`match`>> query
  13. instead.
  14. ====
  15. [[term-query-ex-request]]
  16. ==== Example request
  17. [source,js]
  18. ----
  19. GET /_search
  20. {
  21. "query": {
  22. "term": {
  23. "user": {
  24. "value": "Kimchy",
  25. "boost": 1.0
  26. }
  27. }
  28. }
  29. }
  30. ----
  31. // CONSOLE
  32. [[term-top-level-params]]
  33. ==== Top-level parameters for `term`
  34. `<field>`::
  35. Field you wish to search.
  36. [[term-field-params]]
  37. ==== Parameters for `<field>`
  38. `value`::
  39. Term you wish to find in the provided `<field>`. To return a document, the term
  40. must exactly match the field value, including whitespace and capitalization.
  41. `boost`::
  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. +
  46. You can use the `boost` parameter to adjust relevance scores for searches
  47. containing two or more queries.
  48. +
  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. [[term-query-notes]]
  53. ==== Notes
  54. [[avoid-term-query-text-fields]]
  55. ===== Avoid using the `term` query for `text` fields
  56. By default, {es} changes the values of `text` fields during analysis. For
  57. example, the default <<analysis-standard-analyzer, standard analyzer>> changes
  58. `text` field values as follows:
  59. * Removes most punctuation
  60. * Divides the remaining content into individual words, called
  61. <<analysis-tokenizers, tokens>>
  62. * Lowercases the tokens
  63. To better search `text` fields, the `match` query also analyzes your provided
  64. search term before performing a search. This means the `match` query can search
  65. `text` fields for analyzed tokens rather than an exact term.
  66. The `term` query does *not* analyze the search term. The `term` query only
  67. searches for the *exact* term you provide. This means the `term` query may
  68. return poor or no results when searching `text` fields.
  69. To see the difference in search results, try the following example.
  70. . Create an index with a `text` field called `full_text`.
  71. +
  72. --
  73. [source,js]
  74. ----
  75. PUT my_index
  76. {
  77. "mappings" : {
  78. "properties" : {
  79. "full_text" : { "type" : "text" }
  80. }
  81. }
  82. }
  83. ----
  84. // CONSOLE
  85. --
  86. . Index a document with a value of `Quick Brown Foxes!` in the `full_text`
  87. field.
  88. +
  89. --
  90. [source,js]
  91. ----
  92. PUT my_index/_doc/1
  93. {
  94. "full_text": "Quick Brown Foxes!"
  95. }
  96. ----
  97. // CONSOLE
  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,js]
  107. ----
  108. GET my_index/_search?pretty
  109. {
  110. "query": {
  111. "term": {
  112. "full_text": "Quick Brown Foxes!"
  113. }
  114. }
  115. }
  116. ----
  117. // CONSOLE
  118. // TEST[continued]
  119. Because the `full_text` field no longer contains the *exact* term `Quick Brown
  120. Foxes!`, the `term` query search returns no results.
  121. --
  122. . Use the `match` query to search for `Quick Brown Foxes!` in the `full_text`
  123. field.
  124. +
  125. --
  126. ////
  127. [source,js]
  128. ----
  129. POST my_index/_refresh
  130. ----
  131. // CONSOLE
  132. // TEST[continued]
  133. ////
  134. [source,js]
  135. ----
  136. GET my_index/_search?pretty
  137. {
  138. "query": {
  139. "match": {
  140. "full_text": "Quick Brown Foxes!"
  141. }
  142. }
  143. }
  144. ----
  145. // CONSOLE
  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,js]
  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",
  173. "_type" : "_doc",
  174. "_id" : "1",
  175. "_score" : 0.8630463,
  176. "_source" : {
  177. "full_text" : "Quick Brown Foxes!"
  178. }
  179. }
  180. ]
  181. }
  182. }
  183. ----
  184. // TESTRESPONSE[s/"took" : 1/"took" : $body.took/]
  185. --