'Boosting has no effect in a Boolean-filtered query in Elasticsearch
I'm trying to add a boost to documents that match to a term filter. The basis is a Boolean/MatchAll query. But the boosting in my Elasticsearch query has no effect. All result scores are set to 1:
curl -XPOST localhost:9200/wiki_content/_search?pretty -d '
{
"_source": [
"title"
],
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"filter": [
{
"bool": {
"should": [
{
"term": {
"title.keyword": {
"value": "Main Page",
"boost": 9
}
}
},
{
"term": {
"title.keyword": {
"value": "Top Page",
"boost": 999
}
}
}
]
}
}
]
}
}
}
'
However, when using a filtered query, the boosting works. But due to restrictions in my system I cannot use a filtered query. So is there any method to make the boosting in the original query work?
Solution 1:[1]
In the filter part of the query, boosting will have no effect, as the filters only job is to, ehhm, filter queries that match certain values. Try instead:
curl -XPOST localhost:9200/wiki_content/_search?pretty -d '
{
"_source": [
"title"
],
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"should": [
{
"term": {
"title.keyword": {
"value": "Main Page",
"boost": 9
}
}
},
{
"term": {
"title.keyword": {
"value": "Top Page",
"boost": 999
}
}
}
]
}
}
}
'
...moving the two term-queries directly into the should-clause in your top level bool query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jonasfh |
