2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. 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.
18
package org.apache.solr.search;
20
import org.apache.lucene.search.Filter;
21
import org.apache.lucene.search.DocIdSet;
22
import org.apache.lucene.index.Term;
23
import org.apache.lucene.index.IndexReader;
24
import org.apache.lucene.index.TermEnum;
25
import org.apache.lucene.index.TermDocs;
26
import org.apache.lucene.util.OpenBitSet;
28
import java.io.IOException;
31
* @version $Id: PrefixFilter.java 922957 2010-03-14 20:58:32Z markrmiller $
33
public class PrefixFilter extends Filter {
34
protected final Term prefix;
36
PrefixFilter(Term prefix) {
40
Term getPrefix() { return prefix; }
43
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
44
final OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
45
new PrefixGenerator(prefix) {
46
public void handleDoc(int doc) {
54
public boolean equals(Object o) {
55
return o instanceof PrefixFilter && ((PrefixFilter)o).prefix.equals(this.prefix);
59
public int hashCode() {
60
return 0xcecf7fe2 + prefix.hashCode();
64
public String toString () {
65
StringBuilder sb = new StringBuilder();
66
sb.append("PrefixFilter(");
67
sb.append(prefix.toString());
73
// keep this protected until I decide if it's a good way
74
// to separate id generation from collection (or should
75
// I just reuse hitcollector???)
76
interface IdGenerator {
77
public void generate(IndexReader reader) throws IOException;
78
public void handleDoc(int doc);
82
abstract class PrefixGenerator implements IdGenerator {
83
protected final Term prefix;
85
PrefixGenerator(Term prefix) {
89
public void generate(IndexReader reader) throws IOException {
90
TermEnum enumerator = reader.terms(prefix);
91
TermDocs termDocs = reader.termDocs();
95
String prefixText = prefix.text();
96
String prefixField = prefix.field();
98
Term term = enumerator.term();
100
term.text().startsWith(prefixText) &&
101
term.field() == prefixField)
104
while (termDocs.next()) {
105
handleDoc(termDocs.doc());
110
} while (enumerator.next());