# How to dynamically bucket low, medium, high on prices and pick 2 per bucker

**URL:** <https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256>\
**Category:** Elasticsearch\
**Created:** [August 29, 2019, 4:32am UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256 "2019-08-29T04:32:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gag\_Gag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gag_gag/32/47827_2.png) [@Gag\_Gag](https://discuss.elastic.co/u/Gag_Gag)\
**Post date:** [August 29, 2019, 4:32am UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/1 "2019-08-29T04:32:45Z")

</div>

I have docs containing a price field indexed as such:

> { price: 100 }  
> { price: 200 }  
> { price: 300 }

The prices can be anything from 100 to 1000.

Depending on a given filter, I want to bucket in low, med, high price points and pick 2 from each category for that search.

Is this possible?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 29, 2019, 7:36am UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/2 "2019-08-29T07:36:29Z")

</div>

you could play around with a range aggregation, inside of that a terms aggregation on the category field with a size of 2 and inside of that a top\_hits aggregation.

Another option is always to execute more than one search, but try the above first.

---

<div class="post-metadata">

**Author:** ![Gag\_Gag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gag_gag/32/47827_2.png) [@Gag\_Gag](https://discuss.elastic.co/u/Gag_Gag)\
**Post date:** [August 29, 2019, 11:50am UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/3 "2019-08-29T11:50:32Z")

</div>

In the range aggregation is there a way to dynamically calculate the to and from?

Basically if the filtered dataset has max price 400 and min price 100 then the range should be - low: 100 to 200 medium: 200 to 300, high: 300 to 400

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 29, 2019, 12:51pm UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/4 "2019-08-29T12:51:23Z")

</div>

you need to define the ranges yourself.

Another approach could be to get the max and min values first and then calculate a proper histogram window out of that.

---

<div class="post-metadata">

**Author:** ![Gag\_Gag](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gag_gag/32/47827_2.png) [@Gag\_Gag](https://discuss.elastic.co/u/Gag_Gag)\
**Post date:** [August 29, 2019, 12:52pm UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/5 "2019-08-29T12:52:51Z")

</div>

But that would be two queries right?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 29, 2019, 2:49pm UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/6 "2019-08-29T14:49:58Z")

</div>

yes, that would be two queries

---

<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:** [September 26, 2019, 2:52pm UTC](https://discuss.elastic.co/t/how-to-dynamically-bucket-low-medium-high-on-prices-and-pick-2-per-bucker/197256/7 "2019-09-26T14:52:43Z")

</div>

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