AI Hints
Home›Elasticsearch Tutorial›The bool Query
Elasticsearch · Lesson 34 of 120

The bool Query

Combine everything.

⏱ 2 min read▶ Video at 31:14🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Real searches mix several conditions. Words in the description, a category, a price limit, and in stock only. The bool query combines other queries into one.

  • Words
  • Category
  • Price
  • In stock

Four clauses

Bool has four kinds of clause. Must clauses have to match, and add to the score. Filter clauses have to match, but don't affect the score. Should clauses are nice to have. And must not clauses have to not match.

keyvalue
musthas to match, scores
filterhas to match, no score
shouldnice to have, boosts
must_notmust not match

Two musts

Here, both clauses must match. The description must match warm, and the category must be Sleeping Bags. Run it. Only warm sleeping bags come back.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source.name
{
  "query": {
    "bool": {
      "must": [
        { "match": { "description": "warm" } },
        { "term": { "category.keyword": "Sleeping Bags" } }
      ]
    }
  }
}
Output
{
  "hits": {
    "hits": [
      {"_source": {"name": "Alpine Down Sleeping Bag"}},
      {"_source": {"name": "Camp Synthetic Sleeping Bag"}},
      {"_source": {"name": "Summer Down Sleeping Bag"}}
    ]
  }
}
Also uses _load
products

Summary

Bool combines queries with must, filter, should and must not. Next, the filter clause.

Key takeaways
  • bool
  • must
  • Combine
Next up: filter: No Scores — fast yes or no