Bool query

The bool query in Elasticsearch is your Swiss Army knife. It can help you write many types of complex queries. If you are come from an SQL background, you already know how to filter based on multiple AND and OR conditions in the WHERE clause. The bool query allows you to combine multiple scoring and non-scoring queries.

Let's first see how to implement simple AND and OR conjunctions. 

A bool query has the following sections:

GET /amazon_products/_search
{
"query": {
"bool": {
"must": [...], scoring queries executed in query context
"should": [...], scoring queries executed in query context
"filter": {}, non-scoring queries executed in filter context
"must_not": [...] non-scoring queries executed in filter context
}
}
}

The queries included in must and should clauses are executed in a query context unless the whole bool query is included inside a filter context.

The filter and must_not queries are always executed in the filter context. They will always return a score of zero and only contribute to the filtering of documents.

Let's look at how to form a non-scoring query that just performs a structured search. We will gain an understanding of how to formulate the following types of structured search queries using the bool query:

  • Combining OR conditions
  • Combining AND and OR conditions
  • Adding NOT conditions
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.13.179