# DateRange Aggregation w/ a Script that produces the Dates instead of a static date field

**URL:** <https://discuss.elastic.co/t/daterange-aggregation-w-a-script-that-produces-the-dates-instead-of-a-static-date-field/193145>\
**Category:** Elasticsearch\
**Created:** [July 31, 2019, 2:40pm UTC](https://discuss.elastic.co/t/daterange-aggregation-w-a-script-that-produces-the-dates-instead-of-a-static-date-field/193145 "2019-07-31T14:40:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Doman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_doman/32/11409_2.png) [@Thomas\_Doman](https://discuss.elastic.co/u/Thomas_Doman)\
**Post date:** [July 31, 2019, 2:40pm UTC](https://discuss.elastic.co/t/daterange-aggregation-w-a-script-that-produces-the-dates-instead-of-a-static-date-field/193145/1 "2019-07-31T14:40:59Z")

</div>

ES: 6.1.1  
NEST: 6.1.0

Is it possible to create a DateRange Aggregation where, instead of specifying a `field`, I can specify a script which will produce a Date from a number of source fields? In my case, there's a "good, better, best" set of dates in 3 different fields (all optional expect the "good") in my documents that I'd like to choose from dynamically as each hit is produced. That is, if the "best" is populated, I'll return that one and so on.

My web searches so far haven't produced any results so I'm doubtful it's possible but thought I'd raise the use case and see.

---

<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 1, 2019, 12:29pm UTC](https://discuss.elastic.co/t/daterange-aggregation-w-a-script-that-produces-the-dates-instead-of-a-static-date-field/193145/2 "2019-08-01T12:29:58Z")

</div>

Hey,

how about using a script in a [terms](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-aggregations-bucket-terms-aggregation.html) for this, as this is basically just a question how to name the buckets then.

--Alex

---

<div class="post-metadata">

**Author:** ![Thomas\_Doman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_doman/32/11409_2.png) [@Thomas\_Doman](https://discuss.elastic.co/u/Thomas_Doman)\
**Post date:** [August 2, 2019, 9:52pm UTC](https://discuss.elastic.co/t/daterange-aggregation-w-a-script-that-produces-the-dates-instead-of-a-static-date-field/193145/3 "2019-08-02T21:52:58Z")

</div>

Hey Alex,

Thanks for the suggestion. I actually ended up using a regular `range` aggregation where I **can** specify a `script` and my `params` are in terms of milliseconds as well as the script calculations which use my "good, better, best" date fields in milliseconds. With this I can achieve the same effect w/o any date parsing, etc. and w/o `date_range` supporting scripts.

I also created a companion script for the resulting `range` filters that we implement based on the aggregations we produce.

---

<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:** [August 30, 2019, 9:52pm UTC](https://discuss.elastic.co/t/daterange-aggregation-w-a-script-that-produces-the-dates-instead-of-a-static-date-field/193145/4 "2019-08-30T21:52:59Z")

</div>

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