ElasticSearch 查询某个字段中具有最高值的文档

问题描述 投票:0回答:1

我对 ElasticSearch 还很陌生(这里使用 8.12),只是想掌握它的基础知识。
有没有办法从 ElasticSearch 查询文档,以便具有最高值的文档获得最大的评分值?
因此对于以下文件:

{ "id": 1, "rating": 2.134, "prev_rating": 3.321 },
{ "id": 2, "rating": 3.134, "prev_rating": 3.912 },
{ "id": 3, "rating": 4.134, "prev_rating": 4.321 },
{ "id": 4, "rating": 4.434, "prev_rating": 4.421 },
{ "id": 5, "rating": 4.834, "prev_rating": 4.921 }

将收到以下分数
分数不按比例缩放,而是强调我期望这些文档在查询期间如何获得分数):

{ "id": 5, "rating": 4.834, "prev_rating": 4.921, "_score": 20 }
{ "id": 4, "rating": 4.434, "prev_rating": 4.421, "_score": 13 },
{ "id": 3, "rating": 4.134, "prev_rating": 4.321, "_score": 10 },
{ "id": 2, "rating": 3.134, "prev_rating": 3.912, "_score": "should not event be scored/selected" },
{ "id": 1, "rating": 2.134, "prev_rating": 3.321, "_score": "should not event be scored/selected" },

我尝试在查询中使用“boost”,但它只是产生了基本相同的响应,但“_score”的值更大。
并且“范围”查询似乎并没有因为“评级”或“prev_ rating”的值较大而对文档评分更高
查询示例:

{
   "query":{
      "bool":{
         "must":[
            {
               "range":{
                  "prev_rating":{
                     "gte":4.1
                  }
               }
            },
            {
               "range":{
                  "rating":{
                     "gte":4
                  }
               }
            }
         ]
      }
   }
}
elasticsearch
1个回答
0
投票

听起来您可能想使用

rank_feature
来实现这一点。
rank_feature
是一种特殊的字段类型和查询,它根据一个或多个存储的“排名”字段对文档进行评分。

您也可以通过脚本分数

获得类似的结果
© www.soinside.com 2019 - 2024. All rights reserved.