1 package org.apache.lucene.search;
3 * Copyright 2007 The Apache Software Foundation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 import org.apache.lucene.index.IndexReader;
20 import org.apache.lucene.search.spans.SpanQuery;
21 import org.apache.lucene.search.spans.Spans;
22 import org.apache.lucene.util.FixedBitSet;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
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.
35 * This filter does not cache. See the {@link org.apache.lucene.search.CachingSpanFilter} for a wrapper that
38 public class SpanQueryFilter extends SpanFilter {
39 protected SpanQuery query;
41 protected SpanQueryFilter()
46 /** Constructs a filter which only matches documents matching
48 * @param query The {@link org.apache.lucene.search.spans.SpanQuery} to use as the basis for the Filter.
50 public SpanQueryFilter(SpanQuery query) {
55 public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
56 SpanFilterResult result = bitSpans(reader);
57 return result.getDocIdSet();
61 public SpanFilterResult bitSpans(IndexReader reader) throws IOException {
63 final FixedBitSet bits = new FixedBitSet(reader.maxDoc());
64 Spans spans = query.getSpans(reader);
65 List<SpanFilterResult.PositionInfo> tmp = new ArrayList<SpanFilterResult.PositionInfo>(20);
67 SpanFilterResult.PositionInfo currentInfo = null;
70 int doc = spans.doc();
72 if (currentDoc != doc)
74 currentInfo = new SpanFilterResult.PositionInfo(doc);
78 currentInfo.addPosition(spans.start(), spans.end());
80 return new SpanFilterResult(bits, tmp);
84 public SpanQuery getQuery() {
89 public String toString() {
90 return "SpanQueryFilter(" + query + ")";
94 public boolean equals(Object o) {
95 return o instanceof SpanQueryFilter && this.query.equals(((SpanQueryFilter) o).query);
99 public int hashCode() {
100 return query.hashCode() ^ 0x923F64B9;