01: package org.apache.lucene.queryParser.surround.query;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import org.apache.lucene.index.Term;
21: import org.apache.lucene.index.TermEnum;
22: import org.apache.lucene.index.IndexReader;
23:
24: import java.io.IOException;
25:
26: public class SrndPrefixQuery extends SimpleTerm {
27: public SrndPrefixQuery(String prefix, boolean quoted, char truncator) {
28: super (quoted);
29: this .prefix = prefix;
30: this .truncator = truncator;
31: }
32:
33: private final String prefix;
34:
35: public String getPrefix() {
36: return prefix;
37: }
38:
39: private final char truncator;
40:
41: public char getSuffixOperator() {
42: return truncator;
43: }
44:
45: public Term getLucenePrefixTerm(String fieldName) {
46: return new Term(fieldName, getPrefix());
47: }
48:
49: public String toStringUnquoted() {
50: return getPrefix();
51: }
52:
53: protected void suffixToString(StringBuffer r) {
54: r.append(getSuffixOperator());
55: }
56:
57: public void visitMatchingTerms(IndexReader reader,
58: String fieldName, MatchingTermVisitor mtv)
59: throws IOException {
60: /* inspired by PrefixQuery.rewrite(): */
61: TermEnum enumerator = reader
62: .terms(getLucenePrefixTerm(fieldName));
63: boolean expanded = false;
64: try {
65: do {
66: Term term = enumerator.term();
67: if ((term != null)
68: && term.text().startsWith(getPrefix())
69: && term.field().equals(fieldName)) {
70: mtv.visitMatchingTerm(term);
71: expanded = true;
72: } else {
73: break;
74: }
75: } while (enumerator.next());
76: } finally {
77: enumerator.close();
78: }
79: if (!expanded) {
80: System.out.println("No terms in " + fieldName
81: + " field for: " + toString());
82: }
83: }
84: }
|