Skip to content

Commit 0c9e2a6

Browse files
committed
CSHARP-4440: Incorporate MongoDB.Labs.Search library
1 parent facf8a1 commit 0c9e2a6

35 files changed

+4266
-140
lines changed

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,21 @@ public override IAggregateFluent<TNewResult> ReplaceWith<TNewResult>(AggregateEx
240240
}
241241

242242
public override IAggregateFluent<TResult> Search(
243-
SearchDefinition<TResult> query,
243+
SearchDefinition<TResult> searchDefinition,
244244
HighlightOptions<TResult> highlight = null,
245245
string indexName = null,
246246
SearchCountOptions count = null,
247247
bool returnStoredSource = false)
248248
{
249-
return WithPipeline(_pipeline.Search(query, highlight, indexName, count, returnStoredSource));
249+
return WithPipeline(_pipeline.Search(searchDefinition, highlight, indexName, count, returnStoredSource));
250+
}
251+
252+
public override IAggregateFluent<SearchMetaResult> SearchMeta(
253+
SearchDefinition<TResult> searchDefinition,
254+
string indexName = null,
255+
SearchCountOptions count = null)
256+
{
257+
return WithPipeline(_pipeline.SearchMeta(searchDefinition, indexName, count));
250258
}
251259

252260
public override IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(

src/MongoDB.Driver/AggregateFluentBase.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ public virtual IAggregateFluent<TResult> Search(
227227
throw new NotImplementedException();
228228
}
229229

230+
/// <inheritdoc />
231+
public virtual IAggregateFluent<SearchMetaResult> SearchMeta(
232+
SearchDefinition<TResult> searchDefinition,
233+
string indexName = null,
234+
SearchCountOptions count = null)
235+
{
236+
throw new NotImplementedException();
237+
}
238+
230239
/// <inheritdoc />
231240
public virtual IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
232241
AggregateExpressionDefinition<ISetWindowFieldsPartition<TResult>, TWindowFields> output)

src/MongoDB.Driver/Builders.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ public static class Builders<TDocument>
3939
public static UpdateDefinitionBuilder<TDocument> Update { get; } = new UpdateDefinitionBuilder<TDocument>();
4040

4141
// Search builders
42+
/// <summary>Gets a <see cref="SearchFacetBuilder{TDocument}"/>.</summary>
43+
public static SearchFacetBuilder<TDocument> Facet { get; } = new SearchFacetBuilder<TDocument>();
44+
4245
/// <summary>Gets a <see cref="PathDefinition{TDocument}"/>.</summary>
4346
public static PathDefinitionBuilder<TDocument> Path { get; } = new PathDefinitionBuilder<TDocument>();
4447

4548
/// <summary>Gets a <see cref="ScoreDefinitionBuilder{TDocument}"/>.</summary>
4649
public static ScoreDefinitionBuilder<TDocument> Score { get; } = new ScoreDefinitionBuilder<TDocument>();
4750

51+
/// <summary>Gets a <see cref="ScoreFunctionBuilder{TDocument}"/>.</summary>
52+
public static ScoreFunctionBuilder<TDocument> ScoreFunction { get; } = new ScoreFunctionBuilder<TDocument>();
53+
4854
/// <summary>Gets a <see cref="SearchDefinitionBuilder{TDocument}"/>.</summary>
4955
public static SearchDefinitionBuilder<TDocument> Search { get; } = new SearchDefinitionBuilder<TDocument>();
56+
57+
/// <summary> Gets a <see cref="SpanDefinitionBuilder{TDocument}"/>.</summary>
58+
public static SpanDefinitionBuilder<TDocument> Span { get; } = new SpanDefinitionBuilder<TDocument>();
5059
}
5160
}

src/MongoDB.Driver/IAggregateFluent.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
357357
/// <summary>
358358
/// Appends a $search stage to the pipeline.
359359
/// </summary>
360-
/// <param name="query">The search definition.</param>
360+
/// <param name="searchDefinition">The search definition.</param>
361361
/// <param name="highlight">The highlight options.</param>
362362
/// <param name="indexName">The index name.</param>
363363
/// <param name="count">The count options.</param>
@@ -367,12 +367,24 @@ IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
367367
/// </param>
368368
/// <returns>The fluent aggregate interface.</returns>
369369
IAggregateFluent<TResult> Search(
370-
SearchDefinition<TResult> query,
370+
SearchDefinition<TResult> searchDefinition,
371371
HighlightOptions<TResult> highlight = null,
372372
string indexName = null,
373373
SearchCountOptions count = null,
374374
bool returnStoredSource = false);
375375

376+
/// <summary>
377+
/// Appends a $searchMeta stage to the pipeline.
378+
/// </summary>
379+
/// <param name="searchDefinition">The search definition.</param>
380+
/// <param name="indexName">The index name.</param>
381+
/// <param name="count">The count options.</param>
382+
/// <returns>The fluent aggregate interface.</returns>
383+
IAggregateFluent<SearchMetaResult> SearchMeta(
384+
SearchDefinition<TResult> searchDefinition,
385+
string indexName = null,
386+
SearchCountOptions count = null);
387+
376388
/// <summary>
377389
/// Appends a $setWindowFields to the pipeline.
378390
/// </summary>

