01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21: import java.util.BitSet;
22:
23: import org.apache.lucene.index.IndexReader;
24:
25: /**
26: * Constrains search results to only match those which also match a provided
27: * query.
28: *
29: * <p> This could be used, for example, with a {@link RangeQuery} on a suitably
30: * formatted date field to implement date filtering. One could re-use a single
31: * QueryFilter that matches, e.g., only documents modified within the last
32: * week. The QueryFilter and RangeQuery would only need to be reconstructed
33: * once per day.
34: *
35: * @version $Id:$
36: */
37: public class QueryWrapperFilter extends Filter {
38: private Query query;
39:
40: /** Constructs a filter which only matches documents matching
41: * <code>query</code>.
42: */
43: public QueryWrapperFilter(Query query) {
44: this .query = query;
45: }
46:
47: public BitSet bits(IndexReader reader) throws IOException {
48: final BitSet bits = new BitSet(reader.maxDoc());
49:
50: new IndexSearcher(reader).search(query, new HitCollector() {
51: public final void collect(int doc, float score) {
52: bits.set(doc); // set bit for hit
53: }
54: });
55: return bits;
56: }
57:
58: public String toString() {
59: return "QueryWrapperFilter(" + query + ")";
60: }
61:
62: public boolean equals(Object o) {
63: if (!(o instanceof QueryWrapperFilter))
64: return false;
65: return this .query.equals(((QueryWrapperFilter) o).query);
66: }
67:
68: public int hashCode() {
69: return query.hashCode() ^ 0x923F64B9;
70: }
71: }
|