KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > QueryFilter


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.util.WeakHashMap JavaDoc;
21 import java.util.BitSet JavaDoc;
22 import org.apache.lucene.index.IndexReader;
23
24 /** Constrains search results to only match those which also match a provided
25  * query. Results are cached, so that searches after the first on the same
26  * index using this filter are much faster.
27  *
28  * <p> This could be used, for example, with a {@link RangeQuery} on a suitably
29  * formatted date field to implement date filtering. One could re-use a single
30  * QueryFilter that matches, e.g., only documents modified within the last
31  * week. The QueryFilter and RangeQuery would only need to be reconstructed
32  * once per day.
33  *
34  * @version $Id: QueryFilter.java 328729 2005-10-26 21:05:35Z yonik $
35  */

36 public class QueryFilter extends Filter {
37   private Query query;
38   private transient WeakHashMap JavaDoc cache = null;
39
40   /** Constructs a filter which only matches documents matching
41    * <code>query</code>.
42    */

43   public QueryFilter(Query query) {
44     this.query = query;
45   }
46
47   public BitSet JavaDoc bits(IndexReader reader) throws IOException JavaDoc {
48
49     if (cache == null) {
50       cache = new WeakHashMap JavaDoc();
51     }
52
53     synchronized (cache) { // check cache
54
BitSet JavaDoc cached = (BitSet JavaDoc) cache.get(reader);
55       if (cached != null) {
56         return cached;
57       }
58     }
59
60     final BitSet JavaDoc bits = new BitSet JavaDoc(reader.maxDoc());
61
62     new IndexSearcher(reader).search(query, new HitCollector() {
63       public final void collect(int doc, float score) {
64         bits.set(doc); // set bit for hit
65
}
66     });
67
68     synchronized (cache) { // update cache
69
cache.put(reader, bits);
70     }
71
72     return bits;
73   }
74
75   public String JavaDoc toString() {
76     return "QueryFilter("+query+")";
77   }
78
79   public boolean equals(Object JavaDoc o) {
80     if (!(o instanceof QueryFilter)) return false;
81     return this.query.equals(((QueryFilter)o).query);
82   }
83
84   public int hashCode() {
85     return query.hashCode() ^ 0x923F64B9;
86   }
87 }
88
Popular Tags