1 package org.apache.lucene.queryParser.surround.query;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.search.BooleanClause;
26 import org.apache.lucene.search.Query;
28 public abstract class SimpleTerm
30 implements DistanceSubQuery, Comparable<SimpleTerm>
32 public SimpleTerm(boolean q) {quoted = q;}
34 private boolean quoted;
35 boolean isQuoted() {return quoted;}
37 public String getQuote() {return "\"";}
38 public String getFieldOperator() {return "/";}
40 public abstract String toStringUnquoted();
42 public int compareTo(SimpleTerm ost) {
43 /* for ordering terms and prefixes before using an index, not used */
44 return this.toStringUnquoted().compareTo( ost.toStringUnquoted());
47 protected void suffixToString(StringBuilder r) {} /* override for prefix query */
50 public String toString() {
51 StringBuilder r = new StringBuilder();
55 r.append(toStringUnquoted());
64 public abstract void visitMatchingTerms(
67 MatchingTermVisitor mtv) throws IOException;
69 public interface MatchingTermVisitor {
70 void visitMatchingTerm(Term t)throws IOException;
73 public String distanceSubQueryNotAllowed() {return null;}
77 public Query makeLuceneQueryFieldNoBoost(final String fieldName, final BasicQueryFactory qf) {
80 public String toString(String fn) {
81 return getClass().toString() + " " + fieldName + " (" + fn + "?)";
85 public Query rewrite(IndexReader reader) throws IOException {
86 final List<Query> luceneSubQueries = new ArrayList<Query>();
87 visitMatchingTerms( reader, fieldName,
88 new MatchingTermVisitor() {
89 public void visitMatchingTerm(Term term) throws IOException {
90 luceneSubQueries.add(qf.newTermQuery(term));
93 return (luceneSubQueries.size() == 0) ? SrndQuery.theEmptyLcnQuery
94 : (luceneSubQueries.size() == 1) ? luceneSubQueries.get(0)
95 : SrndBooleanQuery.makeBooleanQuery(
96 /* luceneSubQueries all have default weight */
97 luceneSubQueries, BooleanClause.Occur.SHOULD); /* OR the subquery terms */
102 public void addSpanQueries(final SpanNearClauseFactory sncf) throws IOException {
104 sncf.getIndexReader(),
106 new MatchingTermVisitor() {
107 public void visitMatchingTerm(Term term) throws IOException {
108 sncf.addTermWeighted(term, getWeight());