【问题标题】:sum calculation using elasticsearch-dsl python client使用 elasticsearch-dsl python 客户端求和计算
【发布时间】:2018-01-25 09:38:43
【问题描述】:

我有一个名为“demoadmin”的 Eleaticsearch 索引,类型为“billing”。 我已经使用 POST 成功执行了以下查询正文。

{"query": { "filtered":{ "query" : {"match_all": {}}, "filter": { "bool": { "must":[{ "term": { "Month": "01" }}, { "term": { "Year": "2018" }}] } } } }, "size": 0, "aggregations": { "Total Cost": { "sum": { "field": "UnBlendedCost" } } } }

这个查询返回如下输出

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 503206,
        "max_score": 0,
        "hits": [ ]
    },
    "aggregations": {
        "Total Cost": {
            "value": 5849.867677253019
        }
    }
}

查询和输出在 Elasticsearch-head 中如下所示

Query and output in Elasticsearch - head

当我尝试使用 elasticsearch python API 将此代码转换为 python 时,代码如下所示,它返回相同的输出。

ADMIN_INDEX_NAME ='demoadmin'
es_client = get_es_client()
def get_aggregated_total_cost_for_current_month(month,year):
    '''Get Aggregated total cost for the current month'''
    body = '{"query": { "filtered":{ "query" : {"match_all": {}}, "filter": { "bool": { "must":[{ "term": { "Month": "' + month + '" }}, { "term": { "Year": "' + year + '" }}] } } } }, "size": 0, "aggregations": { "Total Cost": { "sum": { "field": "UnBlendedCost" } } } }'
    total_cost_result = es_client.search(index=ADMIN_INDEX_NAME, doc_type="billing", body=body)
    raw_total_cost = total_cost_result['aggregations']['Total Cost']['value']
    total_cost = float("%.2f" % raw_total_cost)
    print(str(total_cost))
    return total_cost

我正在尝试在 elasticsearch-dsl 中转换代码并卡住了。 我已经完成了应用过滤器,但之后对要做什么感到困惑。 到目前为止,我已经在 elasticsearch-dsl 中实现了代码,如下所示

def get_aggregated_total_cost_for_current_month_new(month,year):
    '''Get Aggregated total cost for the current month'''
    s = Search(using=es_client, index=ADMIN_INDEX_NAME, doc_type="billing") \
        .filter("term", Month=month).filter("term", Year=year)
    response = s.execute()

不知道如何从这里开始。 有人可以在聚合部分帮助我吗?

【问题讨论】:

    标签: python elasticsearch-dsl elasticsearch-py elasticsearch-dsl-py


    【解决方案1】:

    应该这样做:

    # set size 0
    s = s[:0]
    s.aggs.metric('total_cost', 'sum', field="UnBlendedCost")
    

    然后,当您执行查询时,您可以通过以下方式访问 agg 结果:

    response.aggregations.total_cost.value
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-03-02
      • 2014-09-19
      • 2016-11-21
      • 2016-01-18
      • 1970-01-01
      • 2017-12-21
      • 2017-07-05
      • 2014-07-05
      • 2019-10-14
      相关资源
      最近更新 更多