FileDocCategorySizeDatePackage
RangeFilter.javaAPI DocApache Lucene 1.97205Mon Feb 20 09:20:04 GMT 2006org.apache.lucene.search

RangeFilter

public class RangeFilter extends Filter
A Filter that restricts search results to a range of values in a given field.

This code borrows heavily from {@link RangeQuery}, but is implemented as a Filter (much like {@link DateFilter}).

Fields Summary
private String
fieldName
private String
lowerTerm
private String
upperTerm
private boolean
includeLower
private boolean
includeUpper
Constructors Summary
public RangeFilter(String fieldName, String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper)

param
fieldName The field this range applies to
param
lowerTerm The lower bound on this range
param
upperTerm The upper bound on this range
param
includeLower Does this range include the lower bound?
param
includeUpper Does this range include the upper bound?
throws
IllegalArgumentException if both terms are null or if lowerTerm is null and includeLower is true (similar for upperTerm and includeUpper)

        this.fieldName = fieldName;
        this.lowerTerm = lowerTerm;
        this.upperTerm = upperTerm;
        this.includeLower = includeLower;
        this.includeUpper = includeUpper;
        
        if (null == lowerTerm && null == upperTerm) {
            throw new IllegalArgumentException
                ("At least one value must be non-null");
        }
        if (includeLower && null == lowerTerm) {
            throw new IllegalArgumentException
                ("The lower bound must be non-null to be inclusive");
        }
        if (includeUpper && null == upperTerm) {
            throw new IllegalArgumentException
                ("The upper bound must be non-null to be inclusive");
        }
    
Methods Summary
public static org.apache.lucene.search.RangeFilterLess(java.lang.String fieldName, java.lang.String upperTerm)
Constructs a filter for field fieldName matching less than or equal to upperTerm.

        return new RangeFilter(fieldName, null, upperTerm, false, true);
    
public static org.apache.lucene.search.RangeFilterMore(java.lang.String fieldName, java.lang.String lowerTerm)
Constructs a filter for field fieldName matching greater than or equal to lowerTerm.

        return new RangeFilter(fieldName, lowerTerm, null, true, false);
    
public java.util.BitSetbits(org.apache.lucene.index.IndexReader reader)
Returns a BitSet with true for documents which should be permitted in search results, and false for those that should not.

        BitSet bits = new BitSet(reader.maxDoc());
        TermEnum enumerator =
            (null != lowerTerm
             ? reader.terms(new Term(fieldName, lowerTerm))
             : reader.terms(new Term(fieldName,"")));
        
        try {
            
            if (enumerator.term() == null) {
                return bits;
            }
            
            boolean checkLower = false;
            if (!includeLower) // make adjustments to set to exclusive
                checkLower = true;
        
            TermDocs termDocs = reader.termDocs();
            try {
                
                do {
                    Term term = enumerator.term();
                    if (term != null && term.field().equals(fieldName)) {
                        if (!checkLower || null==lowerTerm || term.text().compareTo(lowerTerm) > 0) {
                            checkLower = false;
                            if (upperTerm != null) {
                                int compare = upperTerm.compareTo(term.text());
                                /* if beyond the upper term, or is exclusive and
                                 * this is equal to the upper term, break out */
                                if ((compare < 0) ||
                                    (!includeUpper && compare==0)) {
                                    break;
                                }
                            }
                            /* we have a good term, find the docs */
                            
                            termDocs.seek(enumerator.term());
                            while (termDocs.next()) {
                                bits.set(termDocs.doc());
                            }
                        }
                    } else {
                        break;
                    }
                }
                while (enumerator.next());
                
            } finally {
                termDocs.close();
            }
        } finally {
            enumerator.close();
        }

        return bits;
    
public booleanequals(java.lang.Object o)
Returns true if o is equal to this.

        if (this == o) return true;
        if (!(o instanceof RangeFilter)) return false;
        RangeFilter other = (RangeFilter) o;

        if (!this.fieldName.equals(other.fieldName)
            || this.includeLower != other.includeLower
            || this.includeUpper != other.includeUpper
           ) { return false; }
        if (this.lowerTerm != null ? !this.lowerTerm.equals(other.lowerTerm) : other.lowerTerm != null) return false;
        if (this.upperTerm != null ? !this.upperTerm.equals(other.upperTerm) : other.upperTerm != null) return false;
        return true;
    
public inthashCode()
Returns a hash code value for this object.

      int h = fieldName.hashCode();
      h ^= lowerTerm != null ? lowerTerm.hashCode() : 0xB6ECE882;
      h = (h << 1) | (h >>> 31);  // rotate to distinguish lower from upper
      h ^= (upperTerm != null ? (upperTerm.hashCode()) : 0x91BEC2C2);
      h ^= (includeLower ? 0xD484B933 : 0)
         ^ (includeUpper ? 0x6AE423AC : 0);
      return h;
    
public java.lang.StringtoString()

        StringBuffer buffer = new StringBuffer();
        buffer.append(fieldName);
        buffer.append(":");
        buffer.append(includeLower ? "[" : "{");
        if (null != lowerTerm) {
            buffer.append(lowerTerm);
        }
        buffer.append("-");
        if (null != upperTerm) {
            buffer.append(upperTerm);
        }
        buffer.append(includeUpper ? "]" : "}");
        return buffer.toString();