001: package org.apache.lucene.queryParser.surround.query;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.ArrayList;
021: import java.io.IOException;
022: import org.apache.lucene.index.Term;
023: import org.apache.lucene.index.IndexReader;
024: import org.apache.lucene.search.Query;
025: import org.apache.lucene.search.BooleanClause;
026:
027: public abstract class SimpleTerm extends SrndQuery implements
028: DistanceSubQuery, Comparable {
029: public SimpleTerm(boolean q) {
030: quoted = q;
031: }
032:
033: private boolean quoted;
034:
035: boolean isQuoted() {
036: return quoted;
037: }
038:
039: public String getQuote() {
040: return "\"";
041: }
042:
043: public String getFieldOperator() {
044: return "/";
045: }
046:
047: public abstract String toStringUnquoted();
048:
049: public int compareTo(Object o) {
050: /* for ordering terms and prefixes before using an index, not used */
051: SimpleTerm ost = (SimpleTerm) o;
052: return this .toStringUnquoted()
053: .compareTo(ost.toStringUnquoted());
054: }
055:
056: protected void suffixToString(StringBuffer r) {
057: ;
058: } /* override for prefix query */
059:
060: public String toString() {
061: StringBuffer r = new StringBuffer();
062: if (isQuoted()) {
063: r.append(getQuote());
064: }
065: r.append(toStringUnquoted());
066: if (isQuoted()) {
067: r.append(getQuote());
068: }
069: suffixToString(r);
070: weightToString(r);
071: return r.toString();
072: }
073:
074: public abstract void visitMatchingTerms(IndexReader reader,
075: String fieldName, MatchingTermVisitor mtv)
076: throws IOException;
077:
078: public interface MatchingTermVisitor {
079: void visitMatchingTerm(Term t) throws IOException;
080: }
081:
082: public String distanceSubQueryNotAllowed() {
083: return null;
084: }
085:
086: public Query makeLuceneQueryFieldNoBoost(final String fieldName,
087: final BasicQueryFactory qf) {
088: return new Query() {
089: public String toString(String fn) {
090: return getClass().toString() + " " + fieldName + " ("
091: + fn + "?)";
092: }
093:
094: public Query rewrite(IndexReader reader) throws IOException {
095: final ArrayList luceneSubQueries = new ArrayList();
096: visitMatchingTerms(reader, fieldName,
097: new MatchingTermVisitor() {
098: public void visitMatchingTerm(Term term)
099: throws IOException {
100: luceneSubQueries.add(qf
101: .newTermQuery(term));
102: }
103: });
104: return (luceneSubQueries.size() == 0) ? SrndQuery.theEmptyLcnQuery
105: : (luceneSubQueries.size() == 1) ? (Query) luceneSubQueries
106: .get(0)
107: : SrndBooleanQuery.makeBooleanQuery(
108: /* luceneSubQueries all have default weight */
109: luceneSubQueries,
110: BooleanClause.Occur.SHOULD); /* OR the subquery terms */
111: }
112: };
113: }
114:
115: public void addSpanQueries(final SpanNearClauseFactory sncf)
116: throws IOException {
117: visitMatchingTerms(sncf.getIndexReader(), sncf.getFieldName(),
118: new MatchingTermVisitor() {
119: public void visitMatchingTerm(Term term)
120: throws IOException {
121: sncf.addTermWeighted(term, getWeight());
122: }
123: });
124: }
125: }
|