title: Elasticsearch: find document doesn't have intersecting nested items
body: <p>I have a document with nested collection and the goal is to find that ones which don't have any inner items intersecting particular period considering also an item's status.</p>
  <p>inb4 I've found at least two SO items that didn't help me (probably I'm noob):<br />
  <a href="https://stackoverflow.com/questions/54714760/elasticsearch-find-all-documents-whose-nested-documents-do-not-intersect-with">ElasticSearch - find all documents whose nested documents do not intersect with date range</a> <br />
  and <br />
  <a href="https://stackoverflow.com/questions/37622781/elasticsearch-inverse-range-overlap-query">Elasticsearch inverse range overlap query</a></p>
  <p>So, my document, let's say, simple (I'll paste a JSON definition, just for simplicity. all mappings are fine, trust me ;) ):</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;maintenances&quot;: [ // &lt;-- this is nested collection
    {
      &quot;start&quot;: &quot;date/time&quot;,
      &quot;end&quot;: &quot;date/time&quot;,
      &quot;status&quot;: boolean
    } ]
  }
  </code></pre>
  <p>What I need is to write a query for documents that don't have any active (<code>status = true</code>) maintenance items intersecting some period (<code>from</code> and <code>to</code> for instance).</p>
  <p>I started from simple expression:</p>
  <pre class="lang-json prettyprint-override"><code>must_not { nested { exists { field: maintenances } } }
  or
  must_not { nested { must [
    { maintenances.start &lt;= to },
    { maintenances.end &gt;= from },
    { status = true }
  ] } }
  </code></pre>
  <p>That returned me any document from test entries.<br />
  Remembering that nested query will return outer document in case if <strong>any</strong> document will match expression I decided to make a query complex, something like:</p>
  <pre class="lang-json prettyprint-override"><code>maintenances not exists
  or
  (
    any maintenance within range is not active
    and
    any maintenance outside range is active
  )
  </code></pre>
  <p>But it became clear (not very fast unfortunately) that this query doesn't work for sorts of edge-cases: like all nested maintenance items are inactive, or all maintenance are outside requested bounds.</p>
  <p>Currently I'm not sure, but I assume that query should contain as many <strong>or</strong>-d items as many edge-cases are? Like:</p>
  <pre class="lang-json prettyprint-override"><code>maintenances not exists
  or
  (
    any maintenance within range is not active
    and
    any maintenance outside range
  )
  or
  (
    any maintenance within range is not active
    and
    no maintenance outside range
  )
  or
  (
    no maintenance within range
    and
    any maintenance outside range
  )
  or
  OVER9000 of them
  </code></pre>
  <p>Does anyone know simplest way to query Elastic for my case?</p>