I'm a little confused about what is the difference between should
and boost
final score calculation
- when a bool query has a
must
clause, theshould
clauses act as a boost factor, meaning none of them have to match but if they do, the relevancy score for that document will be boosted and thus appear higher in the result. - so,if we have:
one query which contains must
and should
clauses
vs
second query which contains must
clause and boosting
clause
- Is there a difference ?
- when you recommend to use
must
andshould
vsmust
andboosting
clauses in a query ?
CodePudding user response:
You can read the documentation of boolean query here, there is huge difference in the should
and boost
.
Should
and must
both contributes to the _score
of the document, and as mentioned in the above documentation, follows the
The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document.
While boost
is a parameter, using which you can increase the weight
according to your value, let me explain that using an example.
Index sample docs
POST _doc/1
{
"brand" : "samsung",
"name" : "samsung phone"
}
POST _doc/2
{
"brand" : "apple",
"name" : "apple phone"
}
Boolean Query using should
without boost
{
"query": {
"bool": {
"should": [
{
"match": {
"name": {
"query": "apple"
}
}
},
{
"match": {
"brand": {
"query": "apple"
}
}
}
]
}
}
}
Search result showing score
"max_score": 1.3862942,
Now in same query use boost
of factor 10
{
"query": {
"bool": {
"should": [
{
"match": {
"name": {
"query": "apple"
}
}
},
{
"match": {
"brand": {
"query": "apple",
"boost": 10 --> Note additional boost
}
}
}
]
}
}
}
Query result showing boost
"max_score": 7.624619, (Note considerable high score)
In short, when you want to boost a particular document containing your query term, you can additionally pass the boost
param and it will be on top of the normal score calculated by should
or must
.