FileDocCategorySizeDatePackage
RangeQuery.javaAPI DocApache Lucene 2.1.06888Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.search

RangeQuery

public class RangeQuery extends Query
A Query that matches documents within an exclusive range. A RangeQuery is built by QueryParser for input like [010 TO 120] but only if the QueryParser has the useOldRangeQuery property set to true. The QueryParser default behaviour is to use the newer ConstantScoreRangeQuery class. This is generally preferable because:
  • It is faster than RangeQuery
  • Unlike RangeQuery, it does not cause a BooleanQuery.TooManyClauses exception if the range of values is large
  • Unlike RangeQuery it does not influence scoring based on the scarcity of individual terms that may match
see
ConstantScoreRangeQuery
version
$Id: RangeQuery.java 475435 2006-11-15 21:26:09Z mharwood $

Fields Summary
private Term
lowerTerm
private Term
upperTerm
private boolean
inclusive
Constructors Summary
public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive)
Constructs a query selecting all terms greater than lowerTerm but less than upperTerm. There must be at least one term and either term may be null, in which case there is no bound on that side, but if there are two terms, both terms must be for the same field.

        if (lowerTerm == null && upperTerm == null)
        {
            throw new IllegalArgumentException("At least one term must be non-null");
        }
        if (lowerTerm != null && upperTerm != null && lowerTerm.field() != upperTerm.field())
        {
            throw new IllegalArgumentException("Both terms must be for the same field");
        }

        // if we have a lowerTerm, start there. otherwise, start at beginning
        if (lowerTerm != null) {
            this.lowerTerm = lowerTerm;
        }
        else {
            this.lowerTerm = new Term(upperTerm.field(), "");
        }

        this.upperTerm = upperTerm;
        this.inclusive = inclusive;
    
Methods Summary
public booleanequals(java.lang.Object o)
Returns true iff o is equal to this.

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

        final RangeQuery other = (RangeQuery) o;
        if (this.getBoost() != other.getBoost()) return false;
        if (this.inclusive != other.inclusive) return false;
        // one of lowerTerm and upperTerm can be null
        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 java.lang.StringgetField()
Returns the field name for this query

      return (lowerTerm != null ? lowerTerm.field() : upperTerm.field());
    
public org.apache.lucene.index.TermgetLowerTerm()
Returns the lower term of this range query

 return lowerTerm; 
public org.apache.lucene.index.TermgetUpperTerm()
Returns the upper term of this range query

 return upperTerm; 
public inthashCode()
Returns a hash code value for this object.

      int h = Float.floatToIntBits(getBoost());
      h ^= lowerTerm != null ? lowerTerm.hashCode() : 0;
      // reversible mix to make lower and upper position dependent and
      // to prevent them from cancelling out.
      h ^= (h << 25) | (h >>> 8);
      h ^= upperTerm != null ? upperTerm.hashCode() : 0;
      h ^= this.inclusive ? 0x2742E74A : 0;
      return h;
    
public booleanisInclusive()
Returns true if the range query is inclusive

 return inclusive; 
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)


        BooleanQuery query = new BooleanQuery(true);
        TermEnum enumerator = reader.terms(lowerTerm);

        try {

            boolean checkLower = false;
            if (!inclusive) // make adjustments to set to exclusive
                checkLower = true;

            String testField = getField();

            do {
                Term term = enumerator.term();
                if (term != null && term.field() == testField) {
                    if (!checkLower || term.text().compareTo(lowerTerm.text()) > 0) {
                        checkLower = false;
                        if (upperTerm != null) {
                            int compare = upperTerm.text().compareTo(term.text());
                            /* if beyond the upper term, or is exclusive and
                             * this is equal to the upper term, break out */
                            if ((compare < 0) || (!inclusive && compare == 0))
                                break;
                        }
                        TermQuery tq = new TermQuery(term); // found a match
                        tq.setBoost(getBoost()); // set the boost
                        query.add(tq, BooleanClause.Occur.SHOULD); // add to query
                    }
                }
                else {
                    break;
                }
            }
            while (enumerator.next());
        }
        finally {
            enumerator.close();
        }
        return query;
    
public java.lang.StringtoString(java.lang.String field)
Prints a user-readable version of this query.

        StringBuffer buffer = new StringBuffer();
        if (!getField().equals(field))
        {
            buffer.append(getField());
            buffer.append(":");
        }
        buffer.append(inclusive ? "[" : "{");
        buffer.append(lowerTerm != null ? lowerTerm.text() : "null");
        buffer.append(" TO ");
        buffer.append(upperTerm != null ? upperTerm.text() : "null");
        buffer.append(inclusive ? "]" : "}");
        buffer.append(ToStringUtils.boost(getBoost()));
        return buffer.toString();