WARNING: The 2.x versions of Elasticsearch have passed their EOL dates. If you are running a 2.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
Of course, the
bool query isn’t restricted to combining simple one-word
match queries. It can combine any other query, including other
queries. It is commonly used to fine-tune the relevance
_score for each
document by combining the scores from several distinct queries.
Imagine that we want to search for documents about "full-text search," but we
want to give more weight to documents that also mention "Elasticsearch" or
"Lucene." By more weight, we mean that documents mentioning
"Elasticsearch" or "Lucene" will receive a higher relevance
those that don’t, which means that they will appear higher in the list of
bool query allows us to write this fairly complex logic as follows:
should clauses that match, the more relevant the document. So far,
But what if we want to give more weight to the docs that contain
even more weight to the docs containing
We can control the relative weight of any query clause by specifying a
value, which defaults to
boost value greater than
1 increases the
relative weight of that clause. So we could rewrite the preceding query as
These clauses use the default
This clause is the most important, as it has the highest
This clause is more important than the default, but not as important
boost parameter is used to increase the relative weight of a clause
boost greater than
1) or decrease the relative weight (with a
1), but the increase or decrease is not linear. In
other words, a
2 does not result in double the
Instead, the new
_score is normalized after the boost is applied. Each
type of query has its own normalization algorithm, and the details are beyond
the scope of this book. Suffice to say that a higher
boost value results in
If you are implementing your own scoring model not based on TF/IDF and you
need more control over the boosting process, you can use the
function_score query to manipulate a document’s
boost without the normalization step.
We present other ways of combining queries in the next chapter, Multifield Search. But first, let’s take a look at the other important feature of queries: text analysis.