# How to store disjoint date ranges and then search for a specific range

**URL:** https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935
**Category:** Elasticsearch
**Created:** [June 16, 2016, 2:27am UTC](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935 "2016-06-16T02:27:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [June 16, 2016, 2:27am UTC](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935/1 "2016-06-16T02:27:18Z")

</div>

I am trying to store hotel room availability in elasticsearch. And then I need to  
search rooms those are available from a date till another date. I have come up with  
two ways to store data for availability, and they are as follows:

Here availability dictionary store all dates and value of each date key is true of false, representing its available on  
that day or not.

```
{
  "_id": "khg2uo47tyhgjwebu7624787",
  "room_type": "garden view",
  "hotel_name": "Cool hotel",
  "hotel_id": "jytu64r982u0299023",
  "room_metadata1": 233,
  "room_color": "black",
  "availability": {
    "2016-07-01": true,
    "2016-07-02": true,
    "2016-07-03": false,
    "2016-07-04": true,
    "2016-07-05": true,
    "2016-07-06": null,
    "2016-07-07": true,
    "2016-07-08": true,
    ----
    ----
    for 365 days 
  }

}

```

Here availability array only stores those dates when room is available

```
{
  "_id": "khg2uo47tyhgjwebu7624787",
  "room_type": "garden view",
  "hotel_name": "Cool hotel",
  "hotel_id": "jytu64r982u0299023",
  "room_metadata1": 535,
  "room_color": "black",
  "availability": ["2016-07-01", "2016-07-02", "2016-07-04", "2016-07-05", "2016-07-07", "2016-07-08"] ---for 365 days
  }
}

```

I want to search all rooms, those are available from `from_date` till `to_date` and that should look into `availability` dictionary or array. And my date range may span up to 365 days (e.g. 2016-01-01 to 2016-12-31)

Question 1) How to store these availability data , so that I can perform the above search easily?  
Question 2) I could not find any way to search through range of dates, so any suggestion?

Please note, items  
in `availability` may not be kept sorted. And I may have more than 100 million records to search through.

---

<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: [June 16, 2016, 8:28am UTC](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935/2 "2016-06-16T08:28:37Z")

</div>

Hey,

wouldnt you be able to search for available dates by just making sure that the data is set with a query like this

```json
GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "availability": "2016-07-01" } },
        { "term": { "availability": "2016-07-02" } }
      ]
    }
  }
}

```

Instead of a range query, just add each required day manually?

--Alex

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [June 16, 2016, 4:42pm UTC](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935/3 "2016-06-16T16:42:12Z")

</div>

Thanks @spinscale for the idea. But here is the caveat. If I have to search for a room available from June 1st till Dec 31. That means I will have to manually pass 6x30=180 days. Is that scalable? I doubt. ...what is your opinion?

---

<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: [June 17, 2016, 7:04am UTC](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935/4 "2016-06-17T07:04:21Z")

</div>

If you already have the data, why not give it a try and see if it matches the SLA you set for yourself?

If it does not match your SLA, you could add some client code, that indexes a full month availability as `2016-07` and search for that term for a full month?

---

<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 5, 2017, 10:42pm UTC](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935/5 "2017-07-05T22:42:56Z")

</div>