src/MongoDB.Driver/PipelineDefinitionBuilder.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,28 @@ public static PipelineDefinition<TInput, TOutput> Search<TInput, TOutput>(
11921192
return pipeline.AppendStage(PipelineStageDefinitionBuilder.Search(searchDefinition, highlight, indexName, count, returnStoredSource));
11931193
}
11941194

1195+
/// <summary>
1196+
/// Appends a $searchMeta stage to the pipeline.
1197+
/// </summary>
1198+
/// <typeparam name="TInput">The type of the input documents.</typeparam>
1199+
/// <typeparam name="TOutput">The type of the output documents.</typeparam>
1200+
/// <param name="pipeline">The pipeline.</param>
1201+
/// <param name="query">The search definition.</param>
1202+
/// <param name="indexName">The index name.</param>
1203+
/// <param name="count">The count options.</param>
1204+
/// <returns>
1205+
/// A new pipeline with an additional stage.
1206+
/// </returns>
1207+
public static PipelineDefinition<TInput, SearchMetaResult> SearchMeta<TInput, TOutput>(
1208+
this PipelineDefinition<TInput, TOutput> pipeline,
1209+
SearchDefinition<TOutput> query,
1210+
string indexName = null,
1211+
SearchCountOptions count = null)
1212+
{
1213+
Ensure.IsNotNull(pipeline, nameof(pipeline));
1214+
return pipeline.AppendStage(PipelineStageDefinitionBuilder.SearchMeta(query, indexName, count));
1215+
}
1216+
11951217
/// <summary>
11961218
/// Create a $setWindowFields stage.
11971219
/// </summary>

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,40 @@ public static PipelineStageDefinition<TInput, TInput> Search<TInput>(
13491349
return stage;
13501350
}
13511351

1352+
/// <summary>
1353+
/// Creates a $searchMeta stage.
1354+
/// </summary>
1355+
/// <typeparam name="TInput">The type of the input documents.</typeparam>
1356+
/// <param name="searchDefinition">The search definition.</param>
1357+
/// <param name="indexName">The index name.</param>
1358+
/// <param name="count">The count options.</param>
1359+
/// <returns>The stage.</returns>
1360+
public static PipelineStageDefinition<TInput, SearchMetaResult> SearchMeta<TInput>(
1361+
SearchDefinition<TInput> searchDefinition,
1362+
string indexName = null,
1363+
SearchCountOptions count = null)
1364+
{
1365+
Ensure.IsNotNull(searchDefinition, nameof(searchDefinition));
1366+
1367+
const string operatorName = "$searchMeta";
1368+
var stage = new DelegatedPipelineStageDefinition<TInput, SearchMetaResult>(
1369+
operatorName,
1370+
(s, sr, linqProvider) =>
1371+
{
1372+
var renderedSearchDefinition = searchDefinition.Render(s, sr);
1373+
renderedSearchDefinition.Add("count", () => count.Render(), count != null);
1374+
renderedSearchDefinition.Add("index", indexName, indexName != null);
1375+
1376+
var document = new BsonDocument(operatorName, renderedSearchDefinition);
1377+
return new RenderedPipelineStageDefinition<SearchMetaResult>(
1378+
operatorName,
1379+
document,
1380+
sr.GetSerializer<SearchMetaResult>());
1381+
});
1382+
1383+
return stage;
1384+
}
1385+
13521386
/// <summary>
13531387
/// Creates a $replaceRoot stage.
13541388
/// </summary>

src/MongoDB.Driver/ProjectionDefinitionBuilder.cs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,40 @@ public static ProjectionDefinition<TDocument> Meta<TDocument>(this ProjectionDef
156156
return builder.Combine(projection, builder.Meta(field, metaFieldName));
157157
}
158158

159+
/// <summary>
160+
/// Combines an existing projection with a search highlights projection.
161+
/// </summary>
162+
/// <typeparam name="TDocument">The type of the document.</typeparam>
163+
/// <param name="projection">The projection.</param>
164+
/// <param name="field">The field.</param>
165+
/// <returns>
166+
/// A combined projection.
167+
/// </returns>
168+
public static ProjectionDefinition<TDocument> MetaSearchHighlights<TDocument>(
169+
this ProjectionDefinition<TDocument> projection,
170+
string field)
171+
{
172+
var builder = Builders<TDocument>.Projection;
173+
return builder.Combine(projection, builder.MetaSearchHighlights(field));
174+
}
175+
176+
/// <summary>
177+
/// Combines an existing projection with a search score projection.
178+
/// </summary>
179+
/// <typeparam name="TDocument">The type of the document.</typeparam>
180+
/// <param name="projection">The projection.</param>
181+
/// <param name="field">The field.</param>
182+
/// <returns>
183+
/// A combined projection.
184+
/// </returns>
185+
public static ProjectionDefinition<TDocument> MetaSearchScore<TDocument>(
186+
this ProjectionDefinition<TDocument> projection,
187+
string field)
188+
{
189+
var builder = Builders<TDocument>.Projection;
190+
return builder.Combine(projection, builder.MetaSearchScore(field));
191+
}
192+
159193
/// <summary>
160194
/// Combines an existing projection with a text score projection.
161195
/// </summary>
@@ -171,6 +205,40 @@ public static ProjectionDefinition<TDocument> MetaTextScore<TDocument>(this Proj
171205
return builder.Combine(projection, builder.MetaTextScore(field));
172206
}
173207

