I developed a
Filter that restricts search results to documents that has terms in specific
fields
(because currently we can't search with
lucene documents with this kind of feature (a document with present/absent of
values in specific fields)
nicolas
|
package org.apache.lucene.search;
import java.io.IOException; import org.apache.lucene.index.IndexReader; import java.util.BitSet; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermEnum; import org.apache.lucene.index.TermDocs; import java.util.*; /** * A Filter that restricts search results to documents that has terms in specific fields * (OR operator: the documents that has terms in field1 or in field2) * @author Nicolas Maisonneuve */ public class HasFieldFilter extends Filter { private Set fieldnames; /** * a array of the field's names * @param fieldname String[] a array of field's names */ public HasFieldFilter (String[] fieldnames) { this.fieldnames=new HashSet(); for (int i=0; i<fieldnames.length; i++) { this.fieldnames.add(fieldnames[i].intern()); } } public BitSet bits (IndexReader reader) throws IOException { final BitSet bits=new BitSet(reader.maxDoc()); TermEnum enumerator=reader.terms(); TermDocs termDocs=reader.termDocs(); Iterator iter=reader.getFieldNames().iterator(); try { // for each field while (iter.hasNext()) { String field=(String) iter.next(); //if is not in the list of specific fields if (!fieldnames.contains(field)) { continue; } enumerator.skipTo(new Term(field, "")); if (enumerator.term()==null) { continue; } // restrict doc to the field while (enumerator.term().field()==field) { termDocs.seek(enumerator.term()); while (termDocs.next()) { bits.set(termDocs.doc()); } if (!enumerator.next()) { break; } } } } finally { enumerator.close(); termDocs.close(); } return bits; } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]