1 16 package dlog4j.search; 17 18 import java.io.IOException ; 19 import java.util.BitSet ; 20 21 import org.apache.lucene.index.IndexReader; 22 import org.apache.lucene.index.Term; 23 import org.apache.lucene.index.TermDocs; 24 25 43 public class FieldFilter extends org.apache.lucene.search.Filter { 44 45 private Term [] searchTerms; 46 47 59 public FieldFilter(String field, String [] values) { 60 searchTerms = new Term[values.length]; 61 for (int i=0; i<values.length; i++) { 62 searchTerms[i] = new Term(field, values[i]); 63 } 64 } 65 66 77 public FieldFilter(String field, String value) { 78 this(field, new String [] { value }); 79 } 80 81 public BitSet bits(IndexReader reader) throws IOException { 82 BitSet bits = new BitSet (reader.maxDoc()); 84 for (int i=0; i < searchTerms.length; i++) { 86 TermDocs matchingDocs = reader.termDocs(searchTerms[i]); 89 try { 90 if (matchingDocs != null) { 91 while(matchingDocs.next()) { 92 bits.set(matchingDocs.doc()); 93 } 94 } 95 } 96 finally { 97 if (matchingDocs != null) { 98 matchingDocs.close(); 99 } 100 } 101 } 102 return bits; 103 } 104 } | Popular Tags |