How to get average, max, min etc. from a group of object in ElasticSearch How to get average, max, min etc. from a group of object in ElasticSearch elasticsearch elasticsearch

How to get average, max, min etc. from a group of object in ElasticSearch


You should use the statistical facet (doc)

The query should be something like:

{    "query" : {        "match_all" : {}    },    "facets" : {        "statload_time" : {            "statistical" : {                "field" : "load_time"            }        },        "statexec_time" : {            "statistical" : {                "field" : "execution_time"            }        },        "statlog_time" : {            "statistical" : {                "field" : "log_time"            }        }    }}

As the doc says:

The statistical data include count, total, sum of squares, mean (average), minimum, maximum, variance, and standard deviation

After indexing a couple of documents, the response of the query is:

{  "took" : 2,  "timed_out" : false,  "_shards" : {    "total" : 5,    "successful" : 5,    "failed" : 0  },  "hits" : {    "total" : 2,    "max_score" : 1.0,    "hits" : [ ]  },  "facets" : {    "statload_time" : {      "_type" : "statistical",      "count" : 2,      "total" : 7.0,      "min" : 2.0,      "max" : 5.0,      "mean" : 3.5,      "sum_of_squares" : 29.0,      "variance" : 2.25,      "std_deviation" : 1.5    },    "statexec_time" : {      "_type" : "statistical",      "count" : 2,      "total" : 12.0,      "min" : 5.0,      "max" : 7.0,      "mean" : 6.0,      "sum_of_squares" : 74.0,      "variance" : 1.0,      "std_deviation" : 1.0    },    "statlog_time" : {      "_type" : "statistical",      "count" : 2,      "total" : 16.0,      "min" : 7.0,      "max" : 9.0,      "mean" : 8.0,      "sum_of_squares" : 130.0,      "variance" : 1.0,      "std_deviation" : 1.0    }  }}