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.
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.
30 public class SpanNearQuery
: SpanQuery
32 private System
.Collections
.ArrayList clauses
;
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>.
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
];
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
);
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()
79 /// <summary>Return true if matches are required to be in-order.</summary>
80 public virtual bool IsInOrder()
85 public override System
.String
GetField()
90 public override System
.Collections
.ICollection
GetTerms()
92 System
.Collections
.ArrayList terms
= new System
.Collections
.ArrayList();
93 System
.Collections
.IEnumerator i
= clauses
.GetEnumerator();
96 SpanQuery clause
= (SpanQuery
) i
.Current
;
97 terms
.AddRange(clause
.GetTerms());
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();
109 SpanQuery clause
= (SpanQuery
) i
.Current
;
110 buffer
.Append(clause
.ToString(field
));
116 buffer
.Append("], ");
119 buffer
.Append(inOrder
);
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
);
147 // clause rewrote: must clone
149 clone
= (SpanNearQuery
) this.Clone();
150 clone
.clauses
[i
] = query
;
155 return clone
; // some clauses rewrote
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
)
168 if (o
== null || GetType() != o
.GetType())
171 SpanNearQuery spanNearQuery
= (SpanNearQuery
) o
;
173 if (inOrder
!= spanNearQuery
.inOrder
)
175 if (slop
!= spanNearQuery
.slop
)
177 if (!clauses
.Equals(spanNearQuery
.clauses
))
179 if (!field
.Equals(spanNearQuery
.field
))
182 return GetBoost() == spanNearQuery
.GetBoost();
185 public override int GetHashCode()
188 result
= clauses
.GetHashCode();
190 result
+= (inOrder
?1:0);
191 result ^
= field
.GetHashCode();