Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
Created March 12, 2009 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtemGr/78115 to your computer and use it in GitHub Desktop.
Save ArtemGr/78115 to your computer and use it in GitHub Desktop.
Lucene matching non-empty fields.
// Skip attachments (images, documents, etc.) - all those files without "_id".
booleanQuery.add (new NotEmptyQuery (new Term ("alt__id")), BooleanClause.Occur.MUST)
// Skip removed objects (_removed = time of removal in milliseconds).
booleanQuery.add (new NotEmptyQuery (new Term ("alt__removed")), BooleanClause.Occur.MUST_NOT)
import org.apache.lucene.search._, org.apache.lucene.document._, org.apache.lucene.index._
class NotEmptyQuery (emptyTerm: Term) extends MultiTermQuery {
override def toString (field: String) = "NotEmptyQuery(" + (if ((field ne null) && field.length != 0) field else emptyTerm.field) + ")"
override def getEnum (reader: IndexReader) = new FilteredTermEnum {
setEnum (reader.terms (emptyTerm)) // Position at the start of the field terms.
var theEnd = false
override def termCompare (term: Term): Boolean =
if (term.field == emptyTerm.field && term.text != emptyTerm.text) true else {theEnd = true; false}
override def difference (): Float = 1
override def endEnum (): Boolean = theEnd
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment