# Kibana barchart mandatory order by limits the use of min\_doc\_count

**URL:** <https://discuss.elastic.co/t/kibana-barchart-mandatory-order-by-limits-the-use-of-min-doc-count/172984>\
**Category:** Kibana\
**Created:** [March 19, 2019, 1:50pm UTC](https://discuss.elastic.co/t/kibana-barchart-mandatory-order-by-limits-the-use-of-min-doc-count/172984 "2019-03-19T13:50:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mda](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@mda](https://discuss.elastic.co/u/mda)\
**Post date:** [March 19, 2019, 1:50pm UTC](https://discuss.elastic.co/t/kibana-barchart-mandatory-order-by-limits-the-use-of-min-doc-count/172984/1 "2019-03-19T13:50:13Z")

</div>

Hello,

I'd like to create a bar chart in Kibana, bucketing with a terms aggregation and then using as metrics the average of a field X, but I'd like to filter this only for the buckets with at least 100 items, i.e., I want the average of X, sorted by X for all the buckets with at least 100 items.

To achieve the filtering, I'm using JSON input field in the buckets configuration, adding the following:

`{"min_doc_count": 100}`

However, since the order by is a mandatory field in the buckets configuration, I ended up generating the following query:

`{ "aggs": { "2": { "terms": { "field": "city", "size": 5, "order": { "1": "desc" }, "min_doc_count": 100 }, "aggs": { "1": { "avg": { "field": "reviews.rating" } } } } },`

but like this, I believe that what ES is doing is first the sorting and then trimming the first 5 results by the min\_doc\_count, whereas what i'd like to have is to first trim the results by min\_doc\_count and then sort to get the first 5.

\*\* Example use case\*\*

Suppose that I have a dataset of item reviews, with a category field and a rating field. I would like to create a bar chart showing the top 10 categories in terms of average review rating, but I don't want a category with only a few reviews to show up first, so I'd want to filter for cities having at least 100 reviews.

Thanks  
Marco

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [March 20, 2019, 4:36pm UTC](https://discuss.elastic.co/t/kibana-barchart-mandatory-order-by-limits-the-use-of-min-doc-count/172984/2 "2019-03-20T16:36:05Z")

</div>

From my experiments, it looks like the min\_doc\_count is applying first before the sorting. Do you have an example that suggests otherwise?

---

<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:** [April 17, 2019, 4:36pm UTC](https://discuss.elastic.co/t/kibana-barchart-mandatory-order-by-limits-the-use-of-min-doc-count/172984/3 "2019-04-17T16:36:08Z")

</div>

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