Skip to content

Support dynamic interval and fixed buckets for histogram aggregation #9572

Description

@rayward

This is basically the opposite of how the histogram currently acts: fixed interval and dynamic number of buckets.

My use case is that I would like to build a price range style aggregation for filtering products:

$0 - $100
$101 - $200
$201 - $300
$301 - $400
$401 - $500

While you can do this with the range agg. if you know in advance the lower and upper bounds of all your prices, I can't see a way to do this dynamically based off the current query. The range agg also suffers from other shortcomings such as generating buckets that may contain no documents.

Ideally, I'd like to be able to specify the number of ranges / buckets I want (eg. 5) with a dynamic interval calculated by the aggregation.

min_doc_count could also come into affect here to ensure that either we don't generate buckets that don't contain any products (eg min_doc_count: 0) and in which case the number of buckets just becomes a maximum number of potential buckets, rather than a guaranteed amount.

Activity

  1. alexksikes commented on Feb 5, 2015

    @alexksikes
    Contributor

    It would indeed be a neat feature to dynamically guess the ranges. However, it seems difficult to do in a single request in a distributed environment, as we don't know the data on other shards. So for now as a workaround I would do two requests of the same query. The first one to get the ranges and the second one to perform the aggregation on those ranges.

    For more discussion on a similar feature request see: #9531

  2. rayward commented on Feb 5, 2015

    @rayward
    Author

    Yeh I was really hoping to avoid separate queries as performance is fairly critical for us.

  3. alexksikes commented on Feb 6, 2015

    @alexksikes
    Contributor

    Yes but the second query should execute much faster thanks to the file system cache, not counting also any filter in the query that may also be cached.

  4. sultaniman commented on Feb 10, 2015

    @sultaniman

    Hi,
    I am relatively new to Elasticsearch but I found interesting thing about date_histogram. We have aggregation query with interval=week.

    {
      "aggs": {
        "scores_by_date": {
          "date_histogram": {
            "field": "date",
            "format": "MM-dd-yyyy",
            "interval": "week"
          }
        }
      }
    }

    If we use filter with date ranges as following starting from the Jan 1 YEAR

    {
      "range": {
        "document.date":  {
          "from": "2015-01-01",
          "to": "2015-02-10"
         }
      }
    }

    In the result buckets we get extra week from past year's last month Dec NDAY YEAR

    {
      "aggregations": {
        "scores_by_date": {
          "buckets": [
            {
      HERE => "key_as_string": "12-29-2014",
              "key": 1419811200000,
              "doc_count": 29
            },
            {
              "key_as_string": "01-05-2015",
              "key": 1420416000000,
              "doc_count": 39
            },
            {
              "key_as_string": "01-12-2015",
              "key": 1421020800000,
              "doc_count": 45
            },
            {
              "key_as_string": "01-19-2015",
              "key": 1421625600000,
              "doc_count": 55
            },
            {
              "key_as_string": "01-26-2015",
              "key": 1422230400000,
              "doc_count": 29
            }
          ]
        }
      }
    }

    Is this a standard behaviour of Elasticsearch date histogram aggregations or is this issue the part of current discussion?

  5. rjernst commented on Feb 10, 2015

    @rjernst
    Member

    @imanhodjaev I believe that is because it the date math code includes the entire week, instead of a partial week. It does not truncate the edges of the range.

  6. OPtoss commented on Apr 23, 2015

    @OPtoss

    I'd love to vote for this issue. It's severely prohibiting me from attaining my use case of date_histograms. I'm doing date_histogram as a sub-aggregation to a terms aggregation, so I'm getting one histogram per term bucket. If I can't define the interval dynamically per-bucket, then I have to do hundreds of queries instead of just 1.

    At the moment, I don't care much about cost to server-side computations. The client can't be expected to perform hundreds of separate queries in my real-time interactive application. So having this feature is critical.

    I've attempted to work around it using groovy scripts to calculate the interval, which works, but once again I'm blocked, as I can't refer to the result of my script inside the "interval" argument of the date_histogram.

    Is there another workaround that doesn't multiply queries? Or can this feature be reconsidered? Thanks!

  7. ppf2 commented on Jun 25, 2015

    @ppf2
    Contributor

    +1 on the original use case of dynamic range histogram buckets for numeric (non-datetime fields). This is a pretty common use case for commerce applications.

  8. jpountz commented on Jul 31, 2015

    @jpountz
    Contributor

    Stalled by #12316. It is still possible to implement this feature by doing two requests as described above.

  9. gmoskovicz commented on Aug 25, 2016

    @gmoskovicz
    Contributor

    +1 for a workaround without using a 2 step request to elasticsearch. Auto-Calculating the ranges looks like a great idea, although i can see that implementing this can be tricky given the number of things that we should consider.

  10. Bargs commented on Sep 21, 2016

    @Bargs

    Copying my comments from a duplicate issue:

    This would be a boon for Kibana because it would allow us to easily auto scale the histogram visualization and protect users from accidentally creating too many buckets, crashing their browser in the process.

    We could accomplish this by issuing two requests from the client, the first grabbing the min/max values for the given field. But since the aggregation may have a search query and/or filters associated with it we don't know how performant this would be. I imagine it would be more efficient to implement this option inside elasticsearch so the calculations can be done in a single request.

  11. 7 remaining items

  12. added a commit that references this issue on Jul 16, 2018
    e6b3b08
  13. melissachang commented on Jul 16, 2018

    @melissachang

    I believe this issue should still be open. #28993 only supports date histograms. There needs to be a similar PR for non-date, regular histograms.

  14. pcsanwald commented on Jul 17, 2018

    @pcsanwald
    Contributor

    @melissachang you are absolutely right that we need to support numeric histograms, but I've created a separate issue for that, #31828. We can track that work using this new issue. Thank you!

  15. monfera commented on Sep 14, 2018

    @monfera

    Just for sake of completeness here's a link for Sturges' Rule, which is a sound approach to determining bin counts merely based on sample count, assuming a distribution reasonably close to normal distribution.

  16. Rapster commented on Apr 26, 2019

    @Rapster

    @monfera did you figure out a way to use Sturges' rule to determine the number of buckets? Someone else ask this question few years ago also https://discuss.elastic.co/t/histogram-and-sturges-formula/14133

  17. findinpath commented on Sep 21, 2020

    @findinpath

    @alexksikes Can you point me to an example of the two-query approach you are suggesting?

    I am not sure if I got it right, but here https://github.com/findinpath/elastic-price-range-aggregation/blob/master/src/test/kotlin/com/findinpath/PriceRangeAggregationTest.kt#L119 you can find an implementation of the two-query approach suggested.

  18. mjose007 commented on Oct 8, 2020

    @mjose007

    +1 for the Support dynamic interval and fixed buckets for histogram aggregation

  19. KPGDL commented on Feb 2, 2022

    @KPGDL

    Wow, it's already 6 years and still waiting...

  20. tmushayahama commented on Apr 15, 2022

    @tmushayahama

    So there is no dynamic interval yet>

  21. shu-bham commented on Apr 30, 2022

    @shu-bham

    still waiting for this feature

  22. PendalF89 commented on Jan 27, 2023

    @PendalF89

    Welcome from 2023! So, we are still waiting :)

  23. AdamRohdell commented on Sep 24, 2025

    @AdamRohdell

    If anyone is still looking for a solution, this solved my use case at least:

    https://www.elastic.co/guide/en/elasticsearch/reference/8.4/search-aggregations-bucket-variablewidthhistogram-aggregation.html

    Looks like they implemented it a while ago but it was just not mentioned in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions