1 package org.apache.lucene.search.spans;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
23 import java.util.List;
24 import java.util.ArrayList;
25 import java.util.Iterator;
29 import org.apache.lucene.index.IndexReader;
30 import org.apache.lucene.index.Term;
31 import org.apache.lucene.search.Query;
32 import org.apache.lucene.util.ToStringUtils;
34 /** Matches spans which are near one another. One can specify <i>slop</i>, the
35 * maximum number of intervening unmatched positions, as well as whether
36 * matches are required to be in-order. */
37 public class SpanNearQuery extends SpanQuery implements Cloneable {
38 protected List<SpanQuery> clauses;
40 protected boolean inOrder;
42 protected String field;
43 private boolean collectPayloads;
45 /** Construct a SpanNearQuery. Matches spans matching a span from each
46 * clause, with up to <code>slop</code> total unmatched positions between
47 * them. * When <code>inOrder</code> is true, the spans from each clause
48 * must be * ordered as in <code>clauses</code>.
49 * @param clauses the clauses to find near each other
50 * @param slop The slop value
51 * @param inOrder true if order is important
53 public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder) {
54 this(clauses, slop, inOrder, true);
57 public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder, boolean collectPayloads) {
59 // copy clauses array into an ArrayList
60 this.clauses = new ArrayList<SpanQuery>(clauses.length);
61 for (int i = 0; i < clauses.length; i++) {
62 SpanQuery clause = clauses[i];
63 if (i == 0) { // check field
64 field = clause.getField();
65 } else if (!clause.getField().equals(field)) {
66 throw new IllegalArgumentException("Clauses must have same field.");
68 this.clauses.add(clause);
70 this.collectPayloads = collectPayloads;
72 this.inOrder = inOrder;
75 /** Return the clauses whose spans are matched. */
76 public SpanQuery[] getClauses() {
77 return clauses.toArray(new SpanQuery[clauses.size()]);
80 /** Return the maximum number of intervening unmatched positions permitted.*/
81 public int getSlop() { return slop; }
83 /** Return true if matches are required to be in-order.*/
84 public boolean isInOrder() { return inOrder; }
87 public String getField() { return field; }
90 public void extractTerms(Set<Term> terms) {
91 for (final SpanQuery clause : clauses) {
92 clause.extractTerms(terms);
98 public String toString(String field) {
99 StringBuilder buffer = new StringBuilder();
100 buffer.append("spanNear([");
101 Iterator<SpanQuery> i = clauses.iterator();
102 while (i.hasNext()) {
103 SpanQuery clause = i.next();
104 buffer.append(clause.toString(field));
109 buffer.append("], ");
112 buffer.append(inOrder);
114 buffer.append(ToStringUtils.boost(getBoost()));
115 return buffer.toString();
119 public Spans getSpans(final IndexReader reader) throws IOException {
120 if (clauses.size() == 0) // optimize 0-clause case
121 return new SpanOrQuery(getClauses()).getSpans(reader);
123 if (clauses.size() == 1) // optimize 1-clause case
124 return clauses.get(0).getSpans(reader);
127 ? (Spans) new NearSpansOrdered(this, reader, collectPayloads)
128 : (Spans) new NearSpansUnordered(this, reader);
132 public Query rewrite(IndexReader reader) throws IOException {
133 SpanNearQuery clone = null;
134 for (int i = 0 ; i < clauses.size(); i++) {
135 SpanQuery c = clauses.get(i);
136 SpanQuery query = (SpanQuery) c.rewrite(reader);
137 if (query != c) { // clause rewrote: must clone
139 clone = (SpanNearQuery) this.clone();
140 clone.clauses.set(i,query);
144 return clone; // some clauses rewrote
146 return this; // no clauses rewrote
151 public Object clone() {
152 int sz = clauses.size();
153 SpanQuery[] newClauses = new SpanQuery[sz];
155 for (int i = 0; i < sz; i++) {
156 newClauses[i] = (SpanQuery) clauses.get(i).clone();
158 SpanNearQuery spanNearQuery = new SpanNearQuery(newClauses, slop, inOrder);
159 spanNearQuery.setBoost(getBoost());
160 return spanNearQuery;
163 /** Returns true iff <code>o</code> is equal to this. */
165 public boolean equals(Object o) {
166 if (this == o) return true;
167 if (!(o instanceof SpanNearQuery)) return false;
169 final SpanNearQuery spanNearQuery = (SpanNearQuery) o;
171 if (inOrder != spanNearQuery.inOrder) return false;
172 if (slop != spanNearQuery.slop) return false;
173 if (!clauses.equals(spanNearQuery.clauses)) return false;
175 return getBoost() == spanNearQuery.getBoost();
179 public int hashCode() {
181 result = clauses.hashCode();
182 // Mix bits before folding in things like boost, since it could cancel the
183 // last element of clauses. This particular mix also serves to
184 // differentiate SpanNearQuery hashcodes from others.
185 result ^= (result << 14) | (result >>> 19); // reversible
186 result += Float.floatToRawIntBits(getBoost());
188 result ^= (inOrder ? 0x99AFD3BD : 0);