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;
22 import java.util.List;
23 import java.util.Collection;
24 import java.util.ArrayList;
25 import java.util.Iterator;
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.Term;
30 import org.apache.lucene.util.PriorityQueue;
31 import org.apache.lucene.util.ToStringUtils;
32 import org.apache.lucene.search.Query;
34 /** Matches the union of its clauses.*/
35 public class SpanOrQuery extends SpanQuery implements Cloneable {
36 private List<SpanQuery> clauses;
39 /** Construct a SpanOrQuery merging the provided clauses. */
40 public SpanOrQuery(SpanQuery... clauses) {
42 // copy clauses array into an ArrayList
43 this.clauses = new ArrayList<SpanQuery>(clauses.length);
44 for (int i = 0; i < clauses.length; i++) {
45 addClause(clauses[i]);
49 /** Adds a clause to this query */
50 public final void addClause(SpanQuery clause) {
52 field = clause.getField();
53 } else if (!clause.getField().equals(field)) {
54 throw new IllegalArgumentException("Clauses must have same field.");
56 this.clauses.add(clause);
59 /** Return the clauses whose spans are matched. */
60 public SpanQuery[] getClauses() {
61 return clauses.toArray(new SpanQuery[clauses.size()]);
65 public String getField() { return field; }
68 public void extractTerms(Set<Term> terms) {
69 for(final SpanQuery clause: clauses) {
70 clause.extractTerms(terms);
75 public Object clone() {
76 int sz = clauses.size();
77 SpanQuery[] newClauses = new SpanQuery[sz];
79 for (int i = 0; i < sz; i++) {
80 newClauses[i] = (SpanQuery) clauses.get(i).clone();
82 SpanOrQuery soq = new SpanOrQuery(newClauses);
83 soq.setBoost(getBoost());
88 public Query rewrite(IndexReader reader) throws IOException {
89 SpanOrQuery clone = null;
90 for (int i = 0 ; i < clauses.size(); i++) {
91 SpanQuery c = clauses.get(i);
92 SpanQuery query = (SpanQuery) c.rewrite(reader);
93 if (query != c) { // clause rewrote: must clone
95 clone = (SpanOrQuery) this.clone();
96 clone.clauses.set(i,query);
100 return clone; // some clauses rewrote
102 return this; // no clauses rewrote
107 public String toString(String field) {
108 StringBuilder buffer = new StringBuilder();
109 buffer.append("spanOr([");
110 Iterator<SpanQuery> i = clauses.iterator();
111 while (i.hasNext()) {
112 SpanQuery clause = i.next();
113 buffer.append(clause.toString(field));
119 buffer.append(ToStringUtils.boost(getBoost()));
120 return buffer.toString();
124 public boolean equals(Object o) {
125 if (this == o) return true;
126 if (o == null || getClass() != o.getClass()) return false;
128 final SpanOrQuery that = (SpanOrQuery) o;
130 if (!clauses.equals(that.clauses)) return false;
131 if (!clauses.isEmpty() && !field.equals(that.field)) return false;
133 return getBoost() == that.getBoost();
137 public int hashCode() {
138 int h = clauses.hashCode();
139 h ^= (h << 10) | (h >>> 23);
140 h ^= Float.floatToRawIntBits(getBoost());
145 private class SpanQueue extends PriorityQueue<Spans> {
146 public SpanQueue(int size) {
151 protected final boolean lessThan(Spans spans1, Spans spans2) {
152 if (spans1.doc() == spans2.doc()) {
153 if (spans1.start() == spans2.start()) {
154 return spans1.end() < spans2.end();
156 return spans1.start() < spans2.start();
159 return spans1.doc() < spans2.doc();
165 public Spans getSpans(final IndexReader reader) throws IOException {
166 if (clauses.size() == 1) // optimize 1-clause case
167 return (clauses.get(0)).getSpans(reader);
170 private SpanQueue queue = null;
172 private boolean initSpanQueue(int target) throws IOException {
173 queue = new SpanQueue(clauses.size());
174 Iterator<SpanQuery> i = clauses.iterator();
175 while (i.hasNext()) {
176 Spans spans = i.next().getSpans(reader);
177 if ( ((target == -1) && spans.next())
178 || ((target != -1) && spans.skipTo(target))) {
182 return queue.size() != 0;
186 public boolean next() throws IOException {
188 return initSpanQueue(-1);
191 if (queue.size() == 0) { // all done
195 if (top().next()) { // move to next
200 queue.pop(); // exhausted a clause
201 return queue.size() != 0;
204 private Spans top() { return queue.top(); }
207 public boolean skipTo(int target) throws IOException {
209 return initSpanQueue(target);
212 boolean skipCalled = false;
213 while (queue.size() != 0 && top().doc() < target) {
214 if (top().skipTo(target)) {
223 return queue.size() != 0;
229 public int doc() { return top().doc(); }
231 public int start() { return top().start(); }
233 public int end() { return top().end(); }
236 public Collection<byte[]> getPayload() throws IOException {
237 ArrayList<byte[]> result = null;
238 Spans theTop = top();
239 if (theTop != null && theTop.isPayloadAvailable()) {
240 result = new ArrayList<byte[]>(theTop.getPayload());
246 public boolean isPayloadAvailable() {
248 return top != null && top.isPayloadAvailable();
252 public String toString() {
253 return "spans("+SpanOrQuery.this+")@"+
254 ((queue == null)?"START"
255 :(queue.size()>0?(doc()+":"+start()+"-"+end()):"END"));