Compound Queries: Boolean Query
Explore the boolean query and learn how to utilize it.
We'll cover the following
- Boolean query
- Boolean query syntax
- Example
- Creating data
- Queries
- Retrieving all the products in the "Electronics" category with a price less than 500
- Retrieving products that are not in the "Electronics" category and are currently available ("in stock")
- Retrieving products with the price field between 100 and 1000, excluding those in the "Fashion" category, and prioritizing products that contain the term "Samsung" in their name
- Kibana widget
Boolean query
A boolean query is a compound query type that enables the combination of multiple subqueries using boolean operators. These operators include the following:
-
must
: Themust
clause specifies criteria that must be satisfied by the matching documents. In other words, the documents must match the conditions specified in themust
clause to be considered search results. -
must_not
: Themust_not
clause excludes documents that match the specified conditions. It sets criteria that must not be satisfied by the documents for them to be considered valid matches. -
should
: Theshould
clause specifies the criteria that are desirable but not mandatory for the matching documents. Documents that satisfy theshould
conditions will have a higher relevance score, making them more likely to appear in the search results. However, documents that do not meet theshould
conditions can still be considered valid matches. -
filter
: Thefilter
clause is similar to themust
clause in terms of mandatory conditions. However, unlike themust
clause, thefilter
clause does not contribute to score calculation. It is primarily used for filtering documents based on specific criteria, such as exact matches or range conditions, without affecting the relevance ranking.
By utilizing boolean queries, we can construct complex search conditions and control the scoring and relevance of the search results.
Get hands-on with 1400+ tech skills courses.