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

QueryFilter

public class QueryFilter extends Filter
Constrains search results to only match those which also match a provided query. Results are cached, so that searches after the first on the same index using this filter are much faster.

This could be used, for example, with a {@link RangeQuery} on a suitably formatted date field to implement date filtering. One could re-use a single QueryFilter that matches, e.g., only documents modified within the last week. The QueryFilter and RangeQuery would only need to be reconstructed once per day.

version
$Id: QueryFilter.java 472959 2006-11-09 16:21:50Z yonik $

Fields Summary
private Query
query
private transient WeakHashMap
cache
Constructors Summary
public QueryFilter(Query query)
Constructs a filter which only matches documents matching query.


              
     
    this.query = query;
  
Methods Summary
public java.util.BitSetbits(org.apache.lucene.index.IndexReader reader)


    if (cache == null) {
      cache = new WeakHashMap();
    }

    synchronized (cache) {  // check cache
      BitSet cached = (BitSet) cache.get(reader);
      if (cached != null) {
        return cached;
      }
    }

    final BitSet bits = new BitSet(reader.maxDoc());

    new IndexSearcher(reader).search(query, new HitCollector() {
      public final void collect(int doc, float score) {
        bits.set(doc);  // set bit for hit
      }
    });

    synchronized (cache) {  // update cache
      cache.put(reader, bits);
    }

    return bits;
  
public booleanequals(java.lang.Object o)

    if (!(o instanceof QueryFilter)) return false;
    return this.query.equals(((QueryFilter)o).query);
  
public inthashCode()

    return query.hashCode() ^ 0x923F64B9;  
  
public java.lang.StringtoString()

    return "QueryFilter("+query+")";