bool-query.asciidoc 4.4 KB

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