cvsimport
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanNearQuery.cs
blob0a1bd0bf605f14e1eec1cf43ba14fe540edb36cf
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Query = Lucene.Net.Search.Query;
20 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
22 namespace Lucene.Net.Search.Spans
25 /// <summary>Matches spans which are near one another. One can specify <i>slop</i>, the
26 /// maximum number of intervening unmatched positions, as well as whether
27 /// matches are required to be in-order.
28 /// </summary>
29 [Serializable]
30 public class SpanNearQuery : SpanQuery
32 private System.Collections.ArrayList clauses;
33 private int slop;
34 private bool inOrder;
36 private System.String field;
38 /// <summary>Construct a SpanNearQuery. Matches spans matching a span from each
39 /// clause, with up to <code>slop</code> total unmatched positions between
40 /// them. * When <code>inOrder</code> is true, the spans from each clause
41 /// must be * ordered as in <code>clauses</code>.
42 /// </summary>
43 public SpanNearQuery(SpanQuery[] clauses, int slop, bool inOrder)
46 // copy clauses array into an ArrayList
47 this.clauses = new System.Collections.ArrayList(clauses.Length);
48 for (int i = 0; i < clauses.Length; i++)
50 SpanQuery clause = clauses[i];
51 if (i == 0)
53 // check field
54 field = clause.GetField();
56 else if (!clause.GetField().Equals(field))
58 throw new System.ArgumentException("Clauses must have same field.");
60 this.clauses.Add(clause);
63 this.slop = slop;
64 this.inOrder = inOrder;
67 /// <summary>Return the clauses whose spans are matched. </summary>
68 public virtual SpanQuery[] GetClauses()
70 return (SpanQuery[]) clauses.ToArray(typeof(SpanQuery));
73 /// <summary>Return the maximum number of intervening unmatched positions permitted.</summary>
74 public virtual int GetSlop()
76 return slop;
79 /// <summary>Return true if matches are required to be in-order.</summary>
80 public virtual bool IsInOrder()
82 return inOrder;
85 public override System.String GetField()
87 return field;
90 public override System.Collections.ICollection GetTerms()
92 System.Collections.ArrayList terms = new System.Collections.ArrayList();
93 System.Collections.IEnumerator i = clauses.GetEnumerator();
94 while (i.MoveNext())
96 SpanQuery clause = (SpanQuery) i.Current;
97 terms.AddRange(clause.GetTerms());
99 return terms;
102 public override System.String ToString(System.String field)
104 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
105 buffer.Append("spanNear([");
106 System.Collections.IEnumerator i = clauses.GetEnumerator();
107 while (i.MoveNext())
109 SpanQuery clause = (SpanQuery) i.Current;
110 buffer.Append(clause.ToString(field));
111 if (i.MoveNext())
113 buffer.Append(", ");
116 buffer.Append("], ");
117 buffer.Append(slop);
118 buffer.Append(", ");
119 buffer.Append(inOrder);
120 buffer.Append(")");
121 buffer.Append(ToStringUtils.Boost(GetBoost()));
122 return buffer.ToString();
125 public override Spans GetSpans(IndexReader reader)
127 if (clauses.Count == 0)
128 // optimize 0-clause case
129 return new SpanOrQuery(GetClauses()).GetSpans(reader);
131 if (clauses.Count == 1)
132 // optimize 1-clause case
133 return ((SpanQuery) clauses[0]).GetSpans(reader);
135 return new NearSpans(this, reader);
138 public override Query Rewrite(IndexReader reader)
140 SpanNearQuery clone = null;
141 for (int i = 0; i < clauses.Count; i++)
143 SpanQuery c = (SpanQuery) clauses[i];
144 SpanQuery query = (SpanQuery) c.Rewrite(reader);
145 if (query != c)
147 // clause rewrote: must clone
148 if (clone == null)
149 clone = (SpanNearQuery) this.Clone();
150 clone.clauses[i] = query;
153 if (clone != null)
155 return clone; // some clauses rewrote
157 else
159 return this; // no clauses rewrote
163 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
164 public override bool Equals(System.Object o)
166 if (this == o)
167 return true;
168 if (o == null || GetType() != o.GetType())
169 return false;
171 SpanNearQuery spanNearQuery = (SpanNearQuery) o;
173 if (inOrder != spanNearQuery.inOrder)
174 return false;
175 if (slop != spanNearQuery.slop)
176 return false;
177 if (!clauses.Equals(spanNearQuery.clauses))
178 return false;
179 if (!field.Equals(spanNearQuery.field))
180 return false;
182 return GetBoost() == spanNearQuery.GetBoost();
185 public override int GetHashCode()
187 int result;
188 result = clauses.GetHashCode();
189 result += slop * 29;
190 result += (inOrder?1:0);
191 result ^= field.GetHashCode();
192 return result;