# Date range aggregations over different fields

**URL:** https://discuss.elastic.co/t/date-range-aggregations-over-different-fields/20518
**Category:** Elasticsearch
**Created:** [October 31, 2014, 2:34pm UTC](https://discuss.elastic.co/t/date-range-aggregations-over-different-fields/20518 "2014-10-31T14:34:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Michael\_Gallego](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_gallego/32/933_2.png) [@Michael\_Gallego](https://discuss.elastic.co/u/Michael_Gallego)
#### Post date: [October 31, 2014, 2:34pm UTC](https://discuss.elastic.co/t/date-range-aggregations-over-different-fields/20518/1 "2014-10-31T14:34:52Z")

</div>

Hi,

After my first topic here  
[https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/\_whlH9BWXAQ](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/_whlH9BWXAQ),  
I've decided to rework my data structure. I have now documents that looks  
like this:

{  
"element": "A",  
"date": "2014-01-01",  
"valid\_until": "2014-02-01"  
},  
{  
"element": "A",  
"date": "2014-02-01",  
"valid\_until": "9999-12-31"  
}

The date "9999-12-31" is here to say: "it has not yet expired". There is  
always range like this, so for a given element "A", date \> valid\_until can  
never overlaps. I can therefore count how much element I have by using the  
pseudo-code like this: COUNT elements WHERE date \< date\_to\_count AND  
valid\_until \>= date\_to\_count

Where "date\_to\_count" is the date at which I want to count the values for.  
As I want to calculate this at several points in time, I could either use a  
date histogram, or a date range aggregation. However, the date range does  
seem to work only with one kind of field. Ideally, I'd like to be able to  
do that:

"aggs": {  
"foo": {  
"date\_range": {  
"fields": ["date", "valid\_until"],  
"ranges": [  
{"from": "2014-01-01", "to": {"2014-02-01"}},  
{"from": "2014-02-01", "to": {"2014-03-01"}},  
{"from": "2014-03-01", "to": {"2014-04-01"}}  
]  
}  
}  
}

Where the "date" will be used for "from", and the "valid\_until" would be  
used for "to".

I've tried several other ideas with script, but can't find an efficient way  
to do it this way :/.

Thanks!

--  
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/03f84120-3017-4f90-9c58-018edb49dc73%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/03f84120-3017-4f90-9c58-018edb49dc73%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Michael\_Gallego](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_gallego/32/933_2.png) [@Michael\_Gallego](https://discuss.elastic.co/u/Michael_Gallego)
#### Post date: [November 4, 2014, 10:13am UTC](https://discuss.elastic.co/t/date-range-aggregations-over-different-fields/20518/2 "2014-11-04T10:13:27Z")

</div>

Anyone would have a hint on this? 🙂

--  
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/9cbd8a1a-5f3a-4858-bb11-0b27f8f7fd22%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9cbd8a1a-5f3a-4858-bb11-0b27f8f7fd22%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, 12:52am UTC](https://discuss.elastic.co/t/date-range-aggregations-over-different-fields/20518/3 "2017-07-06T00:52:19Z")

</div>


