-
Notifications
You must be signed in to change notification settings - Fork 623
/
QueryBuilder.cs
489 lines (460 loc) · 21.5 KB
/
QueryBuilder.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
using J2N.Collections.Generic.Extensions;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Util
{
/*
* 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 Analyzer = Lucene.Net.Analysis.Analyzer;
using BooleanQuery = Lucene.Net.Search.BooleanQuery;
using CachingTokenFilter = Lucene.Net.Analysis.CachingTokenFilter;
using MultiPhraseQuery = Lucene.Net.Search.MultiPhraseQuery;
using Occur = Lucene.Net.Search.Occur;
using PhraseQuery = Lucene.Net.Search.PhraseQuery;
using Query = Lucene.Net.Search.Query;
using Term = Lucene.Net.Index.Term;
using TermQuery = Lucene.Net.Search.TermQuery;
using TokenStream = Lucene.Net.Analysis.TokenStream;
/// <summary>
/// Creates queries from the <see cref="Analyzer"/> chain.
/// <para/>
/// Example usage:
/// <code>
/// QueryBuilder builder = new QueryBuilder(analyzer);
/// Query a = builder.CreateBooleanQuery("body", "just a test");
/// Query b = builder.CreatePhraseQuery("body", "another test");
/// Query c = builder.CreateMinShouldMatchQuery("body", "another test", 0.5f);
/// </code>
/// <para/>
/// This can also be used as a subclass for query parsers to make it easier
/// to interact with the analysis chain. Factory methods such as <see cref="NewTermQuery(Term)"/>
/// are provided so that the generated queries can be customized.
/// </summary>
public class QueryBuilder
{
private Analyzer analyzer;
private bool enablePositionIncrements = true;
/// <summary>
/// Creates a new <see cref="QueryBuilder"/> using the given analyzer. </summary>
public QueryBuilder(Analyzer analyzer)
{
this.analyzer = analyzer;
}
/// <summary>
/// Creates a boolean query from the query text.
/// <para/>
/// This is equivalent to <c>CreateBooleanQuery(field, queryText, Occur.SHOULD)</c> </summary>
/// <param name="field"> Field name. </param>
/// <param name="queryText"> Text to be passed to the analyzer. </param>
/// <returns> <see cref="TermQuery"/> or <see cref="BooleanQuery"/>, based on the analysis
/// of <paramref name="queryText"/>. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual Query CreateBooleanQuery(string field, string queryText)
{
return CreateBooleanQuery(field, queryText, Occur.SHOULD);
}
/// <summary>
/// Creates a boolean query from the query text.
/// </summary>
/// <param name="field"> Field name </param>
/// <param name="queryText"> Text to be passed to the analyzer. </param>
/// <param name="operator"> Operator used for clauses between analyzer tokens. </param>
/// <returns> <see cref="TermQuery"/> or <see cref="BooleanQuery"/>, based on the analysis
/// of <paramref name="queryText"/>. </returns>
public virtual Query CreateBooleanQuery(string field, string queryText, Occur @operator)
{
if (@operator != Occur.SHOULD && @operator != Occur.MUST)
{
throw new ArgumentException("invalid operator: only SHOULD or MUST are allowed");
}
return CreateFieldQuery(analyzer, @operator, field, queryText, false, 0);
}
/// <summary>
/// Creates a phrase query from the query text.
/// <para/>
/// This is equivalent to <c>CreatePhraseQuery(field, queryText, 0)</c> </summary>
/// <param name="field"> Field name. </param>
/// <param name="queryText"> Text to be passed to the analyzer. </param>
/// <returns> <see cref="TermQuery"/>, <see cref="BooleanQuery"/>, <see cref="PhraseQuery"/>, or
/// <see cref="MultiPhraseQuery"/>, based on the analysis of <paramref name="queryText"/>. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual Query CreatePhraseQuery(string field, string queryText)
{
return CreatePhraseQuery(field, queryText, 0);
}
/// <summary>
/// Creates a phrase query from the query text.
/// </summary>
/// <param name="field"> Field name. </param>
/// <param name="queryText"> Text to be passed to the analyzer. </param>
/// <param name="phraseSlop"> number of other words permitted between words in query phrase </param>
/// <returns> <see cref="TermQuery"/>, <see cref="BooleanQuery"/>, <see cref="PhraseQuery"/>, or
/// <see cref="MultiPhraseQuery"/>, based on the analysis of <paramref name="queryText"/>. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual Query CreatePhraseQuery(string field, string queryText, int phraseSlop)
{
return CreateFieldQuery(analyzer, Occur.MUST, field, queryText, true, phraseSlop);
}
/// <summary>
/// Creates a minimum-should-match query from the query text.
/// </summary>
/// <param name="field"> Field name. </param>
/// <param name="queryText"> Text to be passed to the analyzer. </param>
/// <param name="fraction"> of query terms <c>[0..1]</c> that should match </param>
/// <returns> <see cref="TermQuery"/> or <see cref="BooleanQuery"/>, based on the analysis
/// of <paramref name="queryText"/>. </returns>
public virtual Query CreateMinShouldMatchQuery(string field, string queryText, float fraction)
{
if (float.IsNaN(fraction) || fraction < 0 || fraction > 1)
{
throw new ArgumentException("fraction should be >= 0 and <= 1");
}
// TODO: wierd that BQ equals/rewrite/scorer doesn't handle this?
if (fraction == 1)
{
return CreateBooleanQuery(field, queryText, Occur.MUST);
}
Query query = CreateFieldQuery(analyzer, Occur.SHOULD, field, queryText, false, 0);
if (query is BooleanQuery bq)
{
bq.MinimumNumberShouldMatch = (int)(fraction * bq.Clauses.Count);
}
return query;
}
/// <summary>
/// Gets or Sets the analyzer. </summary>
public virtual Analyzer Analyzer
{
get => analyzer;
set => this.analyzer = value;
}
/// <summary>
/// Gets or Sets whether position increments are enabled.
/// <para/>
/// When <c>true</c>, result phrase and multi-phrase queries will
/// be aware of position increments.
/// Useful when e.g. a StopFilter increases the position increment of
/// the token that follows an omitted token.
/// <para/>
/// Default: true.
/// </summary>
public virtual bool EnablePositionIncrements
{
get => enablePositionIncrements;
set => this.enablePositionIncrements = value;
}
/// <summary>
/// Creates a query from the analysis chain.
/// <para/>
/// Expert: this is more useful for subclasses such as queryparsers.
/// If using this class directly, just use <see cref="CreateBooleanQuery(string, string)"/>
/// and <see cref="CreatePhraseQuery(string, string)"/>. </summary>
/// <param name="analyzer"> Analyzer used for this query. </param>
/// <param name="operator"> Default boolean operator used for this query. </param>
/// <param name="field"> Field to create queries against. </param>
/// <param name="queryText"> Text to be passed to the analysis chain. </param>
/// <param name="quoted"> <c>true</c> if phrases should be generated when terms occur at more than one position. </param>
/// <param name="phraseSlop"> Slop factor for phrase/multiphrase queries. </param>
protected Query CreateFieldQuery(Analyzer analyzer, Occur @operator, string field, string queryText, bool quoted, int phraseSlop)
{
if (Debugging.AssertsEnabled) Debugging.Assert(@operator == Occur.SHOULD || @operator == Occur.MUST);
// Use the analyzer to get all the tokens, and then build a TermQuery,
// PhraseQuery, or nothing based on the term count
CachingTokenFilter buffer = null;
ITermToBytesRefAttribute termAtt = null;
IPositionIncrementAttribute posIncrAtt = null;
int numTokens = 0;
int positionCount = 0;
bool severalTokensAtSamePosition = false;
bool hasMoreTokens/* = false*/; // LUCENENET: IDE0059: Remove unnecessary value assignment
TokenStream source = null;
try
{
source = analyzer.GetTokenStream(field, new StringReader(queryText));
source.Reset();
buffer = new CachingTokenFilter(source);
buffer.Reset();
if (buffer.HasAttribute<ITermToBytesRefAttribute>())
{
termAtt = buffer.GetAttribute<ITermToBytesRefAttribute>();
}
if (buffer.HasAttribute<IPositionIncrementAttribute>())
{
posIncrAtt = buffer.GetAttribute<IPositionIncrementAttribute>();
}
if (termAtt != null)
{
try
{
hasMoreTokens = buffer.IncrementToken();
while (hasMoreTokens)
{
numTokens++;
int positionIncrement = (posIncrAtt != null) ? posIncrAtt.PositionIncrement : 1;
if (positionIncrement != 0)
{
positionCount += positionIncrement;
}
else
{
severalTokensAtSamePosition = true;
}
hasMoreTokens = buffer.IncrementToken();
}
}
catch (Exception e) when (e.IsIOException())
{
// ignore
}
}
}
catch (Exception e) when (e.IsIOException())
{
throw RuntimeException.Create("Error analyzing query text", e);
}
finally
{
IOUtils.DisposeWhileHandlingException(source);
}
// rewind the buffer stream
buffer.Reset();
BytesRef bytes = termAtt?.BytesRef;
if (numTokens == 0)
{
return null;
}
else if (numTokens == 1)
{
try
{
bool hasNext = buffer.IncrementToken();
if (Debugging.AssertsEnabled) Debugging.Assert(hasNext == true);
termAtt.FillBytesRef();
}
catch (Exception e) when (e.IsIOException())
{
// safe to ignore, because we know the number of tokens
}
return NewTermQuery(new Term(field, BytesRef.DeepCopyOf(bytes)));
}
else
{
if (severalTokensAtSamePosition || (!quoted))
{
if (positionCount == 1 || (!quoted))
{
// no phrase query:
if (positionCount == 1)
{
// simple case: only one position, with synonyms
BooleanQuery q = NewBooleanQuery(true);
for (int i = 0; i < numTokens; i++)
{
try
{
bool hasNext = buffer.IncrementToken();
if (Debugging.AssertsEnabled) Debugging.Assert(hasNext == true);
termAtt.FillBytesRef();
}
catch (Exception e) when (e.IsIOException())
{
// safe to ignore, because we know the number of tokens
}
Query currentQuery = NewTermQuery(new Term(field, BytesRef.DeepCopyOf(bytes)));
q.Add(currentQuery, Occur.SHOULD);
}
return q;
}
else
{
// multiple positions
BooleanQuery q = NewBooleanQuery(false);
Query currentQuery = null;
for (int i = 0; i < numTokens; i++)
{
try
{
bool hasNext = buffer.IncrementToken();
if (Debugging.AssertsEnabled) Debugging.Assert(hasNext == true);
termAtt.FillBytesRef();
}
catch (Exception e) when (e.IsIOException())
{
// safe to ignore, because we know the number of tokens
}
if (posIncrAtt != null && posIncrAtt.PositionIncrement == 0)
{
if (!(currentQuery is BooleanQuery))
{
Query t = currentQuery;
currentQuery = NewBooleanQuery(true);
((BooleanQuery)currentQuery).Add(t, Occur.SHOULD);
}
((BooleanQuery)currentQuery).Add(NewTermQuery(new Term(field, BytesRef.DeepCopyOf(bytes))), Occur.SHOULD);
}
else
{
if (currentQuery != null)
{
q.Add(currentQuery, @operator);
}
currentQuery = NewTermQuery(new Term(field, BytesRef.DeepCopyOf(bytes)));
}
}
q.Add(currentQuery, @operator);
return q;
}
}
else
{
// phrase query:
MultiPhraseQuery mpq = NewMultiPhraseQuery();
mpq.Slop = phraseSlop;
IList<Term> multiTerms = new JCG.List<Term>();
int position = -1;
for (int i = 0; i < numTokens; i++)
{
int positionIncrement = 1;
try
{
bool hasNext = buffer.IncrementToken();
if (Debugging.AssertsEnabled) Debugging.Assert(hasNext == true);
termAtt.FillBytesRef();
if (posIncrAtt != null)
{
positionIncrement = posIncrAtt.PositionIncrement;
}
}
catch (Exception e) when (e.IsIOException())
{
// safe to ignore, because we know the number of tokens
}
if (positionIncrement > 0 && multiTerms.Count > 0)
{
if (enablePositionIncrements)
{
mpq.Add(multiTerms.ToArray(), position);
}
else
{
mpq.Add(multiTerms.ToArray());
}
multiTerms.Clear();
}
position += positionIncrement;
multiTerms.Add(new Term(field, BytesRef.DeepCopyOf(bytes)));
}
if (enablePositionIncrements)
{
mpq.Add(multiTerms.ToArray(), position);
}
else
{
mpq.Add(multiTerms.ToArray());
}
return mpq;
}
}
else
{
PhraseQuery pq = NewPhraseQuery();
pq.Slop = phraseSlop;
int position = -1;
for (int i = 0; i < numTokens; i++)
{
int positionIncrement = 1;
try
{
bool hasNext = buffer.IncrementToken();
if (Debugging.AssertsEnabled) Debugging.Assert(hasNext == true);
termAtt.FillBytesRef();
if (posIncrAtt != null)
{
positionIncrement = posIncrAtt.PositionIncrement;
}
}
catch (Exception e) when (e.IsIOException())
{
// safe to ignore, because we know the number of tokens
}
if (enablePositionIncrements)
{
position += positionIncrement;
pq.Add(new Term(field, BytesRef.DeepCopyOf(bytes)), position);
}
else
{
pq.Add(new Term(field, BytesRef.DeepCopyOf(bytes)));
}
}
return pq;
}
}
}
/// <summary>
/// Builds a new <see cref="BooleanQuery"/> instance.
/// <para/>
/// This is intended for subclasses that wish to customize the generated queries.
/// </summary>
/// <param name="disableCoord"> Disable coord. </param>
/// <returns> New <see cref="BooleanQuery"/> instance. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual BooleanQuery NewBooleanQuery(bool disableCoord)
{
return new BooleanQuery(disableCoord);
}
/// <summary>
/// Builds a new <see cref="TermQuery"/> instance.
/// <para/>
/// This is intended for subclasses that wish to customize the generated queries.
/// </summary>
/// <param name="term"> Term. </param>
/// <returns> New <see cref="TermQuery"/> instance. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual Query NewTermQuery(Term term)
{
return new TermQuery(term);
}
/// <summary>
/// Builds a new <see cref="PhraseQuery"/> instance.
/// <para/>
/// This is intended for subclasses that wish to customize the generated queries.
/// </summary>
/// <returns> New <see cref="PhraseQuery"/> instance. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual PhraseQuery NewPhraseQuery()
{
return new PhraseQuery();
}
/// <summary>
/// Builds a new <see cref="MultiPhraseQuery"/> instance.
/// <para/>
/// This is intended for subclasses that wish to customize the generated queries.
/// </summary>
/// <returns> New <see cref="MultiPhraseQuery"/> instance. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected virtual MultiPhraseQuery NewMultiPhraseQuery()
{
return new MultiPhraseQuery();
}
}
}