~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/PrefixFilter.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
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
 
package org.apache.solr.search;
19
 
 
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;
27
 
 
28
 
import java.io.IOException;
29
 
 
30
 
/**
31
 
 * @version $Id: PrefixFilter.java 922957 2010-03-14 20:58:32Z markrmiller $
32
 
 */
33
 
public class PrefixFilter extends Filter {
34
 
  protected final Term prefix;
35
 
 
36
 
  PrefixFilter(Term prefix) {
37
 
    this.prefix = prefix;
38
 
  }
39
 
 
40
 
  Term getPrefix() { return prefix; }
41
 
 
42
 
 @Override
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) {
47
 
        bitSet.set(doc);
48
 
      }
49
 
    }.generate(reader);
50
 
    return bitSet;
51
 
  }
52
 
 
53
 
  @Override
54
 
  public boolean equals(Object o) {
55
 
    return o instanceof PrefixFilter && ((PrefixFilter)o).prefix.equals(this.prefix);
56
 
  }
57
 
 
58
 
  @Override
59
 
  public int hashCode() {
60
 
    return 0xcecf7fe2 + prefix.hashCode();
61
 
  }
62
 
 
63
 
  @Override
64
 
  public String toString () {
65
 
    StringBuilder sb = new StringBuilder();
66
 
    sb.append("PrefixFilter(");
67
 
    sb.append(prefix.toString());
68
 
    sb.append(")");
69
 
    return sb.toString();
70
 
  }
71
 
}
72
 
 
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);
79
 
}
80
 
 
81
 
 
82
 
abstract class PrefixGenerator implements IdGenerator {
83
 
  protected final Term prefix;
84
 
 
85
 
  PrefixGenerator(Term prefix) {
86
 
    this.prefix = prefix;
87
 
  }
88
 
 
89
 
  public void generate(IndexReader reader) throws IOException {
90
 
    TermEnum enumerator = reader.terms(prefix);
91
 
    TermDocs termDocs = reader.termDocs();
92
 
 
93
 
    try {
94
 
 
95
 
      String prefixText = prefix.text();
96
 
      String prefixField = prefix.field();
97
 
      do {
98
 
        Term term = enumerator.term();
99
 
        if (term != null &&
100
 
            term.text().startsWith(prefixText) &&
101
 
            term.field() == prefixField)
102
 
        {
103
 
          termDocs.seek(term);
104
 
          while (termDocs.next()) {
105
 
            handleDoc(termDocs.doc());
106
 
          }
107
 
        } else {
108
 
          break;
109
 
        }
110
 
      } while (enumerator.next());
111
 
    } finally {
112
 
      termDocs.close();
113
 
      enumerator.close();
114
 
    }
115
 
  }
116
 
}