Reputation: 185
I have 3 documents in a mapping look like this
{
"tags":["a"],
"count":10
}
{
"tags":["a","b","c"],
"count":50
}
{
"tags":["a","c"],
"count":100
}
When I search by tags, say, {"should":["tags":"a", "tags":"b"]}
Want the result to be sorted by number of tag match, then by count desc, like this:
{
"tags":["a","b","c"],
"count":50
}
{
"tags":["a","c"],
"count":100
}
{
"tags":["a"],
"count":10
}
How can I achieve this? I tried sort [_score, {"count":"desc"}], however, because of the score values are all different, the {"count":"desc"} part never worked.
Any help is appreciated. Thanks.
Upvotes: 2
Views: 810
Reputation: 11
This will sort results by score and by count:
{
"query": {
"bool": {
"should": [
{"term": {"tag": "a"}},
{"term": {"tag": "b"}}
]
}
},
"sort": {
"_score": {
"order": "desc"
},
"count": {
"order": "desc"
}
}
}
Upvotes: 1