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

FilteredQuery

public class FilteredQuery extends Query
A query that applies a filter to the results of another query.

Note: the bits are retrieved from the filter each time this query is used in a search - use a CachingWrapperFilter to avoid regenerating the bits every time.

Created: Apr 20, 2004 8:58:29 AM

author
Tim Jones
since
1.4
version
$Id: FilteredQuery.java 331113 2005-11-06 15:55:45Z yonik $
see
CachingWrapperFilter

Fields Summary
Query
query
Filter
filter
Constructors Summary
public FilteredQuery(Query query, Filter filter)
Constructs a new query which applies a filter to the results of the original query. Filter.bits() will be called every time this query is used in a search.

param
query Query to be filtered, cannot be null.
param
filter Filter to apply to query results, cannot be null.

    this.query = query;
    this.filter = filter;
  
Methods Summary
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)
Returns a Weight that applies the filter to the enclosed query's Weight. This is accomplished by overriding the Scorer returned by the Weight.

    final Weight weight = query.createWeight (searcher);
    final Similarity similarity = query.getSimilarity(searcher);
    return new Weight() {

      // pass these methods through to enclosed query's weight
      public float getValue() { return weight.getValue(); }
      public float sumOfSquaredWeights() throws IOException { return weight.sumOfSquaredWeights(); }
      public void normalize (float v) { weight.normalize(v); }
      public Explanation explain (IndexReader ir, int i) throws IOException { return weight.explain (ir, i); }

      // return this query
      public Query getQuery() { return FilteredQuery.this; }

      // return a scorer that overrides the enclosed query's score if
      // the given hit has been filtered out.
      public Scorer scorer (IndexReader indexReader) throws IOException {
        final Scorer scorer = weight.scorer (indexReader);
        final BitSet bitset = filter.bits (indexReader);
        return new Scorer (similarity) {

          // pass these methods through to the enclosed scorer
          public boolean next() throws IOException { return scorer.next(); }
          public int doc() { return scorer.doc(); }
          public boolean skipTo (int i) throws IOException { return scorer.skipTo(i); }

          // if the document has been filtered out, set score to 0.0
          public float score() throws IOException {
            return (bitset.get(scorer.doc())) ? scorer.score() : 0.0f;
          }

          // add an explanation about whether the document was filtered
          public Explanation explain (int i) throws IOException {
            Explanation exp = scorer.explain (i);
            if (bitset.get(i))
              exp.setDescription ("allowed by filter: "+exp.getDescription());
            else
              exp.setDescription ("removed by filter: "+exp.getDescription());
            return exp;
          }
        };
      }
    };
  
public booleanequals(java.lang.Object o)
Returns true iff o is equal to this.

    if (o instanceof FilteredQuery) {
      FilteredQuery fq = (FilteredQuery) o;
      return (query.equals(fq.query) && filter.equals(fq.filter));
    }
    return false;
  
public voidextractTerms(java.util.Set terms)

      getQuery().extractTerms(terms);
  
public org.apache.lucene.search.FiltergetFilter()

    return filter;
  
public org.apache.lucene.search.QuerygetQuery()

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

    return query.hashCode() ^ filter.hashCode();
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)
Rewrites the wrapped query.

    Query rewritten = query.rewrite(reader);
    if (rewritten != query) {
      FilteredQuery clone = (FilteredQuery)this.clone();
      clone.query = rewritten;
      return clone;
    } else {
      return this;
    }
  
public java.lang.StringtoString(java.lang.String s)
Prints a user-readable version of this query.

    StringBuffer buffer = new StringBuffer();
    buffer.append("filtered(");
    buffer.append(query.toString(s));
    buffer.append(")->");
    buffer.append(filter);
    buffer.append(ToStringUtils.boost(getBoost()));
    return buffer.toString();