Of course, the bool
query isn’t restricted to combining simple one-word
match
queries. It can combine any other query, including other bool
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 _score
than
those that don’t, which means that they will appear higher in the list of
results.
A simple bool
query allows us to write this fairly complex logic as follows:
GET /_search
{
"query": {
"bool": {
"must": {
"match": {
"content": { (1)
"query": "full text search",
"operator": "and"
}
}
},
"should": [ (2)
{ "match": { "content": "Elasticsearch" }},
{ "match": { "content": "Lucene" }}
]
}
}
}
-
The
content
field must contain all of the wordsfull
,text
, andsearch
. -
If the
content
field also containsElasticsearch
orLucene
, the document will receive a higher_score
.
The more should
clauses that match, the more relevant the document. So far,
so good.
But what if we want to give more weight to the docs that contain Lucene
and
even more weight to the docs containing Elasticsearch
?
We can control the relative weight of any query clause by specifying a boost
value, which defaults to 1
. A boost
value greater than 1
increases the
relative weight of that clause. So we could rewrite the preceding query as
follows:
GET /_search
{
"query": {
"bool": {
"must": {
"match": { (1)
"content": {
"query": "full text search",
"operator": "and"
}
}
},
"should": [
{ "match": {
"content": {
"query": "Elasticsearch",
"boost": 3 (2)
}
}},
{ "match": {
"content": {
"query": "Lucene",
"boost": 2 (3)
}
}}
]
}
}
}
-
These clauses use the default
boost
of1
. -
This clause is the most important, as it has the highest
boost
. -
This clause is more important than the default, but not as important as the
Elasticsearch
clause.
Note
|
The Instead, the new 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
|
We present other ways of combining queries in the next chapter, [multi-field-search]. But first, let’s take a look at the other important feature of queries: text analysis.