Context : I'm relatively new to ElasticSearch, so maybe there's a concept I'm missing.
I'd like to boost the relevance score of documents that have one or more tags (the more tags, the better the score), without filtering out the documents that do not have any tag.
Attempted solutions :
I tried to use a terms query :
{
"query": {
"terms": {
tags: ['some','tags','to','boost'],
minimum_should_match: 0
}
}
}
But the minimum_should_match
option seems to be ignored (the doc is not quite clear on this, but I think this option MUST be different from zero).
I also tried to "cheat" a bit using a bool should
query with match_all
:
{
"query": {
"bool": {
"should" : [
{"terms": {tags: ['some','tags','to','boost']}},
{"match_all": {}}
]
}
}
}
But for some reason the results not having any tag get filtered out anyway.
I also tried to use a function_score
or a boosting_query
but could not figure out a valid syntax that preserves the relevance score generated by the terms query.
Edit :
There seems to be a working solution in the spirit of the "bool cheat" :
{
"query": {
"dis_max": {
"queries" : [
{"terms": {tags: ['some','tags','to','boost']}},
{"match_all": {}}
]
}
}
}
But :
- this feels hackish,
- I don't know how to control precisely the relevance generated in this case
Question : I feel that this must be a common and simple use case, so is there a way to use a terms query without filtering out the documents that don't match ?