You can achieve it simply by using sort
on your score
field. For a better explanation, I created an index with some sample data according to your example. More info on sort feature with an example can be found in ES doc.
Index mapping
{
"mappings": {
"properties": {
"pid": {
"type": "integer"
},
"cid" :{
"type" : "integer"
},
"score" :{
"type" : "integer"
}
}
}
}
Index sample docs
POST /_doc/1
{
"pid" : 1,
"cid" :1,
"score" : 10
}
POST /_doc/2
{
"pid" : 2,
"cid" :1,
"score" : 9
}
POST /_doc/3
{
"pid" : 4,
"cid" :1,
"score" : 6
}
POST /_doc/4
{
"pid" : 4,
"cid" :2,
"score" : 10
}
POST /_doc/5
{
"pid" : 5,
"cid" :2,
"score" : 8
}
Sample query to fetch record based on cid
aka challengeID
and sort results based on score
of pid
aka playerid
.
Search query
{
"sort": [
{
"score": {
"order": "desc" --> notice `desc` order on `score` field
}
}
],
"query": {
"term": {
"cid": 2
}
}
}
The output of the above search query
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "leaderboard",
"_type": "_doc",
"_id": "4",
"_score": null,
"_source": {
"pid": 4,
"cid": 2,
"score": 10
},
"sort": [
10
]
},
{
"_index": "leaderboard",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"pid": 5,
"cid": 2,
"score": 8
},
"sort": [
8
]
}
]
}
Notice, search query returned 2 docs which have cid
=2 and player ids are sorted in desc order of their score
.