01: package org.apache.lucene.search;
02:
03: /**
04: * Copyright 2007 The Apache Software Foundation
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
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 org.apache.lucene.index.IndexReader;
20: import org.apache.lucene.search.spans.SpanQuery;
21: import org.apache.lucene.search.spans.Spans;
22:
23: import java.io.IOException;
24: import java.util.ArrayList;
25: import java.util.BitSet;
26: import java.util.List;
27:
28: /**
29: * Constrains search results to only match those which also match a provided
30: * query. Also provides position information about where each document matches
31: * at the cost of extra space compared with the QueryWrapperFilter.
32: * There is an added cost to this above what is stored in a {@link QueryWrapperFilter}. Namely,
33: * the position information for each matching document is stored.
34: * <p/>
35: * This filter does not cache. See the {@link org.apache.lucene.search.CachingSpanFilter} for a wrapper that
36: * caches.
37: *
38: *
39: * @version $Id:$
40: */
41: public class SpanQueryFilter extends SpanFilter {
42: protected SpanQuery query;
43:
44: protected SpanQueryFilter() {
45:
46: }
47:
48: /** Constructs a filter which only matches documents matching
49: * <code>query</code>.
50: * @param query The {@link org.apache.lucene.search.spans.SpanQuery} to use as the basis for the Filter.
51: */
52: public SpanQueryFilter(SpanQuery query) {
53: this .query = query;
54: }
55:
56: public BitSet bits(IndexReader reader) throws IOException {
57: SpanFilterResult result = bitSpans(reader);
58: return result.getBits();
59: }
60:
61: public SpanFilterResult bitSpans(IndexReader reader)
62: throws IOException {
63:
64: final BitSet bits = new BitSet(reader.maxDoc());
65: Spans spans = query.getSpans(reader);
66: List tmp = new ArrayList(20);
67: int currentDoc = -1;
68: SpanFilterResult.PositionInfo currentInfo = null;
69: while (spans.next()) {
70: int doc = spans.doc();
71: bits.set(doc);
72: if (currentDoc != doc) {
73: currentInfo = new SpanFilterResult.PositionInfo(doc);
74: tmp.add(currentInfo);
75: currentDoc = doc;
76: }
77: currentInfo.addPosition(spans.start(), spans.end());
78: }
79: return new SpanFilterResult(bits, tmp);
80: }
81:
82: public SpanQuery getQuery() {
83: return query;
84: }
85:
86: public String toString() {
87: return "QueryWrapperFilter(" + query + ")";
88: }
89:
90: public boolean equals(Object o) {
91: return o instanceof SpanQueryFilter
92: && this .query.equals(((SpanQueryFilter) o).query);
93: }
94:
95: public int hashCode() {
96: return query.hashCode() ^ 0x923F64B9;
97: }
98: }
|