title: How can I use Lucene to search for documents that do not contain a term?
body: <p>I am aware that the <a href="http://lucene.apache.org/java/3_4_0/queryparsersyntax.html" rel="nofollow noreferrer">Lucene documentation</a> says</p>
  
  <blockquote>
    <p>Note: The NOT operator cannot be used with just one term. For example,
    the following search will return no results:</p>
    
    <p>NOT "jakarta apache"</p>
  </blockquote>
  
  <p>However, I would like to be able to form a query that returns all documents NOT containing a term. I have looked into stringing together a <a href="http://lucene.apache.org/java/3_4_0/api/core/org/apache/lucene/search/MatchAllDocsQuery.html" rel="nofollow noreferrer">MatchAllDocsQuery</a> and a <a href="http://lucene.apache.org/java/3_4_0/api/core/org/apache/lucene/search/TermQuery.html" rel="nofollow noreferrer">TermQuery</a> into a <a href="http://lucene.apache.org/java/3_4_0/api/core/org/apache/lucene/search/BooleanQuery.html" rel="nofollow noreferrer">BooleanQuery</a>, but I cannot seem to find the right combination.</p>
  
  <p>If I index the following two documents</p>
  
  <pre><code>Doc0: content:The quick brown fox jumps over the lazy dog.
  Doc1: (empty string)
  </code></pre>
  
  <p>The query <code>*:* -content:fox</code> returns both documents when I just want one document.</p>
  
  <p>The RegexQuery <code>content:^((?!fox).)*$</code> suggested by <a href="https://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word">this StackOverflow answer</a> returns one document but it does not seem to be working correctly because <code>content:^((?!foo).)*$</code> returns one document as well when I expect it to return two documents.</p>
  
  <p>I am aware of the performance implications of what I want to do. The query will only be run on a few documents so I am not too worried about performance.</p>
  
  <p>Is there a way to write a Lucene query to get what I want?</p>