-
Notifications
You must be signed in to change notification settings - Fork 624
/
SpanNotQuery.cs
289 lines (247 loc) · 10.8 KB
/
SpanNotQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using J2N.Numerics;
using System;
using System.Collections.Generic;
using System.Text;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Search.Spans
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using AtomicReaderContext = Lucene.Net.Index.AtomicReaderContext;
using IBits = Lucene.Net.Util.IBits;
using IndexReader = Lucene.Net.Index.IndexReader;
using Term = Lucene.Net.Index.Term;
using TermContext = Lucene.Net.Index.TermContext;
using ToStringUtils = Lucene.Net.Util.ToStringUtils;
/// <summary>
/// Removes matches which overlap with another <see cref="SpanQuery"/> or
/// within a x tokens before or y tokens after another <see cref="SpanQuery"/>.
/// </summary>
public class SpanNotQuery : SpanQuery // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
private SpanQuery include;
private SpanQuery exclude;
private readonly int pre;
private readonly int post;
/// <summary>
/// Construct a <see cref="SpanNotQuery"/> matching spans from <paramref name="include"/> which
/// have no overlap with spans from <paramref name="exclude"/>.
/// </summary>
public SpanNotQuery(SpanQuery include, SpanQuery exclude)
: this(include, exclude, 0, 0)
{
}
/// <summary>
/// Construct a <see cref="SpanNotQuery"/> matching spans from <paramref name="include"/> which
/// have no overlap with spans from <paramref name="exclude"/> within
/// <paramref name="dist"/> tokens of <paramref name="include"/>.
/// </summary>
public SpanNotQuery(SpanQuery include, SpanQuery exclude, int dist)
: this(include, exclude, dist, dist)
{
}
/// <summary>
/// Construct a <see cref="SpanNotQuery"/> matching spans from <paramref name="include"/> which
/// have no overlap with spans from <paramref name="exclude"/> within
/// <paramref name="pre"/> tokens before or <paramref name="post"/> tokens of <paramref name="include"/>.
/// </summary>
public SpanNotQuery(SpanQuery include, SpanQuery exclude, int pre, int post)
{
this.include = include;
this.exclude = exclude;
this.pre = (pre >= 0) ? pre : 0;
this.post = (post >= 0) ? post : 0;
if (include.Field != null && exclude.Field != null && !include.Field.Equals(exclude.Field, StringComparison.Ordinal))
{
throw new ArgumentException("Clauses must have same field.");
}
}
/// <summary>
/// Return the <see cref="SpanQuery"/> whose matches are filtered. </summary>
public virtual SpanQuery Include => include;
/// <summary>
/// Return the <see cref="SpanQuery"/> whose matches must not overlap those returned. </summary>
public virtual SpanQuery Exclude => exclude;
public override string Field => include.Field;
public override void ExtractTerms(ISet<Term> terms)
{
include.ExtractTerms(terms);
}
public override string ToString(string field)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("spanNot(");
buffer.Append(include.ToString(field));
buffer.Append(", ");
buffer.Append(exclude.ToString(field));
buffer.Append(", ");
buffer.Append(Convert.ToString(pre));
buffer.Append(", ");
buffer.Append(Convert.ToString(post));
buffer.Append(")");
buffer.Append(ToStringUtils.Boost(Boost));
return buffer.ToString();
}
public override object Clone()
{
SpanNotQuery spanNotQuery = new SpanNotQuery((SpanQuery)include.Clone(), (SpanQuery)exclude.Clone(), pre, post);
spanNotQuery.Boost = Boost;
return spanNotQuery;
}
public override Spans GetSpans(AtomicReaderContext context, IBits acceptDocs, IDictionary<Term, TermContext> termContexts)
{
return new SpansAnonymousClass(this, context, acceptDocs, termContexts);
}
private class SpansAnonymousClass : Spans
{
private readonly SpanNotQuery outerInstance;
public SpansAnonymousClass(SpanNotQuery outerInstance, AtomicReaderContext context, IBits acceptDocs, IDictionary<Term, TermContext> termContexts)
{
this.outerInstance = outerInstance;
includeSpans = outerInstance.include.GetSpans(context, acceptDocs, termContexts);
moreInclude = true;
excludeSpans = outerInstance.exclude.GetSpans(context, acceptDocs, termContexts);
moreExclude = excludeSpans.MoveNext();
}
private readonly Spans includeSpans; // LUCENENET: marked readonly
private bool moreInclude;
private readonly Spans excludeSpans; // LUCENENET: marked readonly
private bool moreExclude;
public override bool MoveNext()
{
if (moreInclude) // move to next include
{
moreInclude = includeSpans.MoveNext();
}
while (moreInclude && moreExclude)
{
if (includeSpans.Doc > excludeSpans.Doc) // skip exclude
{
moreExclude = excludeSpans.SkipTo(includeSpans.Doc);
}
while (moreExclude && includeSpans.Doc == excludeSpans.Doc && excludeSpans.End <= includeSpans.Start - outerInstance.pre) // while exclude is before
{
moreExclude = excludeSpans.MoveNext(); // increment exclude
}
if (!moreExclude || includeSpans.Doc != excludeSpans.Doc || includeSpans.End + outerInstance.post <= excludeSpans.Start) // if no intersection
{
break; // we found a match
}
moreInclude = includeSpans.MoveNext(); // intersected: keep scanning
}
return moreInclude;
}
public override bool SkipTo(int target)
{
if (moreInclude) // skip include
{
moreInclude = includeSpans.SkipTo(target);
}
if (!moreInclude)
{
return false;
}
if (moreExclude && includeSpans.Doc > excludeSpans.Doc) // skip exclude
{
moreExclude = excludeSpans.SkipTo(includeSpans.Doc);
}
while (moreExclude && includeSpans.Doc == excludeSpans.Doc && excludeSpans.End <= includeSpans.Start - outerInstance.pre) // while exclude is before
{
moreExclude = excludeSpans.MoveNext(); // increment exclude
}
if (!moreExclude || includeSpans.Doc != excludeSpans.Doc || includeSpans.End + outerInstance.post <= excludeSpans.Start) // if no intersection
{
return true; // we found a match
}
return MoveNext(); // scan to next match
}
public override int Doc => includeSpans.Doc;
public override int Start => includeSpans.Start;
public override int End => includeSpans.End; // TODO: Remove warning after API has been finalized
public override ICollection<byte[]> GetPayload()
{
JCG.List<byte[]> result = null;
if (includeSpans.IsPayloadAvailable)
{
result = new JCG.List<byte[]>(includeSpans.GetPayload());
}
return result;
}
// TODO: Remove warning after API has been finalized
public override bool IsPayloadAvailable => includeSpans.IsPayloadAvailable;
public override long GetCost()
{
return includeSpans.GetCost();
}
public override string ToString()
{
return "spans(" + outerInstance.ToString() + ")";
}
}
public override Query Rewrite(IndexReader reader)
{
SpanNotQuery clone = null;
var rewrittenInclude = (SpanQuery)include.Rewrite(reader);
if (rewrittenInclude != include)
{
clone = (SpanNotQuery)this.Clone();
clone.include = rewrittenInclude;
}
var rewrittenExclude = (SpanQuery)exclude.Rewrite(reader);
if (rewrittenExclude != exclude)
{
if (clone is null)
{
clone = (SpanNotQuery)this.Clone();
}
clone.exclude = rewrittenExclude;
}
if (clone != null)
{
return clone; // some clauses rewrote
}
else
{
return this; // no clauses rewrote
}
}
/// <summary>
/// Returns <c>true</c> if <paramref name="o"/> is equal to this. </summary>
public override bool Equals(object o)
{
if (!base.Equals(o))
{
return false;
}
SpanNotQuery other = (SpanNotQuery)o;
return this.include.Equals(other.include) && this.exclude.Equals(other.exclude) && this.pre == other.pre && this.post == other.post;
}
public override int GetHashCode()
{
int h = base.GetHashCode();
h = BitOperation.RotateLeft(h, 1);
h ^= include.GetHashCode();
h = BitOperation.RotateLeft(h, 1);
h ^= exclude.GetHashCode();
h = BitOperation.RotateLeft(h, 1);
h ^= pre;
h = BitOperation.RotateLeft(h, 1);
h ^= post;
return h;
}
}
}