Search code examples
pythonelasticsearch-dslelasticsearch-pyelasticsearch-dsl-py

sum calculation using elasticsearch-dsl python client


I have an Eleaticsearch index named 'demoadmin' with type named 'billing'. I have executed this following query body using POST successfully .

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

this query returns the output like the following

{
    "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
        }
    }
}

The query and the outputs looks like the following in Elasticsearch-head

Query and output in Elasticsearch - head

When I am trying to converting this code to python using elasticsearch python API ,the code is like the following and it returns the same output.

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

I am trying to convert the code in elasticsearch-dsl and got stuck. I have completed up to applying the filter but after that getting confused on what to do. So far I have implemented the code in elasticsearch-dsl like the following

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

Not sure how to proceed from here. Could some body help me on the aggregation part ?


Solution

  • This should do it:

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

    then, when you execute your query you can access the agg result as:

    response.aggregations.total_cost.value
    

    Hope this helps!