bool-query.asciidoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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` 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: If this query is used in a filter context and it has `should`
  24. clauses then at least one `should` clause is required to match.
  25. The bool query also supports `disable_coord` parameter (defaults to
  26. `false`). Basically the coord similarity computes a score factor based
  27. on the fraction of all query terms that a document contains. See Lucene
  28. `BooleanQuery` for more details.
  29. The `bool` query takes a _more-matches-is-better_ approach, so the score from
  30. each matching `must` or `should` clause will be added together to provide the
  31. final `_score` for each document.
  32. [source,js]
  33. --------------------------------------------------
  34. {
  35. "bool" : {
  36. "must" : {
  37. "term" : { "user" : "kimchy" }
  38. },
  39. "filter": {
  40. "term" : { "tag" : "tech" }
  41. }
  42. "must_not" : {
  43. "range" : {
  44. "age" : { "from" : 10, "to" : 20 }
  45. }
  46. },
  47. "should" : [
  48. {
  49. "term" : { "tag" : "wow" }
  50. },
  51. {
  52. "term" : { "tag" : "elasticsearch" }
  53. }
  54. ],
  55. "minimum_should_match" : 1,
  56. "boost" : 1.0
  57. }
  58. }
  59. --------------------------------------------------