@@ -477,6 +477,51 @@ public void Text()
477
477
result . Title . Should ( ) . Be ( "Declaration of Independence" ) ;
478
478
}
479
479
480
+ [ Theory ]
481
+ [ InlineData ( "automobile" , "transportSynonyms" , "Blue Car" ) ]
482
+ [ InlineData ( "boat" , "transportSynonyms" , "And the Ship Sails On" ) ]
483
+ public void TextWithSynonymsReturnsCorrectResult ( string query , string synonym , string expected )
484
+ {
485
+ var sortDefinition = Builders < Movie > . Sort . Ascending ( x => x . Title ) ;
486
+ var result =
487
+ GetSynonymTestCollection ( ) . Aggregate ( )
488
+ . Search ( Builders < Movie > . Search . Text ( x => x . Title , query , synonym ) , indexName : "synonyms-tests" )
489
+ . Sort ( sortDefinition )
490
+ . Project < Movie > ( Builders < Movie > . Projection . Include ( "Title" ) . Exclude ( "_id" ) )
491
+ . Limit ( 1 )
492
+ . Single ( ) ;
493
+
494
+ result . Title . Should ( ) . Be ( expected ) ;
495
+ }
496
+
497
+ [ Fact ]
498
+ public void TextWithSynonymsMappings ( )
499
+ {
500
+ var automobileAndAttireSearchResults = SearchMultipleSynonymMapping (
501
+ Builders < Movie > . Search . Text ( x => x . Title , "automobile" , "transportSynonyms" ) ,
502
+ Builders < Movie > . Search . Text ( x => x . Title , "attire" , "attireSynonyms" ) ) ;
503
+
504
+ var vehicleAndDressSearchResults = SearchMultipleSynonymMapping (
505
+ Builders < Movie > . Search . Text ( x => x . Title , "vehicle" , "transportSynonyms" ) ,
506
+ Builders < Movie > . Search . Text ( x => x . Title , "dress" , "attireSynonyms" ) ) ;
507
+
508
+ var boatAndHatSearchResults = SearchMultipleSynonymMapping (
509
+ Builders < Movie > . Search . Text ( x => x . Title , "boat" , "transportSynonyms" ) ,
510
+ Builders < Movie > . Search . Text ( x => x . Title , "hat" , "attireSynonyms" ) ) ;
511
+
512
+ var vesselAndFedoraSearchResults = SearchMultipleSynonymMapping (
513
+ Builders < Movie > . Search . Text ( x => x . Title , "vessel" , "transportSynonyms" ) ,
514
+ Builders < Movie > . Search . Text ( x => x . Title , "fedora" , "attireSynonyms" ) ) ;
515
+
516
+ automobileAndAttireSearchResults . Should ( ) . NotBeNull ( ) ;
517
+ vehicleAndDressSearchResults . Should ( ) . NotBeNull ( ) ;
518
+ boatAndHatSearchResults . Should ( ) . NotBeNull ( ) ;
519
+ vesselAndFedoraSearchResults . Should ( ) . NotBeNull ( ) ;
520
+
521
+ automobileAndAttireSearchResults . Should ( ) . BeEquivalentTo ( vehicleAndDressSearchResults ) ;
522
+ boatAndHatSearchResults . Should ( ) . NotBeEquivalentTo ( vesselAndFedoraSearchResults ) ;
523
+ }
524
+
480
525
[ Fact ]
481
526
public void Wildcard ( )
482
527
{
@@ -500,9 +545,15 @@ private HistoricalDocument SearchSingle(
500
545
fluent = fluent . Project ( projectionDefinition ) ;
501
546
}
502
547
503
- return fluent . Limit ( 1 ) . ToList ( ) . Single ( ) ;
548
+ return fluent . Limit ( 1 ) . Single ( ) ;
504
549
}
505
550
551
+ private List < BsonDocument > SearchMultipleSynonymMapping ( params SearchDefinition < Movie > [ ] clauses ) =>
552
+ GetSynonymTestCollection ( ) . Aggregate ( )
553
+ . Search ( Builders < Movie > . Search . Compound ( ) . Should ( clauses ) , indexName : "synonyms-tests" )
554
+ . Project ( Builders < Movie > . Projection . Include ( "Title" ) . Exclude ( "_id" ) )
555
+ . ToList ( ) ;
556
+
506
557
private IMongoCollection < HistoricalDocument > GetTestCollection ( ) => _disposableMongoClient
507
558
. GetDatabase ( "sample_training" )
508
559
. GetCollection < HistoricalDocument > ( "posts" ) ;
@@ -511,6 +562,10 @@ private IMongoCollection<T> GetTestCollection<T>() => _disposableMongoClient
511
562
. GetDatabase ( "sample_training" )
512
563
. GetCollection < T > ( "posts" ) ;
513
564
565
+ private IMongoCollection < Movie > GetSynonymTestCollection ( ) => _disposableMongoClient
566
+ . GetDatabase ( "sample_mflix" )
567
+ . GetCollection < Movie > ( "movies" ) ;
568
+
514
569
private IMongoCollection < AirbnbListing > GetGeoTestCollection ( ) => _disposableMongoClient
515
570
. GetDatabase ( "sample_airbnb" )
516
571
. GetCollection < AirbnbListing > ( "listingsAndReviews" ) ;
@@ -522,6 +577,13 @@ public class Comment
522
577
public string Author { get ; set ; }
523
578
}
524
579
580
+ [ BsonIgnoreExtraElements ]
581
+ public class Movie
582
+ {
583
+ [ BsonElement ( "title" ) ]
584
+ public string Title { get ; set ; }
585
+ }
586
+
525
587
[ BsonIgnoreExtraElements ]
526
588
public class HistoricalDocumentWithCommentsOnly
527
589
{
0 commit comments