2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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.
26 public class SpanNearQuery
:SpanQuery
28 private System
.Collections
.ArrayList clauses
;
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>.
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
];
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
);
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()
75 /// <summary>Return true if matches are required to be in-order.</summary>
76 public virtual bool IsInOrder()
81 public override System
.String
GetField()
86 public override System
.Collections
.ICollection
GetTerms()
88 System
.Collections
.ArrayList terms
= new System
.Collections
.ArrayList();
89 System
.Collections
.IEnumerator i
= clauses
.GetEnumerator();
92 SpanQuery clause
= (SpanQuery
) i
.Current
;
93 //{{}}// _SupportClass.ICollectionSupport.AddAll(terms, clause.GetTerms()); // {{Aroush}}
94 terms
.AddRange(clause
.GetTerms());
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();
106 SpanQuery clause
= (SpanQuery
) i
.Current
;
107 buffer
.Append(clause
.ToString(field
));
113 buffer
.Append("], ");
116 buffer
.Append(inOrder
);
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
);