使用 elasticsearch-dsl python 客户端进行求和计算

sum calculation using elasticsearch-dsl python client

我有一个名为 '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" } } } }

此查询 returns 输出如下

{
    "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 时,代码如下所示并且它 returns 相同的输出。

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()

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

应该这样做:

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

然后,当您执行查询时,您可以访问聚合结果:

response.aggregations.total_cost.value

希望对您有所帮助!