-
Notifications
You must be signed in to change notification settings - Fork 623
/
MultiTermQuery.cs
367 lines (335 loc) · 16 KB
/
MultiTermQuery.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
namespace Lucene.Net.Search
{
/*
* 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 AttributeSource = Lucene.Net.Util.AttributeSource;
using IndexReader = Lucene.Net.Index.IndexReader;
using Term = Lucene.Net.Index.Term;
using TermContext = Lucene.Net.Index.TermContext;
using Terms = Lucene.Net.Index.Terms;
using TermsEnum = Lucene.Net.Index.TermsEnum;
/// <summary>
/// An abstract <see cref="Query"/> that matches documents
/// containing a subset of terms provided by a
/// <see cref="Index.FilteredTermsEnum"/> enumeration.
///
/// <para/>This query cannot be used directly; you must subclass
/// it and define <see cref="GetTermsEnum(Terms,AttributeSource)"/> to provide a
/// <see cref="Index.FilteredTermsEnum"/> that iterates through the terms to be
/// matched.
///
/// <para/><b>NOTE</b>: if <see cref="MultiTermRewriteMethod"/> is either
/// <see cref="CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE"/> or
/// <see cref="SCORING_BOOLEAN_QUERY_REWRITE"/>, you may encounter a
/// <see cref="BooleanQuery.TooManyClausesException"/> exception during
/// searching, which happens when the number of terms to be
/// searched exceeds
/// <see cref="BooleanQuery.MaxClauseCount"/>. Setting
/// <see cref="MultiTermRewriteMethod"/> to <see cref="CONSTANT_SCORE_FILTER_REWRITE"/>
/// prevents this.
///
/// <para/>The recommended rewrite method is
/// <see cref="CONSTANT_SCORE_AUTO_REWRITE_DEFAULT"/>: it doesn't spend CPU
/// computing unhelpful scores, and it tries to pick the most
/// performant rewrite method given the query. If you
/// need scoring (like <seea cref="FuzzyQuery"/>, use
/// <see cref="TopTermsScoringBooleanQueryRewrite"/> which uses
/// a priority queue to only collect competitive terms
/// and not hit this limitation.
///
/// <para/>Note that QueryParsers.Classic.QueryParser produces
/// <see cref="MultiTermQuery"/>s using
/// <see cref="CONSTANT_SCORE_AUTO_REWRITE_DEFAULT"/> by default.
/// </summary>
public abstract class MultiTermQuery : Query
{
protected internal readonly string m_field;
protected RewriteMethod m_rewriteMethod = CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
/// <summary>
/// Abstract class that defines how the query is rewritten. </summary>
public abstract class RewriteMethod
{
public abstract Query Rewrite(IndexReader reader, MultiTermQuery query);
/// <summary>
/// Returns the <see cref="MultiTermQuery"/>s <see cref="TermsEnum"/> </summary>
/// <seealso cref="MultiTermQuery.GetTermsEnum(Terms, AttributeSource)"/>
protected virtual TermsEnum GetTermsEnum(MultiTermQuery query, Terms terms, AttributeSource atts)
{
return query.GetTermsEnum(terms, atts); // allow RewriteMethod subclasses to pull a TermsEnum from the MTQ
}
}
/// <summary>
/// A rewrite method that first creates a private <see cref="Filter"/>,
/// by visiting each term in sequence and marking all docs
/// for that term. Matching documents are assigned a
/// constant score equal to the query's boost.
///
/// <para/> This method is faster than the <see cref="BooleanQuery"/>
/// rewrite methods when the number of matched terms or
/// matched documents is non-trivial. Also, it will never
/// hit an errant <see cref="BooleanQuery.TooManyClausesException"/>
/// exception.
/// </summary>
/// <seealso cref="MultiTermRewriteMethod"/>
public static readonly RewriteMethod CONSTANT_SCORE_FILTER_REWRITE = new RewriteMethodAnonymousClass();
private class RewriteMethodAnonymousClass : RewriteMethod
{
public RewriteMethodAnonymousClass()
{
}
public override Query Rewrite(IndexReader reader, MultiTermQuery query)
{
Query result = new ConstantScoreQuery(new MultiTermQueryWrapperFilter<MultiTermQuery>(query));
result.Boost = query.Boost;
return result;
}
}
/// <summary>
/// A rewrite method that first translates each term into
/// <see cref="Occur.SHOULD"/> clause in a
/// <see cref="BooleanQuery"/>, and keeps the scores as computed by the
/// query. Note that typically such scores are
/// meaningless to the user, and require non-trivial CPU
/// to compute, so it's almost always better to use
/// <see cref="CONSTANT_SCORE_AUTO_REWRITE_DEFAULT"/> instead.
///
/// <para/><b>NOTE</b>: this rewrite method will hit
/// <see cref="BooleanQuery.TooManyClausesException"/> if the number of terms
/// exceeds <see cref="BooleanQuery.MaxClauseCount"/>.
/// </summary>
/// <seealso cref="MultiTermRewriteMethod"/>
public static readonly RewriteMethod SCORING_BOOLEAN_QUERY_REWRITE = ScoringRewrite<MultiTermQuery>.SCORING_BOOLEAN_QUERY_REWRITE;
/// <summary>
/// Like <see cref="SCORING_BOOLEAN_QUERY_REWRITE"/> except
/// scores are not computed. Instead, each matching
/// document receives a constant score equal to the
/// query's boost.
///
/// <para/><b>NOTE</b>: this rewrite method will hit
/// <see cref="BooleanQuery.TooManyClausesException"/> if the number of terms
/// exceeds <see cref="BooleanQuery.MaxClauseCount"/>.
/// </summary>
/// <seealso cref="MultiTermRewriteMethod"/>
public static readonly RewriteMethod CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE = ScoringRewrite<MultiTermQuery>.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE;
/// <summary>
/// A rewrite method that first translates each term into
/// <see cref="Occur.SHOULD"/> clause in a <see cref="BooleanQuery"/>, and keeps the
/// scores as computed by the query.
///
/// <para/>
/// This rewrite method only uses the top scoring terms so it will not overflow
/// the boolean max clause count. It is the default rewrite method for
/// <see cref="FuzzyQuery"/>.
/// </summary>
/// <seealso cref="MultiTermRewriteMethod"/>
public sealed class TopTermsScoringBooleanQueryRewrite : TopTermsRewrite<BooleanQuery>
{
/// <summary>
/// Create a <see cref="TopTermsScoringBooleanQueryRewrite"/> for
/// at most <paramref name="size"/> terms.
/// <para/>
/// NOTE: if <see cref="BooleanQuery.MaxClauseCount"/> is smaller than
/// <paramref name="size"/>, then it will be used instead.
/// </summary>
public TopTermsScoringBooleanQueryRewrite(int size)
: base(size)
{
}
protected override int MaxSize => BooleanQuery.MaxClauseCount;
protected override BooleanQuery GetTopLevelQuery()
{
return new BooleanQuery(true);
}
protected override void AddClause(BooleanQuery topLevel, Term term, int docCount, float boost, TermContext states)
{
TermQuery tq = new TermQuery(term, states);
tq.Boost = boost;
topLevel.Add(tq, Occur.SHOULD);
}
}
/// <summary>
/// A rewrite method that first translates each term into
/// <see cref="Occur.SHOULD"/> clause in a <see cref="BooleanQuery"/>, but the scores
/// are only computed as the boost.
/// <para/>
/// This rewrite method only uses the top scoring terms so it will not overflow
/// the boolean max clause count.
/// </summary>
/// <seealso cref="MultiTermRewriteMethod"/>
public sealed class TopTermsBoostOnlyBooleanQueryRewrite : TopTermsRewrite<BooleanQuery>
{
/// <summary>
/// Create a <see cref="TopTermsBoostOnlyBooleanQueryRewrite"/> for
/// at most <paramref name="size"/> terms.
/// <para/>
/// NOTE: if <see cref="BooleanQuery.MaxClauseCount"/> is smaller than
/// <paramref name="size"/>, then it will be used instead.
/// </summary>
public TopTermsBoostOnlyBooleanQueryRewrite(int size)
: base(size)
{
}
protected override int MaxSize => BooleanQuery.MaxClauseCount;
protected override BooleanQuery GetTopLevelQuery()
{
return new BooleanQuery(true);
}
protected override void AddClause(BooleanQuery topLevel, Term term, int docFreq, float boost, TermContext states)
{
Query q = new ConstantScoreQuery(new TermQuery(term, states));
q.Boost = boost;
topLevel.Add(q, Occur.SHOULD);
}
}
// LUCENENET specific - just use the non-nested class directly. This is
// confusing in .NET.
// /// <summary>
// /// A rewrite method that tries to pick the best
// /// constant-score rewrite method based on term and
// /// document counts from the query. If both the number of
// /// terms and documents is small enough, then
// /// <see cref="CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE"/> is used.
// /// Otherwise, <see cref="CONSTANT_SCORE_FILTER_REWRITE"/> is
// /// used.
// /// </summary>
// public class ConstantScoreAutoRewrite : Lucene.Net.Search.ConstantScoreAutoRewrite
// {
// }
/// <summary>
/// Read-only default instance of
/// <see cref="ConstantScoreAutoRewrite"/>, with
/// <see cref="Search.ConstantScoreAutoRewrite.TermCountCutoff"/> set to
/// <see cref="Search.ConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFF"/>
/// and
/// <see cref="Search.ConstantScoreAutoRewrite.DocCountPercent"/> set to
/// <see cref="Search.ConstantScoreAutoRewrite.DEFAULT_DOC_COUNT_PERCENT"/>.
/// Note that you cannot alter the configuration of this
/// instance; you'll need to create a private instance
/// instead.
/// </summary>
public static readonly RewriteMethod CONSTANT_SCORE_AUTO_REWRITE_DEFAULT = new ConstantScoreAutoRewriteAnonymousClass();
private class ConstantScoreAutoRewriteAnonymousClass : ConstantScoreAutoRewrite
{
public ConstantScoreAutoRewriteAnonymousClass()
{
}
public override int TermCountCutoff
{
get => base.TermCountCutoff; // LUCENENET specific - adding getter for API consistency check
set => throw UnsupportedOperationException.Create("Please create a private instance");
}
public override double DocCountPercent
{
get => base.DocCountPercent; // LUCENENET specific - adding getter for API consistency check
set => throw UnsupportedOperationException.Create("Please create a private instance");
}
}
/// <summary>
/// Constructs a query matching terms that cannot be represented with a single
/// <see cref="Term"/>.
/// </summary>
protected MultiTermQuery(string field) // LUCENENET: CA1012: Abstract types should not have constructors (marked protected)
{
this.m_field = field ?? throw new ArgumentNullException(nameof(field), "field must not be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
}
/// <summary>
/// Returns the field name for this query </summary>
public string Field => m_field;
/// <summary>
/// Construct the enumeration to be used, expanding the
/// pattern term. this method should only be called if
/// the field exists (ie, implementations can assume the
/// field does exist). this method should not return null
/// (should instead return <see cref="TermsEnum.EMPTY"/> if no
/// terms match). The <see cref="TermsEnum"/> must already be
/// positioned to the first matching term.
/// The given <see cref="AttributeSource"/> is passed by the <see cref="RewriteMethod"/> to
/// provide attributes, the rewrite method uses to inform about e.g. maximum competitive boosts.
/// this is currently only used by <see cref="TopTermsRewrite{Q}"/>.
/// </summary>
protected abstract TermsEnum GetTermsEnum(Terms terms, AttributeSource atts);
/// <summary>
/// Convenience method, if no attributes are needed:
/// this simply passes empty attributes and is equal to:
/// <code>GetTermsEnum(terms, new AttributeSource())</code>
/// </summary>
public TermsEnum GetTermsEnum(Terms terms)
{
return GetTermsEnum(terms, new AttributeSource());
}
/// <summary>
/// To rewrite to a simpler form, instead return a simpler
/// enum from <see cref="GetTermsEnum(Terms, AttributeSource)"/>. For example,
/// to rewrite to a single term, return a <see cref="Index.SingleTermsEnum"/>.
/// </summary>
public override sealed Query Rewrite(IndexReader reader)
{
return m_rewriteMethod.Rewrite(reader, this);
}
// LUCENENET NOTE: Renamed from RewriteMethod to prevent a naming
// conflict with the RewriteMethod class. MultiTermRewriteMethod is consistent
// with the name used in QueryParserBase.
/// <summary>
/// Gets or Sets the rewrite method to be used when executing the
/// query. You can use one of the four core methods, or
/// implement your own subclass of <see cref="RewriteMethod"/>.
/// </summary>
public virtual RewriteMethod MultiTermRewriteMethod
{
get => m_rewriteMethod;
set => m_rewriteMethod = value;
}
public override int GetHashCode()
{
const int prime = 31;
int result = 1;
result = prime * result + J2N.BitConversion.SingleToInt32Bits(Boost);
result = prime * result + m_rewriteMethod.GetHashCode();
if (m_field != null)
{
result = prime * result + m_field.GetHashCode();
}
return result;
}
public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.GetType() != obj.GetType())
{
return false;
}
MultiTermQuery other = (MultiTermQuery)obj;
if (J2N.BitConversion.SingleToInt32Bits(Boost) != J2N.BitConversion.SingleToInt32Bits(other.Boost))
{
return false;
}
if (!m_rewriteMethod.Equals(other.m_rewriteMethod))
{
return false;
}
return (other.m_field == null ? m_field == null : other.m_field.Equals(m_field, StringComparison.Ordinal));
}
}
}