4 stars based on
The previous two examples showed a single filter in use. In practice, you will probably need to filter on multiple values or fields. For example, how would you express this SQL in Elasticsearch? This allows us to build filters that can have multiple components in boolean combinations. In this secondary boolean query, we can ignore the filter clause: Each section of the bool filter is optional for example, you can have a must clause and nothing elseand each section can contain a single query or an array of queries.
To replicate the preceding SQL example, we will take the two term queries that we used previously and place them inside the should clause of a bool query, and add another clause to deal with the NOT condition:. This is what enables non-scoring mode. These two term queries are children of the bool query, and since they are placed inside the should clause, at least one of them needs to match.
Our search results return two hits, each document satisfying a different clause in the bool query:. You can already see how nesting boolean queries together can give rise to more sophisticated boolean logic. If you need to perform more complex operations, you can continue nesting boolean queries in any combination, giving rise to arbitrarily complex boolean logic. Because the term and the bool are sibling clauses inside the Boolean shouldat least one of these queries must match for a document to be a hit.
These two term clauses are siblings in a must clause, so they both have to match for a document to be returned as a hit. The results show us two documents, one matching each of the should clauses:. This productID matches the term in the first bool. These two fields match the term filters in the nested bool. This was a simple example, but it demonstrates how Must and should combined in the bool query does not work queries can be used as building blocks to construct complex logical conditions.
The Definitive Guide [2. The equivalent of AND. The equivalent of NOT. The equivalent of OR. This is what enables non-scoring mode These two term queries are must and should combined in the bool query does not work of the bool query, and since they are placed inside the should clause, at least one of them needs to match. Nesting Boolean Queries edit. Be in the know with the latest and greatest from Elastic.
We'll keep you updated with new releases.