Elastic Search getting average of doc_count out of terms aggregation
Asked Answered
R

1

6

I have terms query as follow in elastic search.

GET http://localhost:9200/adapters/request/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match" : { "adapterName" : "EnginePushableAdapter" }
            },
            "filter": {
                "range" : {
                    "request.requestTime" : {
                        "gte" : 1492080226172,
                        "lte" : 1492080725964
                    }
                }
            }
        }
    }, 
    "aggs" : {
        "call_count" : {
            "terms" : {
                "field" : "deviceId"          
            }
        }
    }
}

Is there a way to calcaulate the average of doc_count returned by terms aggregation?like this

    "avg_agg" : {
        "avg": { "field": "call_count.doc_count" }

    }

In other words, I want to take average of the previous aggregation fields.

Retrospective answered 13/4, 2017 at 11:57 Comment(0)
H
7

I think you may be looking for the average bucket aggregation.

https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-aggregations-pipeline-avg-bucket-aggregation.html

Try something like

...
"my_count_avg": {
  "avg_bucket": {
    "buckets_path": "call_count>_count"
  }
}
Hamm answered 13/4, 2017 at 13:58 Comment(2)
Doing this, the result is the average count calculated of the top 10 buckets. How can I average all the buckets?Nureyev
You can change the size parameter to return more buckets, but there may be an upper limit, Integer.MAX_VALUE I thinkHamm

© 2022 - 2024 — McMap. All rights reserved.