-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathOperatorSearchDefinitions.cs
533 lines (468 loc) · 20.1 KB
/
OperatorSearchDefinitions.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* Copyright 2010-present MongoDB Inc.
*
* Licensed 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.Collections.Generic;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.GeoJsonObjectModel;
namespace MongoDB.Driver.Search
{
internal sealed class AutocompleteSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly SearchFuzzyOptions _fuzzy;
private readonly SearchQueryDefinition _query;
private readonly SearchAutocompleteTokenOrder _tokenOrder;
public AutocompleteSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
SearchAutocompleteTokenOrder tokenOrder,
SearchFuzzyOptions fuzzy,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.Autocomplete, path, score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_tokenOrder = tokenOrder;
_fuzzy = fuzzy;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "query", _query.Render() },
{ "tokenOrder", _tokenOrder.ToCamelCase(), _tokenOrder != SearchAutocompleteTokenOrder.Any },
{ "fuzzy", () => _fuzzy.Render(), _fuzzy != null },
};
}
internal sealed class CompoundSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly List<SearchDefinition<TDocument>> _filter;
private readonly int _minimumShouldMatch;
private readonly List<SearchDefinition<TDocument>> _must;
private readonly List<SearchDefinition<TDocument>> _mustNot;
private readonly List<SearchDefinition<TDocument>> _should;
public CompoundSearchDefinition(
List<SearchDefinition<TDocument>> must,
List<SearchDefinition<TDocument>> mustNot,
List<SearchDefinition<TDocument>> should,
List<SearchDefinition<TDocument>> filter,
int minimumShouldMatch,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.Compound, score)
{
// This constructor should always be called from the compound search definition builder that ensures the arguments are valid.
_must = must;
_mustNot = mustNot;
_should = should;
_filter = filter;
_minimumShouldMatch = minimumShouldMatch;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args)
{
return new()
{
{ "must", Render(_must), _must != null },
{ "mustNot", Render(_mustNot), _mustNot != null },
{ "should", Render(_should), _should != null },
{ "filter", Render(_filter), _filter != null },
{ "minimumShouldMatch", _minimumShouldMatch, _minimumShouldMatch > 0 },
};
Func<BsonArray> Render(List<SearchDefinition<TDocument>> searchDefinitions) =>
() => new BsonArray(searchDefinitions.Select(clause => clause.Render(args)));
}
}
internal sealed class EmbeddedDocumentSearchDefinition<TDocument, TField> : OperatorSearchDefinition<TDocument>
{
private readonly SearchDefinition<TField> _operator;
public EmbeddedDocumentSearchDefinition(FieldDefinition<TDocument, IEnumerable<TField>> path, SearchDefinition<TField> @operator, SearchScoreDefinition<TDocument> score)
: base(OperatorType.EmbeddedDocument,
new SingleSearchPathDefinition<TDocument>(path),
score)
{
_operator = Ensure.IsNotNull(@operator, nameof(@operator));
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args)
{
// Add base path to all nested operator paths
var pathPrefix = _path.Render(args).AsString;
var newArgs = args
.WithNewDocumentType(args.SerializerRegistry.GetSerializer<TField>())
with { PathRenderArgs = new(pathPrefix) };
return new("operator", _operator.Render(newArgs));
}
}
internal sealed class EqualsSearchDefinition<TDocument, TField> : OperatorSearchDefinition<TDocument>
{
private readonly BsonValue _value;
public EqualsSearchDefinition(FieldDefinition<TDocument> path, TField value, SearchScoreDefinition<TDocument> score)
: base(OperatorType.Equals, path, score)
{
_value = ToBsonValue(value);
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new("value", _value);
private static BsonValue ToBsonValue(TField value) =>
value switch
{
bool v => (BsonBoolean)v,
sbyte v => (BsonInt32)v,
byte v => (BsonInt32)v,
short v => (BsonInt32)v,
ushort v => (BsonInt32)v,
int v => (BsonInt32)v,
uint v => (BsonInt64)v,
long v => (BsonInt64)v,
float v => (BsonDouble)v,
double v => (BsonDouble)v,
DateTime v => (BsonDateTime)v,
DateTimeOffset v => (BsonDateTime)v.UtcDateTime,
ObjectId v => (BsonObjectId)v,
Guid v => new BsonBinaryData(v, GuidRepresentation.Standard),
string v => (BsonString)v,
null => BsonNull.Value,
_ => throw new InvalidCastException()
};
}
internal sealed class ExistsSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
public ExistsSearchDefinition(FieldDefinition<TDocument> path)
: base(OperatorType.Exists, path, null)
{
}
}
internal sealed class FacetSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly SearchFacet<TDocument>[] _facets;
private readonly SearchDefinition<TDocument> _operator;
public FacetSearchDefinition(SearchDefinition<TDocument> @operator, IEnumerable<SearchFacet<TDocument>> facets)
: base(OperatorType.Facet)
{
_operator = Ensure.IsNotNull(@operator, nameof(@operator));
_facets = Ensure.IsNotNull(facets, nameof(facets)).ToArray();
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "operator", _operator.Render(args) },
{ "facets", new BsonDocument(_facets.Select(f => new BsonElement(f.Name, f.Render(args)))) }
};
}
internal sealed class GeoShapeSearchDefinition<TDocument, TCoordinates> : OperatorSearchDefinition<TDocument>
where TCoordinates : GeoJsonCoordinates
{
private readonly GeoJsonGeometry<TCoordinates> _geometry;
private readonly GeoShapeRelation _relation;
public GeoShapeSearchDefinition(
SearchPathDefinition<TDocument> path,
GeoShapeRelation relation,
GeoJsonGeometry<TCoordinates> geometry,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.GeoShape, path, score)
{
_geometry = Ensure.IsNotNull(geometry, nameof(geometry));
_relation = relation;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "geometry", _geometry.ToBsonDocument() },
{ "relation", _relation.ToCamelCase() }
};
}
internal sealed class GeoWithinSearchDefinition<TDocument, TCoordinates> : OperatorSearchDefinition<TDocument>
where TCoordinates : GeoJsonCoordinates
{
private readonly GeoWithinArea<TCoordinates> _area;
public GeoWithinSearchDefinition(
SearchPathDefinition<TDocument> path,
GeoWithinArea<TCoordinates> area,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.GeoWithin, path, score)
{
_area = Ensure.IsNotNull(area, nameof(area));
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new(_area.Render());
}
internal sealed class InSearchDefinition<TDocument, TField> : OperatorSearchDefinition<TDocument>
{
private readonly BsonArray _values;
public InSearchDefinition(
SearchPathDefinition<TDocument> path,
IEnumerable<TField> values,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.In, path, score)
{
Ensure.IsNotNullOrEmpty(values, nameof(values));
var array = new BsonArray(values.Select(ToBsonValue));
var bsonType = array[0].GetType();
_values = Ensure.That(array, arr => arr.All(v => v.GetType() == bsonType), nameof(values), "All values must be of the same type.");
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new("value", _values);
private static BsonValue ToBsonValue(TField value) =>
value switch
{
bool v => (BsonBoolean)v,
sbyte v => (BsonInt32)v,
byte v => (BsonInt32)v,
short v => (BsonInt32)v,
ushort v => (BsonInt32)v,
int v => (BsonInt32)v,
uint v => (BsonInt64)v,
long v => (BsonInt64)v,
float v => (BsonDouble)v,
double v => (BsonDouble)v,
decimal v => (BsonDecimal128)v,
DateTime v => (BsonDateTime)v,
DateTimeOffset v => (BsonDateTime)v.UtcDateTime,
string v => (BsonString)v,
ObjectId v => (BsonObjectId)v,
Guid v => new BsonBinaryData(v, GuidRepresentation.Standard),
_ => throw new InvalidCastException()
};
}
internal sealed class MoreLikeThisSearchDefinition<TDocument, TLike> : OperatorSearchDefinition<TDocument>
{
private readonly TLike[] _like;
public MoreLikeThisSearchDefinition(IEnumerable<TLike> like)
: base(OperatorType.MoreLikeThis)
{
_like = Ensure.IsNotNull(like, nameof(like)).ToArray();
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args)
{
var likeSerializer = typeof(TLike) switch
{
var t when t == typeof(BsonDocument) => null,
var t when t == typeof(TDocument) => (IBsonSerializer<TLike>)args.DocumentSerializer,
_ => args.SerializerRegistry.GetSerializer<TLike>()
};
return new("like", new BsonArray(_like.Select(document => document.ToBsonDocument(likeSerializer))));
}
}
internal sealed class NearSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly BsonValue _origin;
private readonly BsonValue _pivot;
public NearSearchDefinition(
SearchPathDefinition<TDocument> path,
BsonValue origin,
BsonValue pivot,
SearchScoreDefinition<TDocument> score = null)
: base(OperatorType.Near, path, score)
{
_origin = origin;
_pivot = pivot;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "origin", _origin },
{ "pivot", _pivot }
};
}
internal sealed class PhraseSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly SearchQueryDefinition _query;
private readonly int? _slop;
private readonly string _synonyms;
public PhraseSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
int? slop,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.Phrase, path, score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_slop = slop;
}
public PhraseSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
SearchPhraseOptions<TDocument> options)
: base(OperatorType.Phrase, path, options?.Score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_slop = options?.Slop;
_synonyms = options?.Synonyms;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "query", _query.Render() },
{ "slop", _slop, _slop != null },
{ "synonyms", _synonyms, _synonyms != null }
};
}
internal sealed class QueryStringSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly SingleSearchPathDefinition<TDocument> _defaultPath;
private readonly string _query;
public QueryStringSearchDefinition(FieldDefinition<TDocument> defaultPath, string query, SearchScoreDefinition<TDocument> score)
: base(OperatorType.QueryString, score)
{
_defaultPath = new SingleSearchPathDefinition<TDocument>(defaultPath);
_query = Ensure.IsNotNull(query, nameof(query));
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "defaultPath", _defaultPath.Render(args) },
{ "query", _query }
};
}
internal sealed class RangeSearchDefinition<TDocument, TField> : OperatorSearchDefinition<TDocument>
{
private readonly SearchRangeV2<TField> _range;
public RangeSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchRangeV2<TField> range,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.Range, path, score)
{
_range = range;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args)
{
BsonValue min = null, max = null;
bool minInclusive = false, maxInclusive = false;
if (_range.Min != null)
{
min = ToBsonValue(_range.Min.Value);
minInclusive = _range.Min.Inclusive;
}
if (_range.Max != null)
{
max = ToBsonValue(_range.Max.Value);
maxInclusive = _range.Max.Inclusive;
}
return new()
{
{ minInclusive ? "gte" : "gt", min, min != null },
{ maxInclusive ? "lte" : "lt", max, max != null }
};
}
private static BsonValue ToBsonValue(TField value) =>
value switch
{
sbyte v => (BsonInt32)v,
byte v => (BsonInt32)v,
short v => (BsonInt32)v,
ushort v => (BsonInt32)v,
int v => (BsonInt32)v,
uint v => (BsonInt64)v,
long v => (BsonInt64)v,
float v => (BsonDouble)v,
double v => (BsonDouble)v,
DateTime v => (BsonDateTime)v,
DateTimeOffset v => (BsonDateTime)v.UtcDateTime,
string v => (BsonString)v,
null => null,
_ => throw new InvalidCastException()
};
}
internal sealed class RegexSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly bool _allowAnalyzedField;
private readonly SearchQueryDefinition _query;
public RegexSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
bool allowAnalyzedField,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.Regex, path, score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_allowAnalyzedField = allowAnalyzedField;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "query", _query.Render() },
{ "allowAnalyzedField", _allowAnalyzedField, _allowAnalyzedField },
};
}
internal sealed class SpanSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly SearchSpanDefinition<TDocument> _clause;
public SpanSearchDefinition(SearchSpanDefinition<TDocument> clause)
: base(OperatorType.Span)
{
_clause = Ensure.IsNotNull(clause, nameof(clause));
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
_clause.Render(args);
}
internal sealed class TextSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly SearchFuzzyOptions _fuzzy;
private readonly MatchCriteria? _matchCriteria;
private readonly SearchQueryDefinition _query;
private readonly string _synonyms;
public TextSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
SearchFuzzyOptions fuzzy,
SearchScoreDefinition<TDocument> score,
string synonyms)
: base(OperatorType.Text, path, score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_fuzzy = fuzzy;
_synonyms = synonyms;
}
public TextSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
SearchTextOptions<TDocument> options)
: base(OperatorType.Text, path, options?.Score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_fuzzy = options?.Fuzzy;
_synonyms = options?.Synonyms;
_matchCriteria = options?.MatchCriteria;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "query", _query.Render() },
{ "fuzzy", () => _fuzzy.Render(), _fuzzy != null },
{ "synonyms", _synonyms, _synonyms != null },
{ "matchCriteria", _matchCriteria == MatchCriteria.Any ? "any" : "all", _matchCriteria != null }
};
}
internal sealed class WildcardSearchDefinition<TDocument> : OperatorSearchDefinition<TDocument>
{
private readonly bool _allowAnalyzedField;
private readonly SearchQueryDefinition _query;
public WildcardSearchDefinition(
SearchPathDefinition<TDocument> path,
SearchQueryDefinition query,
bool allowAnalyzedField,
SearchScoreDefinition<TDocument> score)
: base(OperatorType.Wildcard, path, score)
{
_query = Ensure.IsNotNull(query, nameof(query));
_allowAnalyzedField = allowAnalyzedField;
}
private protected override BsonDocument RenderArguments(RenderArgs<TDocument> args) =>
new()
{
{ "query", _query.Render() },
{ "allowAnalyzedField", _allowAnalyzedField, _allowAnalyzedField },
};
}
}