lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Frank Geary <>
Subject Re: Searching for null (empty) fields, how to use -field:[* TO *]
Date Wed, 29 Sep 2010 18:14:56 GMT

Since my index cannot be re-indexed easily, I had to go with Erick's first
suggestion.  I thought others might be interested in an example of the code
(I did this using Lucene 2.4.1):

// This code worked best to deletes documents with a null field value...
BooleanQuery nullFieldsOnlyQuery = new BooleanQuery();

MatchAllDocsQuery matchAllDocsQuery = new MatchAllDocsQuery();

// ConstantScoreRangeQuery does not throw a BooleanQuery.TooManyClauses
// A regular RangeQuery does throws a BooleanQuery.TooManyClauses exception.
// Obviously the range may be different depending on the nature of the field
ConstantScoreRangeQuery nonNullFieldsRangeQuery = new
ConstantScoreRangeQuery( theFieldName,
"0", //zero
true,							                                                true);

// Load up the BooleanQuery
nullFieldsOnlyQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.MUST
nullFieldsOnlyQuery.add( nonNullFieldsRangeQuery,
BooleanClause.Occur.MUST_NOT );

// Delete the documents from the archive index
indexWriter.deleteDocuments( nullFieldsOnlyQuery );
// End of code that worked best to delete documents with a null field value.

// The following code did NOT always work to delete documents with a null
field value.
// I did get the code below to delete documents with a null field value in
my Windows XP
// environment when I simply added a document with "" for the field value. 
However, it did 
// not work in my CentOS environment.  I imagine this failure had more to do
with the way
// those documents with a null field value ended up erroneously being
indexed in CentOS 
// rather than with the difference in the OS itself.
Term[] fieldTerms = new Term[ 1 ];
fieldTerms[0] = new Term( theFieldName, "" );
indexWriter.deleteDocuments( fieldTerms );
// End of code that did NOT always work for me.

Frank Geary
View this message in context:
Sent from the Lucene - Java Users mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message