# Aggregations: filter by bucket values

**URL:** <https://discuss.elastic.co/t/aggregations-filter-by-bucket-values/16851>\
**Category:** Elasticsearch\
**Created:** [April 7, 2014, 4:56pm UTC](https://discuss.elastic.co/t/aggregations-filter-by-bucket-values/16851 "2014-04-07T16:56:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_4](https://avatars.discourse-cdn.com/v4/letter/d/6f9a4e/32.png) [@David\_4](https://discuss.elastic.co/u/David_4)\
**Post date:** [April 7, 2014, 4:56pm UTC](https://discuss.elastic.co/t/aggregations-filter-by-bucket-values/16851/1 "2014-04-07T16:56:47Z")

</div>

Hello,

I'm starting to use aggregations for some fairly advanced querying, and I'm  
finding it very simple so far. But there's one class of queries I'm  
struggling with.

Given this simplified data model:

Leads

- opened\_at
- closed\_at
- value ($)

I would like to show the number and value of open leads over time.

With a date\_histogram aggregation, I can see the number opened and closed  
over time. But what I'd like to know is not how many were _opened_ during  
a given bucket, but how many are _currently open_--that is, how many were  
opened during or before a given bucket and have not yet been closed, so  
that every bucket gives an accurate snapshot of open leads at a point in  
time.

Is there a way to figure this out using Elasticsearch? For example, can I  
access the start and end dates of a bucket in an aggregation, so that I can  
filter on e.g. range: { opened\_at: { lt: BUCKET\_START } } and range: {  
closed\_at: { gt: BUCKET\_END } }?

Thanks in advance for your help!

David

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/02c45103-7add-4b2a-bd38-7d973ab1c971%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/02c45103-7add-4b2a-bd38-7d973ab1c971%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 9, 2014, 4:54pm UTC](https://discuss.elastic.co/t/aggregations-filter-by-bucket-values/16851/2 "2014-04-09T16:54:06Z")

</div>

Unfortunately not at the moment. The bucket not only determines the date  
range but also all the documents that the metrics are computed on. The only  
thing I can think of is you'll probably need to define the individual  
ranges/filters yourself and build multiple filter buckets in your query  
perhaps?

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-aggregations-bucket-filter-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-aggregations-bucket-filter-aggregation.html)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8b47ae39-e29e-4e93-a0de-259faaf004e8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8b47ae39-e29e-4e93-a0de-259faaf004e8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:37am UTC](https://discuss.elastic.co/t/aggregations-filter-by-bucket-values/16851/3 "2017-07-06T01:37:11Z")

</div>


