pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / spans / SpanNearQuery.java
1 package org.apache.lucene.search.spans;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21
22
23 import java.util.List;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.Set;
27
28
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;
33
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;
39   protected int slop;
40   protected boolean inOrder;
41
42   protected String field;
43   private boolean collectPayloads;
44
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
52    * */
53   public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder) {
54     this(clauses, slop, inOrder, true);     
55   }
56   
57   public SpanNearQuery(SpanQuery[] clauses, int slop, boolean inOrder, boolean collectPayloads) {
58
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.");
67       }
68       this.clauses.add(clause);
69     }
70     this.collectPayloads = collectPayloads;
71     this.slop = slop;
72     this.inOrder = inOrder;
73   }
74
75   /** Return the clauses whose spans are matched. */
76   public SpanQuery[] getClauses() {
77     return clauses.toArray(new SpanQuery[clauses.size()]);
78   }
79
80   /** Return the maximum number of intervening unmatched positions permitted.*/
81   public int getSlop() { return slop; }
82
83   /** Return true if matches are required to be in-order.*/
84   public boolean isInOrder() { return inOrder; }
85
86   @Override
87   public String getField() { return field; }
88   
89   @Override
90   public void extractTerms(Set<Term> terms) {
91             for (final SpanQuery clause : clauses) {
92               clause.extractTerms(terms);
93             }
94   }  
95   
96
97   @Override
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));
105       if (i.hasNext()) {
106         buffer.append(", ");
107       }
108     }
109     buffer.append("], ");
110     buffer.append(slop);
111     buffer.append(", ");
112     buffer.append(inOrder);
113     buffer.append(")");
114     buffer.append(ToStringUtils.boost(getBoost()));
115     return buffer.toString();
116   }
117
118   @Override
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);
122
123     if (clauses.size() == 1)                      // optimize 1-clause case
124       return clauses.get(0).getSpans(reader);
125
126     return inOrder
127             ? (Spans) new NearSpansOrdered(this, reader, collectPayloads)
128             : (Spans) new NearSpansUnordered(this, reader);
129   }
130
131   @Override
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
138         if (clone == null)
139           clone = (SpanNearQuery) this.clone();
140         clone.clauses.set(i,query);
141       }
142     }
143     if (clone != null) {
144       return clone;                        // some clauses rewrote
145     } else {
146       return this;                         // no clauses rewrote
147     }
148   }
149   
150   @Override
151   public Object clone() {
152     int sz = clauses.size();
153     SpanQuery[] newClauses = new SpanQuery[sz];
154
155     for (int i = 0; i < sz; i++) {
156       newClauses[i] = (SpanQuery) clauses.get(i).clone();
157     }
158     SpanNearQuery spanNearQuery = new SpanNearQuery(newClauses, slop, inOrder);
159     spanNearQuery.setBoost(getBoost());
160     return spanNearQuery;
161   }
162
163   /** Returns true iff <code>o</code> is equal to this. */
164   @Override
165   public boolean equals(Object o) {
166     if (this == o) return true;
167     if (!(o instanceof SpanNearQuery)) return false;
168
169     final SpanNearQuery spanNearQuery = (SpanNearQuery) o;
170
171     if (inOrder != spanNearQuery.inOrder) return false;
172     if (slop != spanNearQuery.slop) return false;
173     if (!clauses.equals(spanNearQuery.clauses)) return false;
174
175     return getBoost() == spanNearQuery.getBoost();
176   }
177
178   @Override
179   public int hashCode() {
180     int result;
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());
187     result += slop;
188     result ^= (inOrder ? 0x99AFD3BD : 0);
189     return result;
190   }
191 }