# Date Interval Pattern

**URL:** <https://discuss.elastic.co/t/date-interval-pattern/10076>\
**Category:** Elasticsearch\
**Created:** [December 14, 2012, 8:48pm UTC](https://discuss.elastic.co/t/date-interval-pattern/10076 "2012-12-14T20:48:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gus\_Holcomb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gus_holcomb/32/2579_2.png) [@Gus\_Holcomb](https://discuss.elastic.co/u/Gus_Holcomb)\
**Post date:** [December 14, 2012, 8:48pm UTC](https://discuss.elastic.co/t/date-interval-pattern/10076/1 "2012-12-14T20:48:29Z")

</div>

Hello,  
I'm new to elastic search and I'm having a good time doing some  
benchmarking. However, It's not clear to me what the ideal pattern would be  
for storing and querying date intervals. Most of the example date code  
involves storing an instant and querying with a date range. I would like to  
invert that.

Ideally I would store an array of date intervals like:

1. 2012-12-01T00:00:00 - 2012-12-10T00:00:00
2. 2012-12-12T00:00:00 - 2012-12-15T00:00:00

On a single object and then query for when an interval is contained in or  
overlaps with any of the stored date intervals.

I can see how I could create an index of an interval object with start and  
end mappings and make an array of those but querying would be a VERY  
verbose set of criteria.

Does anyone have any recommendations or is that the best I'm going to do?

Thanks,  
Gus

--

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [December 15, 2012, 7:22am UTC](https://discuss.elastic.co/t/date-interval-pattern/10076/2 "2012-12-15T07:22:44Z")

</div>

Hi,

I think you might want to look at nested type (and nested filter or nested  
query as well).

Regards,  
Lukas

On Fri, Dec 14, 2012 at 9:48 PM, Gus Holcomb [gus@relayrides.com](mailto:gus@relayrides.com) wrote:

> Hello,  
> I'm new to Elasticsearch and I'm having a good time doing some  
> benchmarking. However, It's not clear to me what the ideal pattern would be  
> for storing and querying date intervals. Most of the example date code  
> involves storing an instant and querying with a date range. I would like to  
> invert that.
> 
> Ideally I would store an array of date intervals like:
> 
> 1. 2012-12-01T00:00:00 - 2012-12-10T00:00:00
> 2. 2012-12-12T00:00:00 - 2012-12-15T00:00:00
> 
> On a single object and then query for when an interval is contained in or  
> overlaps with any of the stored date intervals.
> 
> I can see how I could create an index of an interval object with start and  
> end mappings and make an array of those but querying would be a VERY  
> verbose set of criteria.
> 
> Does anyone have any recommendations or is that the best I'm going to do?
> 
> Thanks,  
> Gus
> 
> --

--

---

<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, 2:59am UTC](https://discuss.elastic.co/t/date-interval-pattern/10076/3 "2017-07-06T02:59:47Z")

</div>


