title: Elasticsearch: Match range in list of start and end dates
body: <p>What I want to achieve is to only return results if the dates provided in the query are in range of 1 single period. Right now I have a query that partially works, but it queries over all periods in the availability list. It matches because there exists a start date somewhere lower than or equal to <code>2020-12-25T00:00:00</code> and there exists an end date somewhere greater than or equal to <code>2020-12-22T00:00:00</code>.</p>
  <p>What I'd like is query that doesn't match the document provided below, since neither periods match the query I enter.</p>
  <p>This is a part of my data model:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;units&quot;: [
          {
              &quot;availability&quot;: [
                  {
                      &quot;period&quot;: [
                          {
                              &quot;start&quot;: &quot;2020-09-12T00:00:00&quot;,
                              &quot;end&quot;: &quot;2020-10-31T00:00:00&quot;
                          }
                      ]
                  },
                  {
                      &quot;period&quot;: [
                          {
                              &quot;start&quot;: &quot;2021-04-03T00:00:00&quot;,
                              &quot;end&quot;: &quot;2021-04-24T00:00:00&quot;
                          }
                      ]
                  }
              ]
          }
      ]
  }
  </code></pre>
  <p>This is my Elasticsearch query right now:</p>
  <pre class="lang-json prettyprint-override"><code>{ 
      &quot;query&quot;: {
          &quot;bool&quot;: {
              &quot;must&quot;: [
                  {
                      &quot;range&quot;: {
                          &quot;units.availability.period.start&quot;: {
                              &quot;lte&quot;: &quot;2020-12-25T00:00:00&quot;
                          }
                      }
                  },
                  {
                      &quot;range&quot;: {
                          &quot;units.availability.period.end&quot;: {
                              &quot;gte&quot;: &quot;2020-12-22T00:00:00&quot;
                          }
                      }
                  }
              ]
          }
      }
  }
  </code></pre>
  <p>Any suggestions would be of great help!</p>