My requirement is to rank result based on multiple metadata fields. I have fields like category,country. The result should be ranked based on category and country. My query is provided below :
{"query":
{"bool":
{"must":
{"match": {"Body": {"query":"something","operator": "and"}}},
"should": [{"match":{"Category":{"query": "national","boost":3}}},
{"match":{"Category":{"query":"sports","boost":2}}},
{"match": {"Category":{"query":"politics","boost":1}}},
{"match": {"Country": {"query": "US","boost":5}}}
]}}}
Here the result is getting boosted based on user interests(category) and country. I expect that the result to be first sorted by country and then based on values of category. But category sorting is not working properly. Please let me know if there is any correction needed in my query
Hi Prasenjit,
The score should not be used to sort on documents. Score stands for text relevance and if you sort basically you discard all text relevance.
That said, if you want that national category be more important than sports you should use boost. But if you want that all countries came before other country you use sort.
In my following example I will use search_type=dfs_query_then_fetch
just because it has few documents in order to not interfere in the relevance. In production you don't need to use it.
POST ranking/doc/1
{
"body": "something",
"category": "national",
"country": "br"
}
POST ranking/doc/2
{
"body": "something",
"category": "sports",
"country": "br"
}
POST ranking/doc/3
{
"body": "something",
"category": "politics",
"country": "br"
}
POST ranking/doc/4
{
"body": "something",
"category": "national",
"country": "us"
}
POST ranking/doc/5
{
"body": "something",
"category": "sports",
"country": "us"
}
POST ranking/doc/6
{
"body": "something",
"category": "politics",
"country": "us"
}
Using sort + _score in the query:
GET ranking/doc/_search?search_type=dfs_query_then_fetch
{
"query": {
"bool": {
"must": [
{
"match": {
"body": "something"
}
}
],
"should": [
{
"match": {
"category": {
"query": "national",
"boost": 3
}
}
},
{
"match": {
"category": {
"query": "sports",
"boost": 2
}
}
},
{
"match": {
"category": {
"query": "politics",
"boost": 1
}
}
}
]
}
},
"sort": [
{
"country.keyword": {
"order": "desc"
}
},
"_score"
]
}
Please let me know if that works for you!
Best,
LG
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.