Skip to content

Commit 96149cb

Browse files
sboulemaSamir Boulema
andauthored
CSHARP-4728: Add implementation for Atlas Search Sort (#1139)
Co-authored-by: Samir Boulema <[email protected]>
1 parent e0c14c8 commit 96149cb

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,11 @@ public override IAggregateFluent<TResult> Search(
244244
SearchHighlightOptions<TResult> highlight = null,
245245
string indexName = null,
246246
SearchCountOptions count = null,
247+
SortDefinition<TResult> sort = null,
247248
bool returnStoredSource = false,
248249
bool scoreDetails = false)
249250
{
250-
return WithPipeline(_pipeline.Search(searchDefinition, highlight, indexName, count, returnStoredSource, scoreDetails));
251+
return WithPipeline(_pipeline.Search(searchDefinition, highlight, indexName, count, sort, returnStoredSource, scoreDetails));
251252
}
252253

253254
public override IAggregateFluent<SearchMetaResult> SearchMeta(

src/MongoDB.Driver/AggregateFluentBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public virtual IAggregateFluent<TResult> Search(
222222
SearchHighlightOptions<TResult> highlight = null,
223223
string indexName = null,
224224
SearchCountOptions count = null,
225+
SortDefinition<TResult> sort = null,
225226
bool returnStoredSource = false,
226227
bool scoreDetails = false)
227228
{

src/MongoDB.Driver/IAggregateFluent.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
361361
/// <param name="highlight">The highlight options.</param>
362362
/// <param name="indexName">The index name.</param>
363363
/// <param name="count">The count options.</param>
364+
/// <param name="sort">The sort specification.</param>
364365
/// <param name="returnStoredSource">
365366
/// Flag that specifies whether to perform a full document lookup on the backend database
366367
/// or return only stored source fields directly from Atlas Search.
@@ -375,6 +376,7 @@ IAggregateFluent<TResult> Search(
375376
SearchHighlightOptions<TResult> highlight = null,
376377
string indexName = null,
377378
SearchCountOptions count = null,
379+
SortDefinition<TResult> sort = null,
378380
bool returnStoredSource = false,
379381
bool scoreDetails = false);
380382

src/MongoDB.Driver/Linq/MongoQueryable.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ public static IMongoQueryable<TSource> Sample<TSource>(this IMongoQueryable<TSou
11451145
/// <param name="highlight">The highlight options.</param>
11461146
/// <param name="indexName">The index name.</param>
11471147
/// <param name="count">The count options.</param>
1148+
/// <param name="sort">The sort specification.</param>
11481149
/// <param name="returnStoredSource">
11491150
/// Flag that specifies whether to perform a full document lookup on the backend database
11501151
/// or return only stored source fields directly from Atlas Search.
@@ -1160,12 +1161,13 @@ public static IMongoQueryable<TSource> Search<TSource>(
11601161
SearchHighlightOptions<TSource> highlight = null,
11611162
string indexName = null,
11621163
SearchCountOptions count = null,
1164+
SortDefinition<TSource> sort = null,
11631165
bool returnStoredSource = false,
11641166
bool scoreDetails = false)
11651167
{
11661168
return AppendStage(
11671169
source,
1168-
PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, returnStoredSource, scoreDetails));
1170+
PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, sort, returnStoredSource, scoreDetails));
11691171
}
11701172

11711173
/// <summary>

src/MongoDB.Driver/PipelineDefinitionBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ public static PipelineDefinition<TInput, TOutput> ReplaceWith<TInput, TIntermedi
11751175
/// <param name="highlight">The highlight options.</param>
11761176
/// <param name="indexName">The index name.</param>
11771177
/// <param name="count">The count options.</param>
1178+
/// <param name="sort">The sort specification.</param>
11781179
/// <param name="returnStoredSource">
11791180
/// Flag that specifies whether to perform a full document lookup on the backend database
11801181
/// or return only stored source fields directly from Atlas Search.
@@ -1192,11 +1193,12 @@ public static PipelineDefinition<TInput, TOutput> Search<TInput, TOutput>(
11921193
SearchHighlightOptions<TOutput> highlight = null,
11931194
string indexName = null,
11941195
SearchCountOptions count = null,
1196+
SortDefinition<TOutput> sort = null,
11951197
bool returnStoredSource = false,
11961198
bool scoreDetails = false)
11971199
{
11981200
Ensure.IsNotNull(pipeline, nameof(pipeline));
1199-
return pipeline.AppendStage(PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, returnStoredSource, scoreDetails));
1201+
return pipeline.AppendStage(PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, sort, returnStoredSource, scoreDetails));
12001202
}
12011203

