# Nested multiple date-range intervals

**URL:** <https://discuss.elastic.co/t/nested-multiple-date-range-intervals/301121>\
**Category:** Elasticsearch\
**Created:** [March 30, 2022, 4:12pm UTC](https://discuss.elastic.co/t/nested-multiple-date-range-intervals/301121 "2022-03-30T16:12:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGreatBaldOne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thegreatbaldone/32/93645_2.png) [@TheGreatBaldOne](https://discuss.elastic.co/u/TheGreatBaldOne)\
**Post date:** [March 30, 2022, 4:12pm UTC](https://discuss.elastic.co/t/nested-multiple-date-range-intervals/301121/1 "2022-03-30T16:12:43Z")

</div>

I have an index, that will hold a 'job' and each job will have a couple of date-intervals where that job can be performed. In our case it a plumbing job 🙂

These date-intervals are limited, so they do not expand until forever. At most 5 date-intervals per job.

```auto
PUT /testnesteddateintervals/
 {
 	"mappings":{
 		"properties": {
 			"JobName": {
 				"type": "text"
 			},
 			"dintervals": {
 				"type": "nested",
 				"properties":{					
 					"time_frame": {
 						"type": "date_range", 
 						"format": "yyyy-MM-dd"
 					}
 				} 				
 			}
 		}
 	}
 }

```

```auto
PUT /testnesteddateintervals/_doc/1
{
	"JobName": "PlumbingWork",
	"dintervals": [
		{
			 "gte" : "2022-03-10", 
    	                 "lte" : "2022-03-30"
		},
		{
			 "gte" : "2022-04-10", 
    	                  "lte" : "2022-04-30"
		}
	]	
}

```

How can I take a bunch of 'intervals' where the 'plumber' has time, and get if he can fullfil the required dintervals.

For example suppose the plumbers schedule is like this.

```auto

{
	"PlumberName": "Plumber Joe",
	"availableIntervals": [
		{
			 "gte" : "2022-03-12", 
    	 "lte" : "2022-03-14"
		},
		{
			 "gte" : "2022-02-10", 
    	 "lte" : "2022-03-11"
		}
	]	
}

```

How can I do a query to return a true or false if the schedule is fullfiled.

---

<div class="post-metadata">

**Author:** ![TheGreatBaldOne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thegreatbaldone/32/93645_2.png) [@TheGreatBaldOne](https://discuss.elastic.co/u/TheGreatBaldOne)\
**Post date:** [March 30, 2022, 5:55pm UTC](https://discuss.elastic.co/t/nested-multiple-date-range-intervals/301121/2 "2022-03-30T17:55:19Z")

</div>

This is the solution i Found, multiple bools in a should.

`Get /testnesteddateintervals/_search`

```auto
{
   "query":{
      "bool":{
         "must":[
            {
               "nested":{
                  "path":"dintervals",
                  "query":{
                     "bool":{
                        "should":[
                           {
                              "bool":{
                                 "must":[
                                    {
                                       "range":{
                                          "dintervals.gte":{
                                             "lte":"2022-03-31"
                                          }
                                       }
                                    },
                                    {
                                       "range":{
                                          "dintervals.lte":{
                                             "gte":"2022-03-31"
                                          }
                                       }
                                    }
                                 ]
                              }
                           },
													 {
                              "bool":{
                                 "must":[
                                    {
                                       "range":{
                                          "dintervals.gte":{
                                             "lte":"2022-03-30"
                                          }
                                       }
                                    },
                                    {
                                       "range":{
                                          "dintervals.lte":{
                                             "gte":"2022-03-30"
                                          }
                                       }
                                    }
                                 ]
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

```

---

<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:** [March 31, 2022, 11:57am UTC](https://discuss.elastic.co/t/nested-multiple-date-range-intervals/301121/3 "2022-03-31T11:57:02Z")

</div>

Take a look at the [range field type documentation](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/range.html) - the tiny, but important point is the `relation` parameter to find a hole or an overlapping time to book an appointment.

Hope that helps!

---

<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:** [April 28, 2022, 11:57am UTC](https://discuss.elastic.co/t/nested-multiple-date-range-intervals/301121/4 "2022-04-28T11:57:12Z")

</div>

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