pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / SpanQueryFilter.java
1 package org.apache.lucene.search;
2 /**
3  * Copyright 2007 The Apache Software Foundation
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
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 import org.apache.lucene.util.FixedBitSet;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
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 public class SpanQueryFilter extends SpanFilter {
39   protected SpanQuery query;
40
41   protected SpanQueryFilter()
42   {
43     
44   }
45
46   /** Constructs a filter which only matches documents matching
47    * <code>query</code>.
48    * @param query The {@link org.apache.lucene.search.spans.SpanQuery} to use as the basis for the Filter.
49    */
50   public SpanQueryFilter(SpanQuery query) {
51     this.query = query;
52   }
53
54   @Override
55   public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
56     SpanFilterResult result = bitSpans(reader);
57     return result.getDocIdSet();
58   }
59
60   @Override
61   public SpanFilterResult bitSpans(IndexReader reader) throws IOException {
62
63     final FixedBitSet bits = new FixedBitSet(reader.maxDoc());
64     Spans spans = query.getSpans(reader);
65     List<SpanFilterResult.PositionInfo> tmp = new ArrayList<SpanFilterResult.PositionInfo>(20);
66     int currentDoc = -1;
67     SpanFilterResult.PositionInfo currentInfo = null;
68     while (spans.next())
69     {
70       int doc = spans.doc();
71       bits.set(doc);
72       if (currentDoc != doc)
73       {
74         currentInfo = new SpanFilterResult.PositionInfo(doc);
75         tmp.add(currentInfo);
76         currentDoc = doc;
77       }
78       currentInfo.addPosition(spans.start(), spans.end());
79     }
80     return new SpanFilterResult(bits, tmp);
81   }
82
83
84   public SpanQuery getQuery() {
85     return query;
86   }
87
88   @Override
89   public String toString() {
90     return "SpanQueryFilter(" + query + ")";
91   }
92
93   @Override
94   public boolean equals(Object o) {
95     return o instanceof SpanQueryFilter && this.query.equals(((SpanQueryFilter) o).query);
96   }
97
98   @Override
99   public int hashCode() {
100     return query.hashCode() ^ 0x923F64B9;
101   }
102 }