term-query.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. [[query-dsl-term-query]]
  2. === Term Query
  3. The `term` query finds documents that contain the *exact* term specified
  4. in the inverted index. For instance:
  5. [source,js]
  6. --------------------------------------------------
  7. POST _search
  8. {
  9. "query": {
  10. "term" : { "user" : "Kimchy" } <1>
  11. }
  12. }
  13. --------------------------------------------------
  14. // AUTOSENSE
  15. <1> Finds documents which contain the exact term `Kimchy` in the inverted index
  16. of the `user` field.
  17. A `boost` parameter can be specified to give this `term` query a higher
  18. relevance score than another query, for instance:
  19. [source,js]
  20. --------------------------------------------------
  21. GET _search
  22. {
  23. "query": {
  24. "bool": {
  25. "should": [
  26. {
  27. "term": {
  28. "status": {
  29. "value": "urgent",
  30. "boost": 2.0 <1>
  31. }
  32. }
  33. },
  34. {
  35. "term": {
  36. "status": "normal" <2>
  37. }
  38. }
  39. ]
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. // AUTOSENSE
  45. <1> The `urgent` query clause has a boost of `2.0`, meaning it is twice as important
  46. as the query clause for `normal`.
  47. <2> The `normal` clause has the default neutral boost of `1.0`.
  48. .Why doesn't the `term` query match my document?
  49. **************************************************
  50. String fields can be of type `text` (treated as full text, like the body of an
  51. email), or `keyword` (treated as exact values, like an email address or a
  52. zip code). Exact values (like numbers, dates, and keywords) have
  53. the exact value specified in the field added to the inverted index in order
  54. to make them searchable.
  55. However, `text` fields are `analyzed`. This means that their
  56. values are first passed through an <<analysis,analyzer>> to produce a list of
  57. terms, which are then added to the inverted index.
  58. There are many ways to analyze text: the default
  59. <<analysis-standard-analyzer,`standard` analyzer>> drops most punctuation,
  60. breaks up text into individual words, and lower cases them. For instance,
  61. the `standard` analyzer would turn the string ``Quick Brown Fox!'' into the
  62. terms [`quick`, `brown`, `fox`].
  63. This analysis process makes it possible to search for individual words
  64. within a big block of full text.
  65. The `term` query looks for the *exact* term in the field's inverted index --
  66. it doesn't know anything about the field's analyzer. This makes it useful for
  67. looking up values in keyword fields, or in numeric or date
  68. fields. When querying full text fields, use the
  69. <<query-dsl-match-query,`match` query>> instead, which understands how the field
  70. has been analyzed.
  71. To demonstrate, try out the example below. First, create an index, specifying the field mappings, and index a document:
  72. [source,js]
  73. --------------------------------------------------
  74. PUT my_index
  75. {
  76. "mappings": {
  77. "my_type": {
  78. "properties": {
  79. "full_text": {
  80. "type": "text" <1>
  81. },
  82. "exact_value": {
  83. "type": "keyword" <2>
  84. }
  85. }
  86. }
  87. }
  88. }
  89. PUT my_index/my_type/1
  90. {
  91. "full_text": "Quick Foxes!", <3>
  92. "exact_value": "Quick Foxes!" <4>
  93. }
  94. --------------------------------------------------
  95. // AUTOSENSE
  96. <1> The `full_text` field is of type `text` and will be analyzed.
  97. <2> The `exact_value` field is of type `keyword` and will NOT be analyzed.
  98. <3> The `full_text` inverted index will contain the terms: [`quick`, `foxes`].
  99. <4> The `exact_value` inverted index will contain the exact term: [`Quick Foxes!`].
  100. Now, compare the results for the `term` query and the `match` query:
  101. [source,js]
  102. --------------------------------------------------
  103. GET my_index/my_type/_search
  104. {
  105. "query": {
  106. "term": {
  107. "exact_value": "Quick Foxes!" <1>
  108. }
  109. }
  110. }
  111. GET my_index/my_type/_search
  112. {
  113. "query": {
  114. "term": {
  115. "full_text": "Quick Foxes!" <2>
  116. }
  117. }
  118. }
  119. GET my_index/my_type/_search
  120. {
  121. "query": {
  122. "term": {
  123. "full_text": "foxes" <3>
  124. }
  125. }
  126. }
  127. GET my_index/my_type/_search
  128. {
  129. "query": {
  130. "match": {
  131. "full_text": "Quick Foxes!" <4>
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. // AUTOSENSE
  137. // TEST[continued]
  138. <1> This query matches because the `exact_value` field contains the exact
  139. term `Quick Foxes!`.
  140. <2> This query does not match, because the `full_text` field only contains
  141. the terms `quick` and `foxes`. It does not contain the exact term
  142. `Quick Foxes!`.
  143. <3> A `term` query for the term `foxes` matches the `full_text` field.
  144. <4> This `match` query on the `full_text` field first analyzes the query string,
  145. then looks for documents containing `quick` or `foxes` or both.
  146. **************************************************