Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanNearQuery.cs
bloba03f3d509ddd8cfd63e098ffe18612794c813475
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.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 namespace Lucene.Net.Search.Spans
21 /// <summary>Matches spans which are near one another. One can specify <i>slop</i>, the
22 /// maximum number of intervening unmatched positions, as well as whether
23 /// matches are required to be in-order.
24 /// </summary>
25 [Serializable]
26 public class SpanNearQuery:SpanQuery
28 private System.Collections.ArrayList clauses;
29 private int slop;
30 private bool inOrder;
32 private System.String field;
34 /// <summary>Construct a SpanNearQuery. Matches spans matching a span from each
35 /// clause, with up to <code>slop</code> total unmatched positions between
36 /// them. * When <code>inOrder</code> is true, the spans from each clause
37 /// must be * ordered as in <code>clauses</code>.
38 /// </summary>
39 public SpanNearQuery(SpanQuery[] clauses, int slop, bool inOrder)
42 // copy clauses array into an ArrayList
43 this.clauses = new System.Collections.ArrayList(clauses.Length);
44 for (int i = 0; i < clauses.Length; i++)
46 SpanQuery clause = clauses[i];
47 if (i == 0)
49 // check Field
50 field = clause.GetField();
52 else if (!clause.GetField().Equals(field))
54 throw new System.ArgumentException("Clauses must have same Field.");
56 this.clauses.Add(clause);
59 this.slop = slop;
60 this.inOrder = inOrder;
63 /// <summary>Return the clauses whose spans are matched. </summary>
64 public virtual SpanQuery[] GetClauses()
66 return (SpanQuery[]) clauses.ToArray(typeof(SpanQuery));
69 /// <summary>Return the maximum number of intervening unmatched positions permitted.</summary>
70 public virtual int GetSlop()
72 return slop;
75 /// <summary>Return true if matches are required to be in-order.</summary>
76 public virtual bool IsInOrder()
78 return inOrder;
81 public override System.String GetField()
83 return field;
86 public override System.Collections.ICollection GetTerms()
88 System.Collections.ArrayList terms = new System.Collections.ArrayList();
89 System.Collections.IEnumerator i = clauses.GetEnumerator();
90 while (i.MoveNext())
92 SpanQuery clause = (SpanQuery) i.Current;
93 //{{}}// _SupportClass.ICollectionSupport.AddAll(terms, clause.GetTerms()); // {{Aroush}}
94 terms.AddRange(clause.GetTerms());
96 return terms;
99 public override System.String ToString(System.String field)
101 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
102 buffer.Append("spanNear([");
103 System.Collections.IEnumerator i = clauses.GetEnumerator();
104 while (i.MoveNext())
106 SpanQuery clause = (SpanQuery) i.Current;
107 buffer.Append(clause.ToString(field));
108 if (i.MoveNext())
110 buffer.Append(", ");
113 buffer.Append("], ");
114 buffer.Append(slop);
115 buffer.Append(", ");
116 buffer.Append(inOrder);
117 buffer.Append(")");
118 return buffer.ToString();
121 public override Spans GetSpans(IndexReader reader)
123 if (clauses.Count == 0)
124 // optimize 0-clause case
125 return new SpanOrQuery(GetClauses()).GetSpans(reader);
127 if (clauses.Count == 1)
128 // optimize 1-clause case
129 return ((SpanQuery) clauses[0]).GetSpans(reader);
131 return new NearSpans(this, reader);