# Bucket Terms aggregation, top 20 values

**URL:** <https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031>\
**Category:** Kibana\
**Created:** [December 17, 2020, 7:31pm UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031 "2020-12-17T19:31:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![grra](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@grra](https://discuss.elastic.co/u/grra)\
**Post date:** [December 17, 2020, 7:31pm UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/1 "2020-12-17T19:31:30Z")

</div>

Hi,  
on my Buckets I have the Terms aggregation, with a field of the type `number`.  
I need the 20 biggest values of that field - in ascending order.

- Is it possible to order the Terms `ascending` and then take the last 20 values?
- Or can order all entries of that field in `descending` order, to take the first 20 values, and order them again in `ascending` order?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [December 17, 2020, 7:55pm UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/2 "2020-12-17T19:55:05Z")

</div>

You can get the 20 biggest values of a number by sorting the Terms aggregation in Descending Alphabetical order, but not in ascending order- that will get you the 20 smallest values because it's doing sorting inside Elasticsearch. It is possible to use column sorting in the Data Table visualization, which takes the results from Elasticsearch and re-sorts them.

---

<div class="post-metadata">

**Author:** ![grra](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@grra](https://discuss.elastic.co/u/grra)\
**Post date:** [December 17, 2020, 8:26pm UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/3 "2020-12-17T20:26:29Z")

</div>

Does that mean, there is no chance to get that result in a line graph?

I would need the 20 highest values in ascending order.

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [December 17, 2020, 8:44pm UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/4 "2020-12-17T20:44:09Z")

</div>

That's right, the order of the data is what controls the order of the visualization for other chart types. Yesterday I opened an issue to track this as a feature request for Lens, which would also solve your use case. It also includes links to past requests: [https://github.com/elastic/kibana/issues/86184](https://github.com/elastic/kibana/issues/86184)

---

<div class="post-metadata">

**Author:** ![grra](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@grra](https://discuss.elastic.co/u/grra)\
**Post date:** [December 18, 2020, 8:54am UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/5 "2020-12-18T08:54:26Z")

</div>

Thanks for reply,  
are there the same limitations, if I create a Graph vom custom query?  
Or is that kind of creation even possible?

---

<div class="post-metadata">

**Author:** ![grra](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@grra](https://discuss.elastic.co/u/grra)\
**Post date:** [December 18, 2020, 9:53am UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/6 "2020-12-18T09:53:22Z")

</div>

Is it possible to invert that topic?  
To display the latest value in a `Bucket` but not in a `Metric`?  
[display latest value in a metric](https://discuss.elastic.co/t/display-latest-value-in-a-metric/114930)

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [December 18, 2020, 4:35pm UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/7 "2020-12-18T16:35:48Z")

</div>

We do offer a fully custom visualization builder, you can follow my [Vega tutorial](https://www.elastic.co/guide/en/kibana/current/vega-lite-tutorial-create-your-first-visualizations.html) to learn about how it works.

I don't think I understand your second question about "latest value in a bucket". You would need to be more specific about what your input data is and what you want the output data to be. I suggest that you open a new thread to explain your question more.

---

<div class="post-metadata">

**Author:** ![grra](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@grra](https://discuss.elastic.co/u/grra)\
**Post date:** [December 24, 2020, 11:09am UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/8 "2020-12-24T11:09:47Z")

</div>

Thanks, I opened new thread of my use case. [display-latest-value-in-a-bucket](https://discuss.elastic.co/t/display-latest-value-in-a-bucket/259577)

---

<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:** [January 21, 2021, 11:09am UTC](https://discuss.elastic.co/t/bucket-terms-aggregation-top-20-values/259031/9 "2021-01-21T11:09:52Z")

</div>

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