gpt4 book ai didi

elasticsearch - 具有玩家排名的Elasticsearch排行榜

转载 作者:行者123 更新时间:2023-12-02 23:47:04 26 4
gpt4 key购买 nike

我正在尝试找到最佳的解决方案,以使用Elasticsearch实现排行榜。

我将以下数据存储在Elastic中:playerId,challengeId和成绩。

现在,我想添加“位置”列(基于得分),但是找不到哪种方法最有效地做到这一点。

用例:当我获得“所有具有ChallengeId的玩家”时,我希望响应中的“位置”字段的值基于“得分”值。

乍看之下,文档提供了许多方法来执行此操作:聚合,评估API,要素数据类型和要素数据类型。

有人用过这些东西吗?我可以选择其中一种在排行榜中排名玩家吗?

提前致谢。

最佳答案

您可以简单地通过在sort字段上使用score来实现。为了更好的解释,我根据您的示例创建了一个包含一些示例数据的索引。可以在ES doc中找到有关排序功能的更多信息以及示例。

索引映射

{
"mappings": {
"properties": {
"pid": {
"type": "integer"
},
"cid" :{
"type" : "integer"
},
"score" :{
"type" : "integer"
}
}
}
}

索引样本文档
  • POST / _doc / 1

    {
    “pid”:1
    “cid”:1,
    “得分”:10
  • POST / _doc / 2

    {
    “pid”:2
    “cid”:1,
    “得分”:9
  • POST / _doc / 3

    {
    “pid”:4
    “cid”:1,
    “得分”:6
  • POST / _doc / 4

    {
    “pid”:4
    “cid”:2,
    “得分”:10
  • POST / _doc / 5

    {
    “pid”:5
    “cid”:2,
    “得分”:8

  • 示例查询以获取基于 cid aka challengeID的记录,并基于 score aka pidplayerid排序结果。

    搜索查询
    {
    "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的降序排列。

    关于elasticsearch - 具有玩家排名的Elasticsearch排行榜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60504473/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com