12021204
/// <summary>

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ public static PipelineStageDefinition<TInput, TOutput> Project<TInput, TOutput>(
13161316
/// <param name="highlight">The highlight options.</param>
13171317
/// <param name="indexName">The index name.</param>
13181318
/// <param name="count">The count options.</param>
1319+
/// <param name="sort">The sort specification.</param>
13191320
/// <param name="returnStoredSource">
13201321
/// Flag that specifies whether to perform a full document lookup on the backend database
13211322
/// or return only stored source fields directly from Atlas Search.
@@ -1330,6 +1331,7 @@ public static PipelineStageDefinition<TInput, TInput> Search<TInput>(
13301331
SearchHighlightOptions<TInput> highlight = null,
13311332
string indexName = null,
13321333
SearchCountOptions count = null,
1334+
SortDefinition<TInput> sort = null,
13331335
bool returnStoredSource = false,
13341336
bool scoreDetails = false)
13351337
{
@@ -1343,6 +1345,7 @@ public static PipelineStageDefinition<TInput, TInput> Search<TInput>(
13431345
var renderedSearchDefinition = searchDefinition.Render(s, sr);
13441346
renderedSearchDefinition.Add("highlight", () => highlight.Render(s, sr), highlight != null);
13451347
renderedSearchDefinition.Add("count", () => count.Render(), count != null);
1348+
renderedSearchDefinition.Add("sort", () => sort.Render(s, sr), sort != null);
13461349
renderedSearchDefinition.Add("index", indexName, indexName != null);
13471350
renderedSearchDefinition.Add("returnStoredSource", returnStoredSource, returnStoredSource);
13481351
renderedSearchDefinition.Add("scoreDetails", scoreDetails, scoreDetails);

tests/MongoDB.Driver.Tests/PipelineDefinitionBuilderTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ public void Search_should_add_expected_stage_with_score_details()
194194
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, scoreDetails: true } }");
195195
}
196196

197+
[Fact]
198+
public void Search_should_add_expected_stage_with_sort()
199+
{
200+
var pipeline = new EmptyPipelineDefinition<BsonDocument>();
201+
var builder = new SearchDefinitionBuilder<BsonDocument>();
202+
var sortBuilder = new SortDefinitionBuilder<BsonDocument>();
203+
204+
var result = pipeline.Search(builder.Text("bar", "foo"), sort: sortBuilder.Ascending("foo"));
205+
206+
var stages = RenderStages(result, BsonDocumentSerializer.Instance);
207+
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, sort: { 'foo': 1 } } }");
208+
}
209+
197210
[Fact]
198211
public void Search_should_throw_when_pipeline_is_null()
199212
{

tests/MongoDB.Driver.Tests/Search/AtlasSearchTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void MustNot()
232232
var result = SearchSingle(
233233
Builders.Search.Compound().MustNot(
234234
Builders.Search.Phrase(x => x.Body, "life, liberty")));
235-
result.Title.Should().Be("US Constitution");
235+
result.Title.Should().Be("Gettysburg Address");
236236
}
237237

238238
[Fact]
@@ -413,6 +413,19 @@ public void Should()
413413
result.Title.Should().Be("Declaration of Independence");
414414
}
415415

416+
[Fact]
417+
public void Sort()
418+
{
419+
var results = GetTestCollection().Aggregate()
420+
.Search(
421+
Builders.Search.Text(x => x.Body, "liberty"),
422+
sort: Builders.Sort.Descending(x => x.Title))
423+
.Project<HistoricalDocument>(Builders.Projection.Include(x => x.Title))
424+
.Limit(1)
425+
.ToList();
426+
results.Should().ContainSingle().Which.Title.Should().Be("US Constitution");
427+
}
428+
416429
[Theory]
417430
[InlineData("first")]
418431
[InlineData("near")]

0 commit comments

Comments
 (0)