FileDocCategorySizeDatePackage
QueryFilter.javaAPI DocApache Lucene 1.4.32361Sat May 08 21:54:12 BST 2004org.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,v 1.6 2004/05/08 19:54:12 ehatcher Exp $

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 java.lang.StringtoString()

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