您只需在score 字段上使用sort 即可实现它。为了更好地解释,我根据您的示例创建了一个包含一些示例数据的索引。更多关于排序功能的信息和示例可以在ES doc找到。
索引映射
{
"mappings": {
"properties": {
"pid": {
"type": "integer"
},
"cid" :{
"type" : "integer"
},
"score" :{
"type" : "integer"
}
}
}
}
索引示例文档
-
POST /_doc/1
{
“PID”:1,
“cid”:1,
“分数”:10
}
-
发布 /_doc/2
{
“PID”:2,
“cid”:1,
“分数”:9
}
-
发布 /_doc/3
{
“pid”:4,
“cid”:1,
“分数”:6
}
-
POST /_doc/4
{
“pid”:4,
“cid”:2,
“分数”:10
}
-
发布 /_doc/5
{
“pid”:5,
“cid”:2,
“分数”:8
}
基于cid aka challengeID 获取记录并根据pid aka playerid 的score 对结果进行排序的示例查询。
搜索查询
{
"sort": [
{
"score": {
"order": "desc" --> notice `desc` order on `score` field
}
}
],
"query": {
"term": {
"cid": 2
}
}
}
上述搜索查询的输出
"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
]
}
]
}
注意,搜索查询返回了 2 个具有 cid=2 的文档,并且玩家 ID 按其 score 的降序排列。