Query DSL for MoM, YoY, WoW calculation


(Dheeran) #1

Hi,

I'm in situation to calculate MoM, YoY, WoW for a metric. I'm thinking of fetching all the docs and filtering in programming language. But I feel its not a right way. Can some one help me in forming Query DSL so that load will be on ES server.


(Adrien Grand) #2

I suspect you are looking for the date_histogram aggregation? https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

Your aggregation would look something like that for YoY:

POST /sales/_search?size=0
{
    "aggs" : {
        "YoY" : {
            "date_histogram" : {
                "field" : "my_date_field",
                "interval" : "1Y"
            },
            "aggs": {
              "metric_YoY": {
                "avg": {
                  "field": "my_metric_field"
                }
              }
            }
        }
    }
}

I used an average aggregation so that you can follow how the average value of the my_metric_field moves over time, but you can use any aggregation, eg. sum, percentiles, etc.


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.