# Min\_doc\_count combined with order by average

**URL:** <https://discuss.elastic.co/t/min-doc-count-combined-with-order-by-average/229245>\
**Category:** Elasticsearch\
**Created:** [April 22, 2020, 11:36am UTC](https://discuss.elastic.co/t/min-doc-count-combined-with-order-by-average/229245 "2020-04-22T11:36:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Hansen\_Jam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_hansen_jam/32/44246_2.png) [@Christian\_Hansen\_Jam](https://discuss.elastic.co/u/Christian_Hansen_Jam)\
**Post date:** [April 22, 2020, 11:36am UTC](https://discuss.elastic.co/t/min-doc-count-combined-with-order-by-average/229245/1 "2020-04-22T11:36:34Z")

</div>

As the topic says, Im having problem sorting data when combining with min\_doc\_count with an average value.  
The data om working with is logfiles from a webserver. Two of the field are url.path and event.duration.  
My goal is to identify the slow endpoints.  
This is fairly easy and solved that problem with the following query.

```auto
"aggs" : {
     "endpoints" : {
         "terms" : { 
           "field" : "url.path",
           "size" : 20,
           "order": {
             "average_responsetime": "desc"
           }
         },
         "aggs": {
           "average_responsetime": {
             "avg": {
               "field": "event.duration"
             }
           }
         }
     }
 }

```

When looking a the data i realized that the top results were bucket sizes of one or two. This makes sence, since they are not cached and thus has a long response time and are not really that interesting. To get rid of these small buckets i found the min\_doc\_count and turned it up to 100.

```auto
"aggs" : {
        "endpoints" : {
            "terms" : { 
              "field" : "url.path",
              "size" : 20,
              "min_doc_count": 10, 
              "order": {
                "average_responsetime": "desc"
              }
            },
            "aggs": {
              "average_responsetime": {
                "avg": {
                  "field": "event.duration"
                }
              }
            }
        }
}

```

But this does not work. The only way to get min\_doc\_count working is by removing

```auto
"order": {
    "average_responsetime": "desc"
}

```

but then I dont get the documents im interested in.  
Any ideas on how i can get this to work ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 20, 2020, 11:36am UTC](https://discuss.elastic.co/t/min-doc-count-combined-with-order-by-average/229245/2 "2020-05-20T11:36:40Z")

</div>

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