forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiFieldQueryParser.cs
382 lines (365 loc) · 15.6 KB
/
MultiFieldQueryParser.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
using Lucene.Net.Analysis;
using Lucene.Net.Search;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
namespace Lucene.Net.QueryParsers.Classic
{
/*
* 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.
*/
/// <summary>
/// A <see cref="QueryParser"/> which constructs queries to search multiple fields.
/// </summary>
public class MultiFieldQueryParser : QueryParser
{
protected string[] m_fields;
protected IDictionary<string, float> m_boosts;
/// <summary>
/// Creates a <see cref="MultiFieldQueryParser"/>. Allows passing of a map with term to
/// Boost, and the boost to apply to each term.
///
/// <para/>
/// It will, when <see cref="QueryParserBase.Parse(string)"/> is called, construct a query like this
/// (assuming the query consists of two terms and you specify the two fields
/// <c>title</c> and <c>body</c>):
/// <para/>
///
/// <code>
/// (title:term1 body:term1) (title:term2 body:term2)
/// </code>
///
/// <para/>
/// When <see cref="QueryParserBase.DefaultOperator"/> is set to <see cref="QueryParserBase.AND_OPERATOR"/>, the result will be:
/// <para/>
///
/// <code>
/// +(title:term1 body:term1) +(title:term2 body:term2)
/// </code>
///
/// <para/>
/// When you pass a boost (title=>5 body=>10) you can get
/// <para/>
///
/// <code>
/// +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)
/// </code>
///
/// <para/>
/// In other words, all the query's terms must appear, but it doesn't matter
/// in what fields they appear.
/// <para/>
/// </summary>
public MultiFieldQueryParser(LuceneVersion matchVersion, string[] fields, Analyzer analyzer, IDictionary<string, float> boosts)
: this(matchVersion, fields, analyzer)
{
this.m_boosts = boosts;
}
/// <summary>
/// Creates a MultiFieldQueryParser.
///
/// <para/>
/// It will, when <see cref="QueryParserBase.Parse(string)"/> is called, construct a query like this
/// (assuming the query consists of two terms and you specify the two fields
/// <c>title</c> and <c>body</c>):
/// <para/>
///
/// <code>
/// (title:term1 body:term1) (title:term2 body:term2)
/// </code>
///
/// <para/>
/// When <see cref="QueryParserBase.DefaultOperator"/> is set to <see cref="QueryParserBase.AND_OPERATOR"/>, the result will be:
/// <para/>
///
/// <code>
/// +(title:term1 body:term1) +(title:term2 body:term2)
/// </code>
///
/// <para/>
/// In other words, all the query's terms must appear, but it doesn't matter
/// in what fields they appear.
/// <para/>
/// </summary>
public MultiFieldQueryParser(LuceneVersion matchVersion, string[] fields, Analyzer analyzer)
: base(matchVersion, null, analyzer)
{
this.m_fields = fields;
}
protected internal override Query GetFieldQuery(string field, string queryText, int slop)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
Query q = base.GetFieldQuery(m_fields[i], queryText, true);
if (q != null)
{
//If the user passes a map of boosts
if (m_boosts != null)
{
//Get the boost from the map and apply them
float boost = m_boosts[m_fields[i]];
q.Boost = boost;
}
ApplySlop(q, slop);
clauses.Add(new BooleanClause(q, Occur.SHOULD));
}
}
if (clauses.Count == 0)
// happens for stopwords
return null;
return GetBooleanQuery(clauses, true);
}
Query q2 = base.GetFieldQuery(field, queryText, true);
ApplySlop(q2, slop);
return q2;
}
private void ApplySlop(Query q, int slop)
{
if (q is PhraseQuery phraseQuery)
{
phraseQuery.Slop = slop;
}
else if (q is MultiPhraseQuery multiPhraseQuery)
{
multiPhraseQuery.Slop = slop;
}
}
protected internal override Query GetFieldQuery(string field, string queryText, bool quoted)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
Query q = base.GetFieldQuery(m_fields[i], queryText, quoted);
if (q != null)
{
//If the user passes a map of boosts
if (m_boosts != null)
{
//Get the boost from the map and apply them
float boost = m_boosts[m_fields[i]];
q.Boost = boost;
}
clauses.Add(new BooleanClause(q, Occur.SHOULD));
}
}
if (clauses.Count == 0) // happens for stopwords
return null;
return GetBooleanQuery(clauses, true);
}
Query q2 = base.GetFieldQuery(field, queryText, quoted);
return q2;
}
protected internal override Query GetFuzzyQuery(string field, string termStr, float minSimilarity)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
clauses.Add(new BooleanClause(GetFuzzyQuery(m_fields[i], termStr, minSimilarity), Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetFuzzyQuery(field, termStr, minSimilarity);
}
protected internal override Query GetPrefixQuery(string field, string termStr)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
clauses.Add(new BooleanClause(GetPrefixQuery(m_fields[i], termStr), Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetPrefixQuery(field, termStr);
}
protected internal override Query GetWildcardQuery(string field, string termStr)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
clauses.Add(new BooleanClause(GetWildcardQuery(m_fields[i], termStr), Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetWildcardQuery(field, termStr);
}
protected internal override Query GetRangeQuery(string field, string part1, string part2, bool startInclusive, bool endInclusive)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
clauses.Add(new BooleanClause(GetRangeQuery(m_fields[i], part1, part2, startInclusive, endInclusive), Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetRangeQuery(field, part1, part2, startInclusive, endInclusive);
}
protected internal override Query GetRegexpQuery(string field, string termStr)
{
if (field == null)
{
IList<BooleanClause> clauses = new List<BooleanClause>();
for (int i = 0; i < m_fields.Length; i++)
{
clauses.Add(new BooleanClause(GetRegexpQuery(m_fields[i], termStr),
Occur.SHOULD));
}
return GetBooleanQuery(clauses, true);
}
return base.GetRegexpQuery(field, termStr);
}
/// <summary>
/// Parses a query which searches on the fields specified.
/// <para/>
/// If x fields are specified, this effectively constructs:
///
/// <code>
/// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
/// </code>
///
/// </summary>
/// <param name="matchVersion">Lucene version to match; this is passed through to
/// <see cref="QueryParser"/>.</param>
/// <param name="queries">Queries strings to parse</param>
/// <param name="fields">Fields to search on</param>
/// <param name="analyzer">Analyzer to use</param>
/// <exception cref="ParseException">if query parsing fails</exception>
/// <exception cref="ArgumentException">
/// if the length of the queries array differs from the length of
/// the fields array
/// </exception>
public static Query Parse(LuceneVersion matchVersion, string[] queries, string[] fields, Analyzer analyzer)
{
if (queries.Length != fields.Length)
throw new ArgumentException("queries.length != fields.length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.Length; i++)
{
QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
Query q = qp.Parse(queries[i]);
if (q != null && (!(q is BooleanQuery booleanQuery) || booleanQuery.Clauses.Count > 0))
{
bQuery.Add(q, Occur.SHOULD);
}
}
return bQuery;
}
/// <summary>
/// Parses a query, searching on the fields specified. Use this if you need
/// to specify certain fields as required, and others as prohibited.
/// <para/>
/// Usage:
/// <code>
/// string[] fields = {"filename", "contents", "description"};
/// Occur[] flags = {Occur.SHOULD,
/// Occur.MUST,
/// Occur.MUST_NOT};
/// MultiFieldQueryParser.Parse("query", fields, flags, analyzer);
/// </code>
/// <para/>
/// The code above would construct a query:
///
/// <code>
/// (filename:query) +(contents:query) -(description:query)
/// </code>
///
/// </summary>
/// <param name="matchVersion">Lucene version to match; this is passed through to
/// <see cref="QueryParser"/>.</param>
/// <param name="query">Query string to parse</param>
/// <param name="fields">Fields to search on</param>
/// <param name="flags">Flags describing the fields</param>
/// <param name="analyzer">Analyzer to use</param>
/// <exception cref="ParseException">if query parsing fails</exception>
/// <exception cref="ArgumentException">
/// if the length of the fields array differs from the length of
/// the flags array
/// </exception>
public static Query Parse(LuceneVersion matchVersion, string query, string[] fields, Occur[] flags, Analyzer analyzer)
{
if (fields.Length != flags.Length)
throw new ArgumentException("fields.length != flags.length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.Length; i++)
{
QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
Query q = qp.Parse(query);
if (q != null && (!(q is BooleanQuery booleanQuery) || booleanQuery.Clauses.Count > 0))
{
bQuery.Add(q, flags[i]);
}
}
return bQuery;
}
/// <summary>
/// Parses a query, searching on the fields specified. Use this if you need
/// to specify certain fields as required, and others as prohibited.
/// <para/>
/// Usage:
/// <code>
/// string[] query = {"query1", "query2", "query3"};
/// string[] fields = {"filename", "contents", "description"};
/// Occur[] flags = {Occur.SHOULD,
/// Occur.MUST,
/// Occur.MUST_NOT};
/// MultiFieldQueryParser.Parse(query, fields, flags, analyzer);
/// </code>
/// <para/>
/// The code above would construct a query:
///
/// <code>
/// (filename:query1) +(contents:query2) -(description:query3)
/// </code>
///
/// </summary>
/// <param name="matchVersion">Lucene version to match; this is passed through to
/// <see cref="QueryParser"/>.</param>
/// <param name="queries">Queries string to parse</param>
/// <param name="fields">Fields to search on</param>
/// <param name="flags">Flags describing the fields</param>
/// <param name="analyzer">Analyzer to use</param>
/// <exception cref="ParseException">if query parsing fails</exception>
/// <exception cref="ArgumentException">if the length of the queries, fields, and flags array differ</exception>
public static Query Parse(LuceneVersion matchVersion, string[] queries, string[] fields, Occur[] flags, Analyzer analyzer)
{
if (!(queries.Length == fields.Length && queries.Length == flags.Length))
throw new ArgumentException("queries, fields, and flags array have have different length");
BooleanQuery bQuery = new BooleanQuery();
for (int i = 0; i < fields.Length; i++)
{
QueryParser qp = new QueryParser(matchVersion, fields[i], analyzer);
Query q = qp.Parse(queries[i]);
if (q != null && (!(q is BooleanQuery booleanQuery) || booleanQuery.Clauses.Count > 0))
{
bQuery.Add(q, flags[i]);
}
}
return bQuery;
}
}
}