bool-query.asciidoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. [[query-dsl-bool-query]]
  2. === Bool Query
  3. A query that matches documents matching boolean combinations of other
  4. queries. The bool query maps to Lucene `BooleanQuery`. It is built using
  5. one or more boolean clauses, each clause with a typed occurrence. The
  6. occurrence types are:
  7. [cols="<,<",options="header",]
  8. |=======================================================================
  9. |Occur |Description
  10. |`must` |The clause (query) must appear in matching documents and will
  11. contribute to the score.
  12. |`filter` |The clause (query) must appear in matching documents. However unlike
  13. `must` the score of the query will be ignored. Filter clauses are executed
  14. in <<query-filter-context,filter context>>, meaning that scoring is ignored
  15. and clauses are considered for caching.
  16. |`should` |The clause (query) should appear in the matching document. If the
  17. `bool` query is in a <<query-filter-context,query context>> and has a `must` or
  18. `filter` clause then a document will match the `bool` query even if none of the
  19. `should` queries match. In this case these clauses are only used to influence
  20. the score. If the `bool` query is a <<query-filter-context,filter context>>
  21. or has neither `must` or `filter` then at least one of the `should` queries
  22. must match a document for it to match the `bool` query. This behavior may be
  23. explicitly controlled by settings the
  24. <<query-dsl-minimum-should-match,`minimum_should_match`>> parameter.
  25. |`must_not` |The clause (query) must not appear in the matching
  26. documents. Clauses are executed in <<query-filter-context,filter context>> meaning
  27. that scoring is ignored and clauses are considered for caching. Because scoring is
  28. ignored, a score of `0` for all documents is returned.
  29. |=======================================================================
  30. [IMPORTANT]
  31. .Bool query in filter context
  32. ========================================================================
  33. If this query is used in a filter context and it has `should`
  34. clauses then at least one `should` clause is required to match.
  35. ========================================================================
  36. The `bool` query takes a _more-matches-is-better_ approach, so the score from
  37. each matching `must` or `should` clause will be added together to provide the
  38. final `_score` for each document.
  39. [source,js]
  40. --------------------------------------------------
  41. POST _search
  42. {
  43. "query": {
  44. "bool" : {
  45. "must" : {
  46. "term" : { "user" : "kimchy" }
  47. },
  48. "filter": {
  49. "term" : { "tag" : "tech" }
  50. },
  51. "must_not" : {
  52. "range" : {
  53. "age" : { "gte" : 10, "lte" : 20 }
  54. }
  55. },
  56. "should" : [
  57. { "term" : { "tag" : "wow" } },
  58. { "term" : { "tag" : "elasticsearch" } }
  59. ],
  60. "minimum_should_match" : 1,
  61. "boost" : 1.0
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // CONSOLE
  67. ==== Scoring with `bool.filter`
  68. Queries specified under the `filter` element have no effect on scoring --
  69. scores are returned as `0`. Scores are only affected by the query that has
  70. been specified. For instance, all three of the following queries return
  71. all documents where the `status` field contains the term `active`.
  72. This first query assigns a score of `0` to all documents, as no scoring
  73. query has been specified:
  74. [source,js]
  75. ---------------------------------
  76. GET _search
  77. {
  78. "query": {
  79. "bool": {
  80. "filter": {
  81. "term": {
  82. "status": "active"
  83. }
  84. }
  85. }
  86. }
  87. }
  88. ---------------------------------
  89. // CONSOLE
  90. This `bool` query has a `match_all` query, which assigns a score of `1.0` to
  91. all documents.
  92. [source,js]
  93. ---------------------------------
  94. GET _search
  95. {
  96. "query": {
  97. "bool": {
  98. "must": {
  99. "match_all": {}
  100. },
  101. "filter": {
  102. "term": {
  103. "status": "active"
  104. }
  105. }
  106. }
  107. }
  108. }
  109. ---------------------------------
  110. // CONSOLE
  111. This `constant_score` query behaves in exactly the same way as the second example above.
  112. The `constant_score` query assigns a score of `1.0` to all documents matched
  113. by the filter.
  114. [source,js]
  115. ---------------------------------
  116. GET _search
  117. {
  118. "query": {
  119. "constant_score": {
  120. "filter": {
  121. "term": {
  122. "status": "active"
  123. }
  124. }
  125. }
  126. }
  127. }
  128. ---------------------------------
  129. // CONSOLE
  130. ==== Using named queries to see which clauses matched
  131. If you need to know which of the clauses in the bool query matched the documents
  132. returned from the query, you can use
  133. <<search-request-named-queries-and-filters,named queries>> to assign a name to
  134. each clause.