title: How to return all documents where a field does not exist, but if it exists return documents with value "tag1"
body: <p>I am facing a problem in building an elasticsearch query in Java.</p>
  <p>I want to search all the documents where either a field X is not present OR if it is present, return all those documents where the value inside field X is &quot;tag1&quot;</p>
  <p>Currently, I have written this query in Java:</p>
  <pre><code>BoolQueryBuilder boolQueryBuilder = boolQuery();`
  boolQueryBuilder.mustNot(QueryBuilders.existsQuery(&quot;X&quot;));`
  boolQueryBuilder.filter(termsQuery(&quot;X&quot;, &quot;tag1&quot;));`
  </code></pre>
  <p>The SQL query for this would be something like:</p>
  <pre><code>select * from table_name where X is null or x = 'tag1';
  </code></pre>
  <p>But doing this gives me empty search results. However, if I search each of these queries individually, it works fine.</p>