/
MultiMatchQuery.cs
330 lines (270 loc) · 12.8 KB
/
MultiMatchQuery.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
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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 System;
using System.Runtime.Serialization;
using OpenSearch.Net.Utf8Json;
namespace OpenSearch.Client
{
/// <summary>
/// A match query across multiple fields.
/// </summary>
[InterfaceDataContract]
[ReadAs(typeof(MultiMatchQuery))]
public interface IMultiMatchQuery : IQuery
{
/// <summary>
/// The analyzer name used to analyze the query
/// </summary>
[DataMember(Name = "analyzer")]
string Analyzer { get; set; }
/// <summary></summary>
[DataMember(Name = "auto_generate_synonyms_phrase_query")]
bool? AutoGenerateSynonymsPhraseQuery { get; set; }
/// <summary>
/// A cutoff frequency that allows specifying an absolute or relative document frequency where
/// high frequency terms are moved into an optional subquery and are only scored if one of the low frequency
/// (below the cutoff) terms in the case of <see cref="Client.Operator.Or" />,
/// or all of the low frequency terms in the case of an <see cref="Client.Operator.And" /> match.
/// </summary>
[DataMember(Name = "cutoff_frequency")]
double? CutoffFrequency { get; set; }
/// <summary>
/// The fields to perform the query against.
/// </summary>
[DataMember(Name = "fields")]
Fields Fields { get; set; }
/// <summary>
/// Allows fuzzy matching based on the type of field being queried.
/// Cannot be used with the
/// <see cref="TextQueryType.CrossFields" />,
/// <see cref="TextQueryType.Phrase" /> or
/// <see cref="TextQueryType.PhrasePrefix" /> types.
/// </summary>
[DataMember(Name = "fuzziness")]
Fuzziness Fuzziness { get; set; }
/// <summary>
/// Controls how the query is rewritten if <see cref="Fuzziness" /> is set.
/// In this scenario, the default is <see cref="MultiTermQueryRewrite.TopTermsBlendedFreqs" />.
/// </summary>
[DataMember(Name = "fuzzy_rewrite")]
MultiTermQueryRewrite FuzzyRewrite { get; set; }
/// <summary>
/// Sets whether transpositions are supported in fuzzy queries.
/// <para />
/// The default metric used by fuzzy queries to determine a match is the Damerau-Levenshtein
/// distance formula which supports transpositions. Setting transposition to false will
/// switch to classic Levenshtein distance.
/// If not set, Damerau-Levenshtein distance metric will be used.
/// </summary>
[DataMember(Name = "fuzzy_transpositions")]
bool? FuzzyTranspositions { get; set; }
/// <summary>
/// If set to <c>true</c> will cause format based failures (like providing text to a numeric field)
/// to be ignored
/// </summary>
[DataMember(Name = "lenient")]
bool? Lenient { get; set; }
/// <summary>
/// Controls the number of terms fuzzy queries will expand to. Defaults to <c>50</c>
/// </summary>
[DataMember(Name = "max_expansions")]
int? MaxExpansions { get; set; }
/// <summary>
/// A value controlling how many "should" clauses in the resulting boolean query should match.
/// It can be an absolute value, a percentage or a combination of both.
/// </summary>
[DataMember(Name = "minimum_should_match")]
MinimumShouldMatch MinimumShouldMatch { get; set; }
/// <summary>
/// The operator used if no explicit operator is specified.
/// The default operator is <see cref="Client.Operator.Or" />
/// </summary>
/// <remarks>
/// <see cref="TextQueryType.BestFields" /> and <see cref="TextQueryType.MostFields" /> types are field-centric ;
/// they generate a match query per field. This means that <see cref="Operator" /> and <see cref="MinimumShouldMatch" />
/// are applied to each field individually, which is probably not what you want.
/// Consider using <see cref="TextQueryType.CrossFields" />.
/// </remarks>
[DataMember(Name = "operator")]
Operator? Operator { get; set; }
/// <summary>
/// Set the prefix length for fuzzy queries. Default is <c>0</c>.
/// </summary>
[DataMember(Name = "prefix_length")]
int? PrefixLength { get; set; }
/// <summary>
/// The query to execute
/// </summary>
[DataMember(Name = "query")]
string Query { get; set; }
/// <summary>
/// How far apart terms are allowed to be while still considering the document to be a match.
/// </summary>
[DataMember(Name = "slop")]
int? Slop { get; set; }
/// <summary>
/// Used to influence how the score is calculated for <see cref="TextQueryType.BestFields" />. If specified,
/// score is calculated using
/// </summary>
[DataMember(Name = "tie_breaker")]
double? TieBreaker { get; set; }
/// <summary>
/// How the fields should be combined to build the text query.
/// Default is <see cref="TextQueryType.BestFields" />
/// </summary>
[DataMember(Name = "type")]
TextQueryType? Type { get; set; }
/// <summary>
/// By default, a <see cref="IMultiMatchQuery" /> generates a match clause per field, then wraps them
/// in a <see cref="IDisMaxQuery" />. By setting <see cref="UseDisMax" /> to <c>false</c>,
/// they will be wrapped in a <see cref="IBoolQuery" /> instead.
/// </summary>
[DataMember(Name = "use_dis_max")]
bool? UseDisMax { get; set; }
/// <summary>
/// If the analyzer used removes all tokens in a query like a stop filter does, the default behavior is
/// to match no documents at all. In order to change that, <see cref="Client.ZeroTermsQuery" /> can be used,
/// which accepts <see cref="Client.ZeroTermsQuery.None" /> (default) and <see cref="Client.ZeroTermsQuery.All" />
/// which corresponds to a match_all query.
/// </summary>
[DataMember(Name = "zero_terms_query")]
ZeroTermsQuery? ZeroTermsQuery { get; set; }
}
/// <inheritdoc cref="IMultiMatchQuery" />
public class MultiMatchQuery : QueryBase, IMultiMatchQuery
{
/// <inheritdoc />
public string Analyzer { get; set; }
/// <inheritdoc />
public bool? AutoGenerateSynonymsPhraseQuery { get; set; }
/// <inheritdoc />
public double? CutoffFrequency { get; set; }
/// <inheritdoc />
public Fields Fields { get; set; }
/// <inheritdoc />
public Fuzziness Fuzziness { get; set; }
/// <inheritdoc />
public MultiTermQueryRewrite FuzzyRewrite { get; set; }
/// <inheritdoc />
public bool? FuzzyTranspositions { get; set; }
/// <inheritdoc />
public bool? Lenient { get; set; }
/// <inheritdoc />
public int? MaxExpansions { get; set; }
/// <inheritdoc />
public MinimumShouldMatch MinimumShouldMatch { get; set; }
/// <inheritdoc />
public Operator? Operator { get; set; }
/// <inheritdoc />
public int? PrefixLength { get; set; }
/// <inheritdoc />
public string Query { get; set; }
/// <inheritdoc />
public int? Slop { get; set; }
/// <inheritdoc />
public double? TieBreaker { get; set; }
/// <inheritdoc />
public TextQueryType? Type { get; set; }
/// <inheritdoc />
public bool? UseDisMax { get; set; }
/// <inheritdoc />
public ZeroTermsQuery? ZeroTermsQuery { get; set; }
protected override bool Conditionless => IsConditionless(this);
internal override void InternalWrapInContainer(IQueryContainer c) => c.MultiMatch = this;
internal static bool IsConditionless(IMultiMatchQuery q) => q.Query.IsNullOrEmpty();
}
/// <inheritdoc cref="IMultiMatchQuery" />
[DataContract]
public class MultiMatchQueryDescriptor<T>
: QueryDescriptorBase<MultiMatchQueryDescriptor<T>, IMultiMatchQuery>
, IMultiMatchQuery where T : class
{
protected override bool Conditionless => MultiMatchQuery.IsConditionless(this);
string IMultiMatchQuery.Analyzer { get; set; }
bool? IMultiMatchQuery.AutoGenerateSynonymsPhraseQuery { get; set; }
double? IMultiMatchQuery.CutoffFrequency { get; set; }
Fields IMultiMatchQuery.Fields { get; set; }
Fuzziness IMultiMatchQuery.Fuzziness { get; set; }
MultiTermQueryRewrite IMultiMatchQuery.FuzzyRewrite { get; set; }
bool? IMultiMatchQuery.FuzzyTranspositions { get; set; }
bool? IMultiMatchQuery.Lenient { get; set; }
int? IMultiMatchQuery.MaxExpansions { get; set; }
MinimumShouldMatch IMultiMatchQuery.MinimumShouldMatch { get; set; }
Operator? IMultiMatchQuery.Operator { get; set; }
int? IMultiMatchQuery.PrefixLength { get; set; }
string IMultiMatchQuery.Query { get; set; }
int? IMultiMatchQuery.Slop { get; set; }
double? IMultiMatchQuery.TieBreaker { get; set; }
TextQueryType? IMultiMatchQuery.Type { get; set; }
bool? IMultiMatchQuery.UseDisMax { get; set; }
ZeroTermsQuery? IMultiMatchQuery.ZeroTermsQuery { get; set; }
/// <inheritdoc cref="IMultiMatchQuery.Fields" />
public MultiMatchQueryDescriptor<T> Fields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
Assign(fields, (a, v) => a.Fields = v?.Invoke(new FieldsDescriptor<T>())?.Value);
/// <inheritdoc cref="IMultiMatchQuery.Fields" />
public MultiMatchQueryDescriptor<T> Fields(Fields fields) => Assign(fields, (a, v) => a.Fields = v);
/// <inheritdoc cref="IMultiMatchQuery.Query" />
public MultiMatchQueryDescriptor<T> Query(string query) => Assign(query, (a, v) => a.Query = v);
/// <inheritdoc cref="IMultiMatchQuery.Analyzer" />
public MultiMatchQueryDescriptor<T> Analyzer(string analyzer) => Assign(analyzer, (a, v) => a.Analyzer = v);
/// <inheritdoc cref="IMultiMatchQuery.Fuzziness" />
public MultiMatchQueryDescriptor<T> Fuzziness(Fuzziness fuzziness) => Assign(fuzziness, (a, v) => a.Fuzziness = v);
/// <inheritdoc cref="IMultiMatchQuery.CutoffFrequency" />
public MultiMatchQueryDescriptor<T> CutoffFrequency(double? cutoffFrequency)
=> Assign(cutoffFrequency, (a, v) => a.CutoffFrequency = v);
/// <inheritdoc cref="IMultiMatchQuery.MinimumShouldMatch" />
public MultiMatchQueryDescriptor<T> MinimumShouldMatch(MinimumShouldMatch minimumShouldMatch)
=> Assign(minimumShouldMatch, (a, v) => a.MinimumShouldMatch = v);
/// <inheritdoc cref="IMultiMatchQuery.FuzzyRewrite" />
public MultiMatchQueryDescriptor<T> FuzzyRewrite(MultiTermQueryRewrite rewrite) => Assign(rewrite, (a, v) => a.FuzzyRewrite = v);
/// <inheritdoc cref="IMultiMatchQuery.FuzzyTranspositions" />
public MultiMatchQueryDescriptor<T> FuzzyTranspositions(bool? fuzzyTranpositions = true) =>
Assign(fuzzyTranpositions, (a, v) => a.FuzzyTranspositions = v);
/// <inheritdoc cref="IMultiMatchQuery.Lenient" />
public MultiMatchQueryDescriptor<T> Lenient(bool? lenient = true) => Assign(lenient, (a, v) => a.Lenient = v);
/// <inheritdoc cref="IMultiMatchQuery.PrefixLength" />
public MultiMatchQueryDescriptor<T> PrefixLength(int? prefixLength) => Assign(prefixLength, (a, v) => a.PrefixLength = v);
/// <inheritdoc cref="IMultiMatchQuery.MaxExpansions" />
public MultiMatchQueryDescriptor<T> MaxExpansions(int? maxExpansions) => Assign(maxExpansions, (a, v) => a.MaxExpansions = v);
/// <inheritdoc cref="IMultiMatchQuery.Slop" />
public MultiMatchQueryDescriptor<T> Slop(int? slop) => Assign(slop, (a, v) => a.Slop = v);
/// <inheritdoc cref="IMultiMatchQuery.Operator" />
public MultiMatchQueryDescriptor<T> Operator(Operator? op) => Assign(op, (a, v) => a.Operator = v);
/// <inheritdoc cref="IMultiMatchQuery.TieBreaker" />
public MultiMatchQueryDescriptor<T> TieBreaker(double? tieBreaker) => Assign(tieBreaker, (a, v) => a.TieBreaker = v);
/// <inheritdoc cref="IMultiMatchQuery.Type" />
public MultiMatchQueryDescriptor<T> Type(TextQueryType? type) => Assign(type, (a, v) => a.Type = v);
/// <inheritdoc cref="IMultiMatchQuery.UseDisMax" />
public MultiMatchQueryDescriptor<T> UseDisMax(bool? useDisMax = true) => Assign(useDisMax, (a, v) => a.UseDisMax = v);
/// <inheritdoc cref="IMultiMatchQuery.ZeroTermsQuery" />
public MultiMatchQueryDescriptor<T> ZeroTermsQuery(ZeroTermsQuery? zeroTermsQuery) => Assign(zeroTermsQuery, (a, v) => a.ZeroTermsQuery = v);
/// <inheritdoc cref="IMultiMatchQuery.AutoGenerateSynonymsPhraseQuery" />
public MultiMatchQueryDescriptor<T> AutoGenerateSynonymsPhraseQuery(bool? autoGenerateSynonymsPhraseQuery = true) =>
Assign(autoGenerateSynonymsPhraseQuery, (a, v) => a.AutoGenerateSynonymsPhraseQuery = v);
}
}