Skip to content

Commit

Permalink
CSHARP-4733: Add implementation for Atlas Search Tracking search terms (
Browse files Browse the repository at this point in the history
#1143)

Co-authored-by: Samir Boulema <samir@auto.nl>
  • Loading branch information
sboulema and Samir Boulema committed Aug 2, 2023
1 parent 945ae83 commit 26fa40a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,7 @@ public static class PipelineStageDefinitionBuilder
renderedSearchDefinition.Add("index", searchOptions.IndexName, searchOptions.IndexName != null);
renderedSearchDefinition.Add("returnStoredSource", searchOptions.ReturnStoredSource, searchOptions.ReturnStoredSource);
renderedSearchDefinition.Add("scoreDetails", searchOptions.ScoreDetails, searchOptions.ScoreDetails);
renderedSearchDefinition.Add("tracking", () => searchOptions.Tracking.Render(), searchOptions.Tracking != null);
var document = new BsonDocument(operatorName, renderedSearchDefinition);
return new RenderedPipelineStageDefinition<TInput>(operatorName, document, s);
Expand Down
5 changes: 5 additions & 0 deletions src/MongoDB.Driver/Search/SearchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ public sealed class SearchOptions<TResult>
/// Gets or sets the sort specification.
/// </summary>
public SortDefinition<TResult> Sort { get; set; }

/// <summary>
/// Gets or sets the options for tracking search terms.
/// </summary>
public SearchTrackingOptions Tracking { get; set; }
}
}
43 changes: 43 additions & 0 deletions src/MongoDB.Driver/Search/SearchTrackingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* 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 MongoDB.Bson;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver.Search
{
/// <summary>
/// Options for tracking the search query.
/// </summary>
public sealed class SearchTrackingOptions
{
private string _searchTerms;

/// <summary>
/// Text or term associated with the query to track. You can specify only one term per query.
/// </summary>
public string SearchTerms
{
get => _searchTerms;
set => _searchTerms = Ensure.IsNotNullOrEmpty(value, nameof(value));
}

internal BsonDocument Render() =>
new()
{
{ "searchTerms", _searchTerms, !string.IsNullOrEmpty(_searchTerms) }
};
}
}
19 changes: 19 additions & 0 deletions tests/MongoDB.Driver.Tests/PipelineDefinitionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ public void Search_should_add_expected_stage_with_sort()
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, sort: { 'foo': 1 } } }");
}

[Fact]
public void Search_should_add_expected_stage_with_tracking()
{
var pipeline = new EmptyPipelineDefinition<BsonDocument>();
var builder = new SearchDefinitionBuilder<BsonDocument>();
var searchOptions = new SearchOptions<BsonDocument>()
{
Tracking = new SearchTrackingOptions()
{
SearchTerms = "foo"
}
};

var result = pipeline.Search(builder.Text("bar", "foo"), searchOptions);

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

[Fact]
public void Search_should_throw_when_pipeline_is_null()
{
Expand Down

0 comments on commit 26fa40a

Please sign in to comment.