# Pure negation query in lucene

In many information-retrieval system, you can use queries like “term1 <span class="caps">AND</span> (<span class="caps">NOT</span> term2)” but you cannot use queries like “<span class="caps">NOT</span> term2” on their own (e.g. to get only documents that do not contain term2). At least the system returns no result even if some documents do not contain term2. This is because:

- Most of these system uses inverted indices, which are essentially “term =&gt; document list” mappings. There is no way to reconstruct a list of documents for “<span class="caps">NOT</span> term2” from such a mapping.
- The system can generate a set of all document and then subtract those that contains term2 from the set. This can be resource-intensive (e.g. needs O(doc count) storage and processing time) and can be misused by users.

Despite the difficulties, users of some system might need this kind of query. In Lucene, there are two ways to support it:

- When indexing, create a field (e.g. “exists”) with a constant value across documents (e.g. “true”). Then rewrite the query “<span class="caps">NOT</span> term2” into “+exists:true -term2”.
- Combine [MatchAllDocsQuery](http://lucene.apache.org/java/2_3_2/api/org/apache/lucene/search/MatchAllDocsQuery.html), which matches all document, with BooleanQuery. The code will look like this:  
    ```
    <br></br>// Create a query that matches something like “everything <span class="caps">AND</span> <span class="caps">NOT</span> term2”<br></br>MatchAllDocsQuery everyDocClause = new MatchAllDocsQuery();<br></br>TermQuery termClause = new TermQuery(new Term(“text”, “term2”));<br></br>BooleanQuery query = new BooleanQuery();<br></br>query.add(everyDocClause, BooleanClause.Occur.<span class="caps">MUST</span>);<br></br>query.add(termClause, BooleanClause.Occur.MUST_NOT);<br></br>…<br></br>IndexSearcher searcher = new IndexSearcher(“/path/to/index”);<br></br>Hits hits = searcher.search(query);<br></br>// hits contains only documents that do not contain term2<br></br>
    ```