bool-query.asciidoc 4.0 KB

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