title: Elasticsearch: Conditionally filter query on fields if they exist in multi-index query
body: <p>I have a query for a general search which spans multiple indices. Some of the indices have a field called <code>is_published</code> and some have a field called <code>date_review</code>, some have both.</p>
  
  <p>I'm struggling to write a query which will search across fields and filter on the fields mentioned above <em>but only if they exist</em>. I have managed to achieve what I want on the individual fields using <code>missing</code> and/or <code>exists</code>, but it excludes the other variants.</p>
  
  <p>In english, I want to keep documents in the result where:</p>
  
  <ol>
  <li><code>is_published</code> is true OR the field does not exist</li>
  <li><code>date_review</code> is in the future OR the field does not exist</li>
  </ol>
  
  <p>So, if a document has <code>is_published</code> and it's false, remove it. If a document has <code>date_review</code> in the past, remove it. If it has <code>is_published == false</code> and <code>date_review</code> is in the future, remove it.</p>
  
  <p>I hope this makes sense?</p>
  
  <p>For the purpose of answering, assume the documents might look like this:</p>
  
  <pre><code>//  Has `is_published` flag
  {
      "label": "My document",
      "body": "Lorem ipsum doler et sum.",
      "is_published": true
  }
  
  //  Has `date_review` flag
  {
      "label": "My document",
      "body": "Lorem ipsum doler et sum.",
      "date_review": "2017-01-01"
  }
  
  
  //  Has both `is_published` and `date_review` flags
  {
      "label": "My document",
      "body": "Lorem ipsum doler et sum.",
      "is_published": true
      "date_review": "2017-01-01"
  }
  </code></pre>
  
  <p>At the moment, my [unfiltered] query looks like this:</p>
  
  <pre><code>{
    "index": "index-1,index-2,index-3",
    "type": "item",
    "body": {
      "query": {
        "filtered": {
          "query": {
            "multi_match": {
              "query": "my serach phrase",
              "type": "phrase_prefix",
              "fuzziness": null,
              "fields": [
                "label^3",
                "body",
              ]
            }
          },
          "filter": []
        }
      }
    }
  }
  </code></pre>
  
  <p>Very grateful for any pointers.</p>
  
  <p>Thanks.</p>