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.
20 import java.util.List;
21 import java.util.Iterator;
23 import java.io.IOException;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.search.Query;
27 import org.apache.lucene.search.spans.SpanNearQuery;
28 import org.apache.lucene.search.spans.SpanQuery;
30 public class DistanceQuery extends ComposedQuery implements DistanceSubQuery {
32 List<SrndQuery> queries,
37 super(queries, infix, opName);
38 this.opDistance = opDistance; /* the distance indicated in the operator */
39 this.ordered = ordered;
42 private int opDistance;
43 public int getOpDistance() {return opDistance;}
45 private boolean ordered;
46 public boolean subQueriesOrdered() {return ordered;}
48 public String distanceSubQueryNotAllowed() {
49 Iterator<?> sqi = getSubQueriesIterator();
50 while (sqi.hasNext()) {
51 Object leq = sqi.next();
52 if (leq instanceof DistanceSubQuery) {
53 DistanceSubQuery dsq = (DistanceSubQuery) leq;
54 String m = dsq.distanceSubQueryNotAllowed();
59 return "Operator " + getOperatorName() + " does not allow subquery " + leq.toString();
62 return null; /* subqueries acceptable */
66 public void addSpanQueries(SpanNearClauseFactory sncf) throws IOException {
67 Query snq = getSpanNearQuery(sncf.getIndexReader(),
70 sncf.getBasicQueryFactory());
71 sncf.addSpanNearQuery(snq);
75 public Query makeLuceneQueryFieldNoBoost(final String fieldName, final BasicQueryFactory qf) {
79 public String toString(String fn) {
80 return getClass().toString() + " " + fieldName + " (" + fn + "?)";
84 public Query rewrite(IndexReader reader) throws IOException {
85 return getSpanNearQuery(reader, fieldName, getBoost(), qf);
91 public Query getSpanNearQuery(
95 BasicQueryFactory qf) throws IOException {
96 SpanQuery[] spanNearClauses = new SpanQuery[getNrSubQueries()];
97 Iterator<?> sqi = getSubQueriesIterator();
99 while (sqi.hasNext()) {
100 SpanNearClauseFactory sncf = new SpanNearClauseFactory(reader, fieldName, qf);
102 ((DistanceSubQuery)sqi.next()).addSpanQueries(sncf);
103 if (sncf.size() == 0) { /* distance operator requires all sub queries */
104 while (sqi.hasNext()) { /* produce evt. error messages but ignore results */
105 ((DistanceSubQuery)sqi.next()).addSpanQueries(sncf);
108 return SrndQuery.theEmptyLcnQuery;
111 spanNearClauses[qi] = sncf.makeSpanNearClause();
116 SpanNearQuery r = new SpanNearQuery(spanNearClauses, getOpDistance() - 1, subQueriesOrdered());