/
AtlasSearchTests.cs
518 lines (430 loc) · 19.3 KB
/
AtlasSearchTests.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
/* 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 FluentAssertions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.TestHelpers.XunitExtensions;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.TestHelpers.Logging;
using MongoDB.Driver.GeoJsonObjectModel;
using MongoDB.Driver.Search;
using MongoDB.Driver.TestHelpers;
using Xunit;
using Xunit.Abstractions;
using Builders = MongoDB.Driver.Builders<MongoDB.Driver.Tests.Search.AtlasSearchTests.HistoricalDocument>;
using GeoBuilders = MongoDB.Driver.Builders<MongoDB.Driver.Tests.Search.AtlasSearchTests.AirbnbListing>;
namespace MongoDB.Driver.Tests.Search
{
[Trait("Category", "AtlasSearch")]
public class AtlasSearchTests : LoggableTestClass
{
#region static
private static readonly GeoJsonPolygon<GeoJson2DGeographicCoordinates> __testPolygon =
new(new(new(new GeoJson2DGeographicCoordinates[]
{
new(-8.6131, 41.14),
new(-8.6131, 41.145),
new(-8.60308, 41.145),
new(-8.60308, 41.14),
new(-8.6131, 41.14),
})));
private static readonly GeoWithinBox<GeoJson2DGeographicCoordinates> __testBox =
new(new(new(-8.6131, 41.14)), new(new(-8.60308, 41.145)));
private static readonly GeoWithinCircle<GeoJson2DGeographicCoordinates> __testCircle =
new(new(new(-8.61308, 41.1413)), 273);
#endregion
private readonly DisposableMongoClient _disposableMongoClient;
public AtlasSearchTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
RequireEnvironment.Check().EnvironmentVariable("ATLAS_SEARCH_TESTS_ENABLED");
var atlasSearchUri = Environment.GetEnvironmentVariable("ATLAS_SEARCH");
Ensure.IsNotNullOrEmpty(atlasSearchUri, nameof(atlasSearchUri));
_disposableMongoClient = new(new MongoClient(atlasSearchUri), CreateLogger<DisposableMongoClient>());
}
protected override void DisposeInternal() => _disposableMongoClient.Dispose();
[Fact]
public void Autocomplete()
{
var result = SearchSingle(Builders.Search.Autocomplete(x => x.Title, "Declaration of Ind"));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void Compound()
{
var result = SearchSingle(Builders.Search.Compound()
.Must(Builders.Search.Text(x => x.Body, "life"), Builders.Search.Text(x => x.Body, "liberty"))
.MustNot(Builders.Search.Text(x => x.Body, "property"))
.Must(Builders.Search.Text(x => x.Body, "pursuit of happiness")));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void Count_total()
{
var results = GetTestCollection().Aggregate()
.Search(
Builders.Search.Phrase(x => x.Body, "life, liberty, and the pursuit of happiness"),
count: new SearchCountOptions()
{
Type = SearchCountType.Total
})
.Project<HistoricalDocument>(Builders.Projection.SearchMeta(x => x.MetaResult))
.Limit(1)
.ToList();
results.Should().ContainSingle().Which.MetaResult.Count.Total.Should().Be(108);
}
[Fact]
public void Exists()
{
var result = SearchSingle(
Builders.Search.Compound().Must(
Builders.Search.Text(x => x.Body, "life, liberty, and the pursuit of happiness"),
Builders.Search.Exists(x => x.Title)));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void Filter()
{
var result = SearchSingle(
Builders.Search.Compound().Filter(
Builders.Search.Phrase(x => x.Body, "life, liberty"),
Builders.Search.Wildcard(x => x.Body, "happ*", true)));
result.Title.Should().Be("Declaration of Independence");
}
[Theory]
[InlineData("add")]
[InlineData("constant")]
[InlineData("gauss")]
[InlineData("log")]
[InlineData("log1p")]
[InlineData("multiply")]
[InlineData("path")]
[InlineData("relevance")]
public void FunctionScore(string functionScoreType)
{
var scoreFunction = functionScoreType switch
{
"add" => Builders.SearchScoreFunction.Add(Constant(1), Constant(2)),
"constant" => Constant(1),
"gauss" => Builders.SearchScoreFunction.Gauss(x => x.Score, 100, 1, 0.1, 1),
"log" => Builders.SearchScoreFunction.Log(Constant(1)),
"log1p" => Builders.SearchScoreFunction.Log1p(Constant(1)),
"multiply" => Builders.SearchScoreFunction.Multiply(Constant(1), Constant(2)),
"path" => Builders.SearchScoreFunction.Path(x => x.Score, 1),
"relevance" => Builders.SearchScoreFunction.Relevance(),
_ => throw new ArgumentOutOfRangeException(nameof(functionScoreType), functionScoreType, "Invalid score function")
};
var result = SearchSingle(Builders.Search.Phrase(
x => x.Body,
"life, liberty, and the pursuit of happiness",
score: Builders.SearchScore.Function(scoreFunction)));
result.Title.Should().Be("Declaration of Independence");
SearchScoreFunction<HistoricalDocument> Constant(double value) =>
Builders.SearchScoreFunction.Constant(value);
}
[Fact]
public void GeoShape()
{
var results = GeoSearch(
GeoBuilders.Search.GeoShape(
x => x.Address.Location,
GeoShapeRelation.Intersects,
__testPolygon));
results.Count.Should().Be(25);
results.First().Name.Should().Be("Ribeira Charming Duplex");
}
[Theory]
[InlineData("box")]
[InlineData("circle")]
[InlineData("polygon")]
public void GeoWithin(string geometryType)
{
GeoWithinArea<GeoJson2DGeographicCoordinates> geoArea = geometryType switch
{
"box" => __testBox,
"circle" => __testCircle,
"polygon" => new GeoWithinGeometry<GeoJson2DGeographicCoordinates>(__testPolygon),
_ => throw new ArgumentOutOfRangeException(nameof(geometryType), geometryType, "Invalid geometry type")
};
var results = GeoSearch(GeoBuilders.Search.GeoWithin(x => x.Address.Location, geoArea));
results.Count.Should().Be(25);
results.First().Name.Should().Be("Ribeira Charming Duplex");
}
[Fact]
public void MoreLikeThis()
{
var likeThisDocument = new HistoricalDocument
{
Title = "Declaration of Independence",
Body = "We hold these truths to be self-evident that all men are created equal..."
};
var result = SearchSingle(Builders.Search.MoreLikeThis(likeThisDocument));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void Must()
{
var result = SearchSingle(
Builders.Search.Compound().Must(
Builders.Search.Phrase(x => x.Body, "life, liberty"),
Builders.Search.Wildcard(x => x.Body, "happ*", true)));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void MustNot()
{
var result = SearchSingle(
Builders.Search.Compound().MustNot(
Builders.Search.Phrase(x => x.Body, "life, liberty")));
result.Title.Should().Be("US Constitution");
}
[Fact]
public void Near()
{
var results = GetGeoTestCollection().Aggregate()
.Search(GeoBuilders.Search.Near(x => x.Address.Location, __testCircle.Center, 1000))
.Limit(1)
.ToList();
results.Should().ContainSingle().Which.Name.Should().Be("Ribeira Charming Duplex");
}
[Fact]
public void Phrase()
{
// This test case exercises the indexName and returnStoredSource arguments. The
// remaining test cases omit them.
var coll = GetTestCollection();
var results = GetTestCollection().Aggregate()
.Search(Builders.Search.Phrase(x => x.Body, "life, liberty, and the pursuit of happiness"),
new SearchHighlightOptions<HistoricalDocument>(x => x.Body),
indexName: "default",
returnStoredSource: true)
.Limit(1)
.Project<HistoricalDocument>(Builders.Projection
.Include(x => x.Title)
.Include(x => x.Body)
.MetaSearchScore("score")
.MetaSearchHighlights("highlights"))
.ToList();
var result = results.Should().ContainSingle().Subject;
result.Title.Should().Be("Declaration of Independence");
result.Score.Should().NotBe(0);
var highlightTexts = result.Highlights.Should().ContainSingle().Subject.Texts;
highlightTexts.Should().HaveCount(15);
foreach (var highlight in highlightTexts)
{
var expectedType = char.IsLetter(highlight.Value[0]) ? HighlightTextType.Hit : HighlightTextType.Text;
highlight.Type.Should().Be(expectedType);
}
var highlightRangeStr = string.Join(string.Empty, highlightTexts.Skip(1).Select(x => x.Value));
highlightRangeStr.Should().Be("Life, Liberty and the pursuit of Happiness.");
}
[Fact]
public void PhraseMultiPath()
{
var result = SearchSingle(
Builders.Search.Phrase(
Builders.SearchPath.Multi(x => x.Title, x => x.Body),
"life, liberty, and the pursuit of happiness"));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void PhraseAnalyzerPath()
{
var result = SearchSingle(
Builders.Search.Phrase(
Builders.SearchPath.Analyzer(x => x.Body, "english"),
"life, liberty, and the pursuit of happiness"));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void PhraseWildcardPath()
{
var result = SearchSingle(
Builders.Search.Phrase(
Builders.SearchPath.Wildcard("b*"),
"life, liberty, and the pursuit of happiness"));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void QueryString()
{
var result = SearchSingle(Builders.Search.QueryString(x => x.Body, "life, liberty, and the pursuit of happiness"));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void Range()
{
var results = GeoSearch(
GeoBuilders.Search.Compound().Must(
GeoBuilders.Search.Range(x => x.Bedrooms, SearchRangeBuilder.Gt(2).Lt(4)),
GeoBuilders.Search.Range(x => x.Beds, SearchRangeBuilder.Gte(14).Lte(14))));
results.Should().ContainSingle().Which.Name.Should().Be("House close to station & direct to opera house....");
}
[Fact]
public void Search_count_lowerBound()
{
var results = GetTestCollection().Aggregate()
.Search(
Builders.Search.Phrase(x => x.Body, "life, liberty, and the pursuit of happiness"),
count: new SearchCountOptions()
{
Type = SearchCountType.LowerBound,
Threshold = 128
})
.Project<HistoricalDocument>(Builders.Projection.SearchMeta(x => x.MetaResult))
.Limit(1)
.ToList();
results.Should().ContainSingle().Which.MetaResult.Count.LowerBound.Should().Be(108);
}
[Fact]
public void SearchMeta_count()
{
var result = GetTestCollection().Aggregate()
.SearchMeta(
Builders.Search.Phrase(x => x.Body, "life, liberty, and the pursuit of happiness"),
"default",
new SearchCountOptions() { Type = SearchCountType.Total })
.Single();
result.Should().NotBeNull();
result.Count.Should().NotBeNull();
result.Count.Total.Should().Be(108);
}
[Fact]
public void SearchMeta_facet()
{
var result = GetTestCollection().Aggregate()
.SearchMeta(Builders.Search.Facet(
Builders.Search.Phrase(x => x.Body, "life, liberty, and the pursuit of happiness"),
Builders.SearchFacet.String("string", x => x.Author, 100),
Builders.SearchFacet.Number("number", x => x.Index, 0, 100),
Builders.SearchFacet.Date("date", x => x.Date, DateTime.MinValue, DateTime.MaxValue)))
.Single();
result.Should().NotBeNull();
var bucket = result.Facet["string"].Buckets.Should().NotBeNull().And.ContainSingle().Subject;
bucket.Id.Should().Be((BsonString)"machine");
bucket.Count.Should().Be(108);
bucket = result.Facet["number"].Buckets.Should().NotBeNull().And.ContainSingle().Subject;
bucket.Id.Should().Be((BsonInt32)0);
bucket.Count.Should().Be(0);
bucket = result.Facet["date"].Buckets.Should().NotBeNull().And.ContainSingle().Subject;
bucket.Id.Should().Be((BsonDateTime)DateTime.MinValue);
bucket.Count.Should().Be(108);
}
[Fact]
public void Should()
{
var result = SearchSingle(
Builders.Search.Compound().Should(
Builders.Search.Phrase(x => x.Body, "life, liberty"),
Builders.Search.Wildcard(x => x.Body, "happ*", true))
.MinimumShouldMatch(2));
result.Title.Should().Be("Declaration of Independence");
}
[Theory]
[InlineData("first")]
[InlineData("near")]
[InlineData("or")]
[InlineData("subtract")]
public void Span(string spanType)
{
var spanDefinition = spanType switch
{
"first" => Builders.SearchSpan.First(Term("happiness"), 250),
"near" => Builders.SearchSpan.Near(new[] { Term("life"), Term("liberty"), Term("pursuit"), Term("happiness") }, 3, true),
"or" => Builders.SearchSpan.Or(Term("unalienable"), Term("inalienable")),
"subtract" => Builders.SearchSpan.Subtract(Term("unalienable"), Term("inalienable")),
_ => throw new ArgumentOutOfRangeException(nameof(spanType), spanType, "Invalid span type")
};
var result = SearchSingle(Builders.Search.Span(spanDefinition));
result.Title.Should().Be("Declaration of Independence");
SearchSpanDefinition<HistoricalDocument> Term(string term) => Builders.SearchSpan.Term(x => x.Body, term);
}
[Fact]
public void Text()
{
var result = SearchSingle(Builders.Search.Text(x => x.Body, "life, liberty, and the pursuit of happiness"));
result.Title.Should().Be("Declaration of Independence");
}
[Fact]
public void Wildcard()
{
var result = SearchSingle(Builders.Search.Wildcard(x => x.Body, "tranquil*", true));
result.Title.Should().Be("US Constitution");
}
private List<AirbnbListing> GeoSearch(SearchDefinition<AirbnbListing> searchDefintion) =>
GetGeoTestCollection().Aggregate().Search(searchDefintion).ToList();
private HistoricalDocument SearchSingle(SearchDefinition<HistoricalDocument> searchDefintion) =>
GetTestCollection()
.Aggregate()
.Search(searchDefintion)
.Limit(1)
.ToList()
.Single();
private IMongoCollection<HistoricalDocument> GetTestCollection() => _disposableMongoClient
.GetDatabase("sample_training")
.GetCollection<HistoricalDocument>("posts");
private IMongoCollection<AirbnbListing> GetGeoTestCollection() => _disposableMongoClient
.GetDatabase("sample_airbnb")
.GetCollection<AirbnbListing>("listingsAndReviews");
[BsonIgnoreExtraElements]
public class HistoricalDocument
{
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("body")]
public string Body { get; set; }
[BsonElement("author")]
public string Author { get; set; }
[BsonElement("title")]
public string Title { get; set; }
[BsonElement("highlights")]
public List<SearchHighlight> Highlights { get; set; }
[BsonElement("score")]
public double Score { get; set; }
[BsonElement("date")]
public DateTime Date { get; set; }
[BsonElement("index")]
public int Index { get; set; }
[BsonElement("metaResult")]
public SearchMetaResult MetaResult { get; set; }
}
[BsonIgnoreExtraElements]
public class Address
{
[BsonElement("location")]
public GeoJsonObject<GeoJson2DGeographicCoordinates> Location { get; set; }
[BsonElement("street")]
public string Street { get; set; }
}
[BsonIgnoreExtraElements]
public class AirbnbListing
{
[BsonElement("address")]
public Address Address { get; set; }
[BsonElement("bedrooms")]
public int Bedrooms { get; set; }
[BsonElement("beds")]
public int Beds { get; set; }
[BsonElement("description")]
public string Description { get; set; }
[BsonElement("space")]
public string Space { get; set; }
[BsonElement("name")]
public string Name { get; set; }
}
}
}