bool-query.asciidoc 3.7 KB

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