Skip to content

Commit 26fa40a

Browse files
sboulemaSamir Boulema
andauthored
CSHARP-4733: Add implementation for Atlas Search Tracking search terms (#1143)
Co-authored-by: Samir Boulema <samir@auto.nl>
1 parent 945ae83 commit 26fa40a

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ public static PipelineStageDefinition<TInput, TInput> Search<TInput>(
13711371
renderedSearchDefinition.Add("index", searchOptions.IndexName, searchOptions.IndexName != null);
13721372
renderedSearchDefinition.Add("returnStoredSource", searchOptions.ReturnStoredSource, searchOptions.ReturnStoredSource);
13731373
renderedSearchDefinition.Add("scoreDetails", searchOptions.ScoreDetails, searchOptions.ScoreDetails);
1374+
renderedSearchDefinition.Add("tracking", () => searchOptions.Tracking.Render(), searchOptions.Tracking != null);
13741375

13751376
var document = new BsonDocument(operatorName, renderedSearchDefinition);
13761377
return new RenderedPipelineStageDefinition<TInput>(operatorName, document, s);

src/MongoDB.Driver/Search/SearchOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,10 @@ public sealed class SearchOptions<TResult>
5151
/// Gets or sets the sort specification.
5252
/// </summary>
5353
public SortDefinition<TResult> Sort { get; set; }
54+
55+
/// <summary>
56+
/// Gets or sets the options for tracking search terms.
57+
/// </summary>
58+
public SearchTrackingOptions Tracking { get; set; }
5459
}
5560
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
using MongoDB.Driver.Core.Misc;
18+
19+
namespace MongoDB.Driver.Search
20+
{
21+
/// <summary>
22+
/// Options for tracking the search query.
23+
/// </summary>
24+
public sealed class SearchTrackingOptions
25+
{
26+
private string _searchTerms;
27+
28+
/// <summary>
29+
/// Text or term associated with the query to track. You can specify only one term per query.
30+
/// </summary>
31+
public string SearchTerms
32+
{
33+
get => _searchTerms;
34+
set => _searchTerms = Ensure.IsNotNullOrEmpty(value, nameof(value));
35+
}
36+
37+
internal BsonDocument Render() =>
38+
new()
39+
{
40+
{ "searchTerms", _searchTerms, !string.IsNullOrEmpty(_searchTerms) }
41+
};
42+
}
43+
}

tests/MongoDB.Driver.Tests/PipelineDefinitionBuilderTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,25 @@ public void Search_should_add_expected_stage_with_sort()
207207
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, sort: { 'foo': 1 } } }");
208208
}
209209

210+
[Fact]
211+
public void Search_should_add_expected_stage_with_tracking()
212+
{
213+
var pipeline = new EmptyPipelineDefinition<BsonDocument>();
214+
var builder = new SearchDefinitionBuilder<BsonDocument>();
215+
var searchOptions = new SearchOptions<BsonDocument>()
216+
{
217+
Tracking = new SearchTrackingOptions()
218+
{
219+
SearchTerms = "foo"
220+
}
221+
};
222+
223+
var result = pipeline.Search(builder.Text("bar", "foo"), searchOptions);
224+
225+
var stages = RenderStages(result, BsonDocumentSerializer.Instance);
226+
stages[0].Should().Be("{ $search: { text: { query: 'foo', path: 'bar' }, tracking: { searchTerms: 'foo' } } }");
227+
}
228+
210229
[Fact]
211230
public void Search_should_throw_when_pipeline_is_null()
212231
{

0 commit comments

Comments
 (0)