gpt4 book ai didi

elasticsearch - 在Elasticsearch中可以计算 “distinct sum”和 “distinct average”吗?

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

如何在Elasticsearch中计算“不同的平均值”?我有一些这样的非规范化数据:

{ "record_id" : "100", "cost" : 42 }
{ "record_id" : "200", "cost" : 67 }
{ "record_id" : "200", "cost" : 67 }
{ "record_id" : "200", "cost" : 67 }
{ "record_id" : "400", "cost" : 11 }
{ "record_id" : "400", "cost" : 11 }
{ "record_id" : "500", "cost" : 10 }
{ "record_id" : "600", "cost" : 99 }

请注意,对于给定的“record_id”,“成本”始终是相同的。

因此,根据以上数据:
  • 如何获取“成本”字段的平均值但由“record_id”区分?
    结果将是(42 + 67 + 11 + 10 + 99)/5=45.8
  • 如何获取“费用”字段的总和值,但如何通过“record_id”区分?
    结果将是42 + 67 + 11 + 10 + 99 = 229

  • 我可以结合使用“术语”聚合,然后结合“第一”和“平均”子聚合吗?我在想这样的事情: elasticsearch calculate average of unique values

    最佳答案

    它不适用于terms aggs。这是使用无痛脚本的可能方式:

    编制索引-您的实际映射可能与生成的默认映射不同(特别是.keyword上的rec_id部分):

    POST _bulk
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"100","cost":42}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"200","cost":67}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"200","cost":67}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"200","cost":67}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"400","cost":11}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"400","cost":11}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"500","cost":10}
    {"index":{"_index":"uniques","_type":"_doc"}}
    {"record_id":"600","cost":99}

    然后汇总
    GET uniques/_search
    {
    "size": 0,
    "aggs": {
    "terms": {
    "scripted_metric": {
    "init_script": "state.id_map = [:]; state.sum = 0.0; state.elem_count = 0.0;",
    "map_script": """
    def id = doc['record_id.keyword'].value;
    if (!state.id_map.containsKey(id)) {
    state.id_map[id] = true;
    state.elem_count++;
    state.sum += doc['cost'].value;
    }
    """,
    "combine_script": """
    def sum = state.sum;
    def avg = sum / state.elem_count;

    def stats = [:];
    stats.sum = sum;
    stats.avg = avg;

    return stats
    """,
    "reduce_script": "return states"
    }
    }
    }
    }

    并屈服
    ...
    "aggregations" : {
    "terms" : {
    "value" : [
    {
    "avg" : 45.8,
    "sum" : 229.0
    }
    ]
    }
    }

    关于elasticsearch - 在Elasticsearch中可以计算 “distinct sum”和 “distinct average”吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61508849/

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