gpt4 book ai didi

python - 具有范围的Elastic Search复合分组

转载 作者:行者123 更新时间:2023-12-02 22:54:15 24 4
gpt4 key购买 nike

考虑以下文件在我的 flex 搜索中。我想根据等级对文档进行分组,但是低于1000的任何等级都必须单独显示,高于1000的任何等级都必须进行分组我如何使用复合聚合来实现此目的,我是新手,而我使用的是复合 Material ,因为我想使用after允许分页的关键功能。

Documents 

{
rank : 200,
name:abcd,
score1 :100,
score2:200
},
{
rank 300,
name:abcd,
score1:100,
score2:200
}
Expected Result:
{
key:{
rank:101
},
doc_count:1,
_score1: {value:3123}
_score2 : {value :3323}
}
{
key:{
rank:1000-*
},
doc_count:1,
_score1: {value:3123}
_score2 : {value :3323}
},
{
key:{
rank:300
},
doc_count:1,
_score1: {value:3123}
_score2 : {value :3323}
}

######## QUery that I tried

{
"query":{"match_all":{}},
"aggs":{
"_scores":{
"composite"{
"sources":[
{"_rank":{"terms":{"field":"rank"}}}
]
}
},
"aggs":{
"_ranks":{
"field":"rank:[
{"to":1000},
{"from":1000}
]
}
"_score1": {"sum": {"field": "score1"}}
"_score2": {"sum": {"field": "score2"}}
}
}
}

最佳答案

据我了解,你想

  • 将值低于1000的聚合分组到其自己的存储桶中
  • 使用键1000-*
  • 将值大于等于1000的聚合分组到单个存储桶中
  • 对于每个存储桶,计算所有存储桶_score1的总和
  • 同样计算所有存储桶的_score2的总和

  • 对于这种情况,您可以简单地使用 Terms Aggregation,正如我在下面的答案中提到的那样。

    我已经提到了示例映射,示例文档,查询和响应,以便您可以清楚了解正在发生的事情。

    对应:
    PUT my_sample_index
    {
    "mappings": {
    "properties": {
    "rank":{
    "type": "integer"
    },
    "name":{
    "type": "keyword"
    },
    "_score1": {
    "type":"integer"
    },
    "_score2":{
    "type": "integer"
    }
    }
    }
    }

    样本文件:
    POST my_sample_index/_doc/1
    {
    "rank": 100,
    "name": "john",
    "_score1": 100,
    "_score2": 100
    }

    POST my_sample_index/_doc/2
    {
    "rank": 1001, <--- Rank > 1000
    "name": "constantine",
    "_score1": 200,
    "_score2": 200
    }

    POST my_sample_index/_doc/3
    {
    "rank": 200,
    "name": "bruce",
    "_score1": 100,
    "_score2": 100
    }

    POST my_sample_index/_doc/4
    {
    "rank": 2001, <--- Rank > 1000
    "name": "arthur",
    "_score1": 200,
    "_score2": 200
    }

    汇总查询:
    POST my_sample_index/_search
    {
    "size":0,
    "aggs": {
    "_score": {
    "terms": {
    "script": {
    "source": """
    if(doc['rank'].value < 1000){
    return doc['rank'];
    }else
    return '1000-*';
    """
    }
    },
    "aggs":{
    "_score1_sum":{
    "sum": {
    "field": "_score1"
    }
    },
    "_score2_sum":{
    "sum":{
    "field": "_score2"
    }
    }
    }
    }
    }
    }

    请注意,我在脚本中通过逻辑提到的地方使用了 Scripted Terms Aggregation。我相信,一旦您了解了逻辑,它就可以自我解释。

    响应:
    {
    "took" : 5,
    "timed_out" : false,
    "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
    },
    "hits" : {
    "total" : {
    "value" : 4,
    "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
    },
    "aggregations" : {
    "_score" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
    {
    "key" : "1000-*", <---- Note this
    "doc_count" : 2, <---- Note this
    "_score2_sum" : {
    "value" : 400.0
    },
    "_score1_sum" : {
    "value" : 400.0
    }
    },
    {
    "key" : "100",
    "doc_count" : 1,
    "_score2_sum" : {
    "value" : 100.0
    },
    "_score1_sum" : {
    "value" : 100.0
    }
    },
    {
    "key" : "200",
    "doc_count" : 1,
    "_score2_sum" : {
    "value" : 100.0
    },
    "_score1_sum" : {
    "value" : 100.0
    }
    }
    ]
    }
    }
    }

    请注意,有两个具有 rank > 1000的键,它们对 _score1_score2的得分都等于 400,这是预期的结果。

    让我知道这是否有帮助!

    关于python - 具有范围的Elastic Search复合分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60693770/

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