FileDocCategorySizeDatePackage
ChainedFilter.javaAPI DocApache Lucene 1.97894Mon Feb 20 09:18:28 GMT 2006org.apache.lucene.misc

ChainedFilter

public class ChainedFilter extends Filter

Allows multiple {@link Filter}s to be chained. Logical operations such as NOT and XOR are applied between filters. One operation can be used for all filters, or a specific operation can be declared for each filter.

Order in which filters are called depends on the position of the filter in the chain. It's probably more efficient to place the most restrictive filters /least computationally-intensive filters first.

author
Kelvin Tan

Fields Summary
public static final int
OR
{@link BitSet#or}.
public static final int
AND
{@link BitSet#and}.
public static final int
ANDNOT
{@link BitSet#andNot}.
public static final int
XOR
{@link BitSet#xor}.
public static int
DEFAULT
Logical operation when none is declared. Defaults to {@link BitSet#or}.
private Filter[]
chain
The filter chain
private int[]
logicArray
private int
logic
Constructors Summary
public ChainedFilter(Filter[] chain)
Ctor.

param
chain The chain of filters


                
      
    
        this.chain = chain;
    
public ChainedFilter(Filter[] chain, int[] logicArray)
Ctor.

param
chain The chain of filters
param
logicArray Logical operations to apply between filters

        this.chain = chain;
        this.logicArray = logicArray;
    
public ChainedFilter(Filter[] chain, int logic)
Ctor.

param
chain The chain of filters
param
logic Logicial operation to apply to ALL filters

        this.chain = chain;
        this.logic = logic;
    
Methods Summary
public java.util.BitSetbits(org.apache.lucene.index.IndexReader reader)
{@link Filter#bits}.

        if (logic != -1)
            return bits(reader, logic);
        else if (logicArray != null)
            return bits(reader, logicArray);
        else
            return bits(reader, DEFAULT);
    
private java.util.BitSetbits(org.apache.lucene.index.IndexReader reader, int logic)
Delegates to each filter in the chain.

param
reader IndexReader
param
logic Logical operation
return
BitSet

        BitSet result;
        int i = 0;

        /**
         * First AND operation takes place against a completely false
         * bitset and will always return zero results. Thanks to
         * Daniel Armbrust for pointing this out and suggesting workaround.
         */
        if (logic == AND)
        {
            result = (BitSet) chain[i].bits(reader).clone();
            ++i;
        }
        else
        {
            result = new BitSet(reader.maxDoc());
        }

        for (; i < chain.length; i++)
        {
            doChain(result, reader, logic, chain[i]);
        }
        return result;
    
private java.util.BitSetbits(org.apache.lucene.index.IndexReader reader, int[] logic)
Delegates to each filter in the chain.

param
reader IndexReader
param
logic Logical operation
return
BitSet

        if (logic.length != chain.length)
            throw new IllegalArgumentException("Invalid number of elements in logic array");
        BitSet result;
        int i = 0;

        /**
         * First AND operation takes place against a completely false
         * bitset and will always return zero results. Thanks to
         * Daniel Armbrust for pointing this out and suggesting workaround.
         */
        if (logic[0] == AND)
        {
            result = (BitSet) chain[i].bits(reader).clone();
            ++i;
        }
        else
        {
            result = new BitSet(reader.maxDoc());
        }

        for (; i < chain.length; i++)
        {
            doChain(result, reader, logic[i], chain[i]);
        }
        return result;
    
private voiddoChain(java.util.BitSet result, org.apache.lucene.index.IndexReader reader, int logic, org.apache.lucene.search.Filter filter)

        switch (logic)
        {
            case OR:
                result.or(filter.bits(reader));
                break;
            case AND:
                result.and(filter.bits(reader));
                break;
            case ANDNOT:
                result.andNot(filter.bits(reader));
                break;
            case XOR:
                result.xor(filter.bits(reader));
                break;
            default:
                doChain(result, reader, DEFAULT, filter);
                break;
        }
    
public java.lang.StringtoString()

        StringBuffer sb = new StringBuffer();
        sb.append("ChainedFilter: [");
        for (int i = 0; i < chain.length; i++)
        {
            sb.append(chain[i]);
            sb.append(' ");
        }
        sb.append(']");
        return sb.toString();