I would like to contribute the following class 
com.lucene.search.MultiFilter which I wrote.
This class essentially allow the use of more than one filter in a search. 
Perhaps the best way to illustrate this is with the following code:

DateFilter dateRange1 = new DateFilter("publishdate", new Date(1970), new 
Date(1975));
DateFitler dateRange2 = new DateFilter("publishdate", new Date(1980), new 
Date(1985));
MultiFilter multiFilter = new MultiFilter(dateRange1, MultiFilter.OR, 
dateRange2);

Hits hits = searcher.search(query, multiFilter);

In the above code, we need to filter documents that are published between 
1970 to 1975, or 1980 to 1985. This is done by creating two DateFilters, 
dateRange1 and dateRange2. The two DateFilters will each flag documents 
that fall into their respective date ranges and return the result as a 
BitSet. MultiFilter takes the two resulting BitSets and merges them using 
the OR operator.

Other operators supported by MultiFilter include AND, AND NOT, XOR.
Below is the MultiFilter source:



package com.lucene.search;

import com.lucene.index.IndexReader;
import com.lucene.search.Filter;
import java.io.IOException;
import java.util.BitSet;

public class MultiFilter extends Filter {

   public static final int AND = 0;
   public static final int ANDNOT = 1;
   public static final int OR = 2;
   public static final int XOR = 3;

   Filter left, right;
   int operator;

   public MultiFilter(Filter left, int operator, Filter right) {
     this.left = left;
     this.operator = operator;
     this.right = right;
   }

   /**
    * Returns a BitSet with true for documents which should be permitted in
    * search results, and false for those that should not.
    */
   public BitSet bits(IndexReader reader) throws IOException {
     if ((left == null) && (right == null)) return null; // nothing to do
     if (left == null) return right.bits(reader); // left is null, return 
right filter's bitset
     if (right == null) return left.bits(reader); // right is null, return 
left filter's bitset

     // Merge left and right bitsets
     BitSet bitset = left.bits(reader);
     switch (operator) {
       case AND    : bitset.and(right.bits(reader)); break;
       case ANDNOT : bitset.andNot(right.bits(reader)); break;
       case OR     : bitset.or(right.bits(reader)); break;
       case XOR    : bitset.xor(right.bits(reader)); break;
     }
     return bitset;
   }
}


_______________________________________________
Lucene-dev mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/lucene-dev

Reply via email to