Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/MongoDB.Driver/AggregateFluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,11 @@ public override IAggregateFluent<TResult> Search(
SearchHighlightOptions<TResult> highlight = null,
string indexName = null,
SearchCountOptions count = null,
SortDefinition<TResult> sort = null,
bool returnStoredSource = false,
bool scoreDetails = false)
{
return WithPipeline(_pipeline.Search(searchDefinition, highlight, indexName, count, returnStoredSource, scoreDetails));
return WithPipeline(_pipeline.Search(searchDefinition, highlight, indexName, count, sort, returnStoredSource, scoreDetails));
}

public override IAggregateFluent<SearchMetaResult> SearchMeta(
Expand Down
1 change: 1 addition & 0 deletions src/MongoDB.Driver/AggregateFluentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public virtual IAggregateFluent<TResult> Search(
SearchHighlightOptions<TResult> highlight = null,
string indexName = null,
SearchCountOptions count = null,
SortDefinition<TResult> sort = null,
bool returnStoredSource = false,
bool scoreDetails = false)
{
Expand Down
2 changes: 2 additions & 0 deletions src/MongoDB.Driver/IAggregateFluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
/// <param name="highlight">The highlight options.</param>
/// <param name="indexName">The index name.</param>
/// <param name="count">The count options.</param>
/// <param name="sort">The sort specification.</param>
/// <param name="returnStoredSource">
/// Flag that specifies whether to perform a full document lookup on the backend database
/// or return only stored source fields directly from Atlas Search.
Expand All @@ -375,6 +376,7 @@ IAggregateFluent<TResult> Search(
SearchHighlightOptions<TResult> highlight = null,
string indexName = null,
SearchCountOptions count = null,
SortDefinition<TResult> sort = null,
bool returnStoredSource = false,
bool scoreDetails = false);

Expand Down
4 changes: 3 additions & 1 deletion src/MongoDB.Driver/Linq/MongoQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ public static IMongoQueryable<TSource> Sample<TSource>(this IMongoQueryable<TSou
/// <param name="highlight">The highlight options.</param>
/// <param name="indexName">The index name.</param>
/// <param name="count">The count options.</param>
/// <param name="sort">The sort specification.</param>
/// <param name="returnStoredSource">
/// Flag that specifies whether to perform a full document lookup on the backend database
/// or return only stored source fields directly from Atlas Search.
Expand All @@ -1160,12 +1161,13 @@ public static IMongoQueryable<TSource> Search<TSource>(
SearchHighlightOptions<TSource> highlight = null,
string indexName = null,
SearchCountOptions count = null,
SortDefinition<TSource> sort = null,
bool returnStoredSource = false,
bool scoreDetails = false)
{
return AppendStage(
source,
PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, returnStoredSource, scoreDetails));
PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, sort, returnStoredSource, scoreDetails));
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/MongoDB.Driver/PipelineDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ public static PipelineDefinition<TInput, TOutput> ReplaceWith<TInput, TIntermedi
/// <param name="highlight">The highlight options.</param>
/// <param name="indexName">The index name.</param>
/// <param name="count">The count options.</param>
/// <param name="sort">The sort specification.</param>
/// <param name="returnStoredSource">
/// Flag that specifies whether to perform a full document lookup on the backend database
/// or return only stored source fields directly from Atlas Search.
Expand All @@ -1192,11 +1193,12 @@ public static PipelineDefinition<TInput, TOutput> Search<TInput, TOutput>(
SearchHighlightOptions<TOutput> highlight = null,
string indexName = null,
SearchCountOptions count = null,
SortDefinition<TOutput> sort = null,
bool returnStoredSource = false,
bool scoreDetails = false)
{
Ensure.IsNotNull(pipeline, nameof(pipeline));
return pipeline.AppendStage(PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, returnStoredSource, scoreDetails));
return pipeline.AppendStage(PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, sort, returnStoredSource, scoreDetails));
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@ public static PipelineStageDefinition<TInput, TOutput> Project<TInput, TOutput>(
/// <param name="highlight">The highlight options.</param>
/// <param name="indexName">The index name.</param>
/// <param name="count">The count options.</param>
/// <param name="sort">The sort specification.</param>
/// <param name="returnStoredSource">
/// Flag that specifies whether to perform a full document lookup on the backend database
/// or return only stored source fields directly from Atlas Search.
Expand All @@ -1330,6 +1331,7 @@ public static PipelineStageDefinition<TInput, TInput> Search<TInput>(
SearchHighlightOptions<TInput> highlight = null,
string indexName = null,
SearchCountOptions count = null,
SortDefinition<TInput> sort = null,
bool returnStoredSource = false,
bool scoreDetails = false)
{
Expand All @@ -1343,6 +1345,7 @@ public static PipelineStageDefinition<TInput, TInput> Search<TInput>(
var renderedSearchDefinition = searchDefinition.Render(s, sr);
renderedSearchDefinition.Add("highlight", () => highlight.Render(s, sr), highlight != null);
renderedSearchDefinition.Add("count", () => count.Render(), count != null);
renderedSearchDefinition.Add("sort", () => sort.Render(s, sr), sort != null);
renderedSearchDefinition.Add("index", indexName, indexName != null);
renderedSearchDefinition.Add("returnStoredSource", returnStoredSource, returnStoredSource);
renderedSearchDefinition.Add("scoreDetails", scoreDetails, scoreDetails);
Expand Down
13 changes: 13 additions & 0 deletions tests/MongoDB.Driver.Tests/PipelineDefinitionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ public void Search_should_add_expected_stage_with_score_details()
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, scoreDetails: true } }");
}

[Fact]
public void Search_should_add_expected_stage_with_sort()
{
var pipeline = new EmptyPipelineDefinition<BsonDocument>();
var builder = new SearchDefinitionBuilder<BsonDocument>();
var sortBuilder = new SortDefinitionBuilder<BsonDocument>();

var result = pipeline.Search(builder.Text("bar", "foo"), sort: sortBuilder.Ascending("foo"));

var stages = RenderStages(result, BsonDocumentSerializer.Instance);
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, sort: { 'foo': 1 } } }");
}

[Fact]
public void Search_should_throw_when_pipeline_is_null()
{
Expand Down
15 changes: 14 additions & 1 deletion tests/MongoDB.Driver.Tests/Search/AtlasSearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void MustNot()
var result = SearchSingle(
Builders.Search.Compound().MustNot(
Builders.Search.Phrase(x => x.Body, "life, liberty")));
result.Title.Should().Be("US Constitution");
result.Title.Should().Be("Gettysburg Address");
}

[Fact]
Expand Down Expand Up @@ -413,6 +413,19 @@ public void Should()
result.Title.Should().Be("Declaration of Independence");
}

[Fact]
public void Sort()
{
var results = GetTestCollection().Aggregate()
.Search(
Builders.Search.Text(x => x.Body, "liberty"),
sort: Builders.Sort.Descending(x => x.Title))
.Project<HistoricalDocument>(Builders.Projection.Include(x => x.Title))
.Limit(1)
.ToList();
results.Should().ContainSingle().Which.Title.Should().Be("US Constitution");
}

[Theory]
[InlineData("first")]
[InlineData("near")]
Expand Down