term-query.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // CONSOLE
  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. // CONSOLE
  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. A `term` query can also match against <<range, range data types>>.
  49. .Why doesn't the `term` query match my document?
  50. **************************************************
  51. String fields can be of type `text` (treated as full text, like the body of an
  52. email), or `keyword` (treated as exact values, like an email address or a
  53. zip code). Exact values (like numbers, dates, and keywords) have
  54. the exact value specified in the field added to the inverted index in order
  55. to make them searchable.
  56. However, `text` fields are `analyzed`. This means that their
  57. values are first passed through an <<analysis,analyzer>> to produce a list of
  58. terms, which are then added to the inverted index.
  59. There are many ways to analyze text: the default
  60. <<analysis-standard-analyzer,`standard` analyzer>> drops most punctuation,
  61. breaks up text into individual words, and lower cases them. For instance,
  62. the `standard` analyzer would turn the string ``Quick Brown Fox!'' into the
  63. terms [`quick`, `brown`, `fox`].
  64. This analysis process makes it possible to search for individual words
  65. within a big block of full text.
  66. The `term` query looks for the *exact* term in the field's inverted index --
  67. it doesn't know anything about the field's analyzer. This makes it useful for
  68. looking up values in keyword fields, or in numeric or date
  69. fields. When querying full text fields, use the
  70. <<query-dsl-match-query,`match` query>> instead, which understands how the field
  71. has been analyzed.
  72. To demonstrate, try out the example below. First, create an index, specifying the field mappings, and index a document:
  73. [source,js]
  74. --------------------------------------------------
  75. PUT my_index
  76. {
  77. "mappings": {
  78. "_doc": {
  79. "properties": {
  80. "full_text": {
  81. "type": "text" <1>
  82. },
  83. "exact_value": {
  84. "type": "keyword" <2>
  85. }
  86. }
  87. }
  88. }
  89. }
  90. PUT my_index/_doc/1
  91. {
  92. "full_text": "Quick Foxes!", <3>
  93. "exact_value": "Quick Foxes!" <4>
  94. }
  95. --------------------------------------------------
  96. // CONSOLE
  97. <1> The `full_text` field is of type `text` and will be analyzed.
  98. <2> The `exact_value` field is of type `keyword` and will NOT be analyzed.
  99. <3> The `full_text` inverted index will contain the terms: [`quick`, `foxes`].
  100. <4> The `exact_value` inverted index will contain the exact term: [`Quick Foxes!`].
  101. Now, compare the results for the `term` query and the `match` query:
  102. [source,js]
  103. --------------------------------------------------
  104. GET my_index/_search
  105. {
  106. "query": {
  107. "term": {
  108. "exact_value": "Quick Foxes!" <1>
  109. }
  110. }
  111. }
  112. GET my_index/_search
  113. {
  114. "query": {
  115. "term": {
  116. "full_text": "Quick Foxes!" <2>
  117. }
  118. }
  119. }
  120. GET my_index/_search
  121. {
  122. "query": {
  123. "term": {
  124. "full_text": "foxes" <3>
  125. }
  126. }
  127. }
  128. GET my_index/_search
  129. {
  130. "query": {
  131. "match": {
  132. "full_text": "Quick Foxes!" <4>
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // CONSOLE
  138. // TEST[continued]
  139. <1> This query matches because the `exact_value` field contains the exact
  140. term `Quick Foxes!`.
  141. <2> This query does not match, because the `full_text` field only contains
  142. the terms `quick` and `foxes`. It does not contain the exact term
  143. `Quick Foxes!`.
  144. <3> A `term` query for the term `foxes` matches the `full_text` field.
  145. <4> This `match` query on the `full_text` field first analyzes the query string,
  146. then looks for documents containing `quick` or `foxes` or both.
  147. **************************************************