208+
/// <summary>
209+
/// Combines an existing projection with a search metadata projection.
210+
/// </summary>
211+
/// <typeparam name="TDocument">The type of the document.</typeparam>
212+
/// <param name="projection">The projection.</param>
213+
/// <param name="field">The field.</param>
214+
/// <returns>
215+
/// A combined projection.
216+
/// </returns>
217+
public static ProjectionDefinition<TDocument> SearchMeta<TDocument>(
218+
this ProjectionDefinition<TDocument> projection,
219+
FieldDefinition<TDocument> field)
220+
{
221+
var builder = Builders<TDocument>.Projection;
222+
return builder.Combine(projection, builder.SearchMeta(field));
223+
}
224+
225+
/// <summary>
226+
/// Combines an existing projection with a search metadata projection.
227+
/// </summary>
228+
/// <typeparam name="TDocument">The type of the document.</typeparam>
229+
/// <param name="projection">The projection.</param>
230+
/// <param name="field">The field.</param>
231+
/// <returns>
232+
/// A combined projection.
233+
/// </returns>
234+
public static ProjectionDefinition<TDocument> SearchMeta<TDocument>(
235+
this ProjectionDefinition<TDocument> projection,
236+
Expression<Func<TDocument, object>> field)
237+
{
238+
var builder = Builders<TDocument>.Projection;
239+
return builder.Combine(projection, builder.SearchMeta(field));
240+
}
241+
174242
/// <summary>
175243
/// Combines an existing projection with an array slice projection.
176244
/// </summary>
@@ -363,6 +431,30 @@ public ProjectionDefinition<TSource> Meta(string field, string metaFieldName)
363431
return new SingleFieldProjectionDefinition<TSource>(field, new BsonDocument("$meta", metaFieldName));
364432
}
365433

434+
/// <summary>
435+
/// Creates a search highlights projection.
436+
/// </summary>
437+
/// <param name="field">The field.</param>
438+
/// <returns>
439+
/// A search highlights projection.
440+
/// </returns>
441+
public ProjectionDefinition<TSource> MetaSearchHighlights(string field)
442+
{
443+
return Meta(field, "searchHighlights");
444+
}
445+
446+
/// <summary>
447+
/// Creates a search score projection.
448+
/// </summary>
449+
/// <param name="field">The field.</param>
450+
/// <returns>
451+
/// A search score projection.
452+
/// </returns>
453+
public ProjectionDefinition<TSource> MetaSearchScore(string field)
454+
{
455+
return Meta(field, "searchScore");
456+
}
457+
366458
/// <summary>
367459
/// Creates a text score projection.
368460
/// </summary>
@@ -375,6 +467,30 @@ public ProjectionDefinition<TSource> MetaTextScore(string field)
375467
return Meta(field, "textScore");
376468
}
377469

470+
/// <summary>
471+
/// Creates a search metadata projection.
472+
/// </summary>
473+
/// <param name="field">The field.</param>
474+
/// <returns>
475+
/// A search metadata projection.
476+
/// </returns>
477+
public ProjectionDefinition<TSource> SearchMeta(FieldDefinition<TSource> field)
478+
{
479+
return new SingleFieldProjectionDefinition<TSource>(field, new BsonString("$$SEARCH_META"));
480+
}
481+
482+
/// <summary>
483+
/// Creates a search metadata projection.
484+
/// </summary>
485+
/// <param name="field">The field.</param>
486+
/// <returns>
487+
/// A search metadata projection.
488+
/// </returns>
489+
public ProjectionDefinition<TSource> SearchMeta(Expression<Func<TSource, object>> field)
490+
{
491+
return SearchMeta(new ExpressionFieldDefinition<TSource>(field));
492+
}
493+
378494
/// <summary>
379495
/// Creates an array slice projection.
380496
/// </summary>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespace MongoDB.Driver.Search
16+
{
17+
/// <summary>
18+
/// The order in which to search for tokens in an autocomplete search definition.
19+
/// </summary>
20+
public enum AutocompleteTokenOrder
21+
{
22+
/// <summary>
23+
/// Indicates that tokens in the query can appear in any order in the documents.
24+
/// </summary>
25+
Any,
26+
27+
/// <summary>
28+
/// Indicates that tokens in the query must appear adjacent to each other or in the order
29+
/// specified in the query in the documents.
30+
/// </summary>
31+
Sequential
32+
}
33+
}

0 commit comments

Comments
 (0)