Skip to content

Commit edf672c

Browse files
committed
CSHARP-1412: Support new text search features introduced with v3 text indexes.
1 parent 07d2fa1 commit edf672c

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

src/MongoDB.Driver.Tests/FilterDefinitionBuilderTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,9 @@ public void Text()
806806
var subject = CreateSubject<BsonDocument>();
807807
Assert(subject.Text("funny"), "{$text: {$search: 'funny'}}");
808808
Assert(subject.Text("funny", "en"), "{$text: {$search: 'funny', $language: 'en'}}");
809+
Assert(subject.Text("funny", new TextSearchOptions { Language = "en" }), "{$text: {$search: 'funny', $language: 'en'}}");
810+
Assert(subject.Text("funny", new TextSearchOptions { CaseSensitive = true }), "{$text: {$search: 'funny', $caseSensitive: true}}");
811+
Assert(subject.Text("funny", new TextSearchOptions { DiacriticSensitive = true }), "{$text: {$search: 'funny', $diacriticSensitive: true}}");
809812
}
810813

811814
[Test]

src/MongoDB.Driver/FilterDefinitionBuilder.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1121,18 +1121,32 @@ public FilterDefinition<TDocument> SizeLte(Expression<Func<TDocument, object>> f
11211121
/// Creates a text filter.
11221122
/// </summary>
11231123
/// <param name="search">The search.</param>
1124-
/// <param name="language">The language.</param>
1124+
/// <param name="options">The text search options.</param>
11251125
/// <returns>A text filter.</returns>
1126-
public FilterDefinition<TDocument> Text(string search, string language = null)
1126+
public FilterDefinition<TDocument> Text(string search, TextSearchOptions options = null)
11271127
{
11281128
var document = new BsonDocument
11291129
{
11301130
{ "$search", search },
1131-
{ "$language", language, language != null }
1131+
{ "$language", () => options.Language, options != null && options.Language != null },
1132+
{ "$caseSensitive", () => options.CaseSensitive.Value, options != null && options.CaseSensitive.HasValue },
1133+
{ "$diacriticSensitive", () => options.DiacriticSensitive.Value, options != null && options.DiacriticSensitive.HasValue }
11321134
};
11331135
return new BsonDocumentFilterDefinition<TDocument>(new BsonDocument("$text", document));
11341136
}
11351137

1138+
/// <summary>
1139+
/// Creates a text filter.
1140+
/// </summary>
1141+
/// <param name="search">The search.</param>
1142+
/// <param name="language">The language.</param>
1143+
/// <returns>A text filter.</returns>
1144+
public FilterDefinition<TDocument> Text(string search, string language)
1145+
{
1146+
var options = new TextSearchOptions { Language = language };
1147+
return Text(search, options);
1148+
}
1149+
11361150
/// <summary>
11371151
/// Creates a type filter.
11381152
/// </summary>

src/MongoDB.Driver/MongoDB.Driver.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
<Compile Include="Sync\AsyncCursorEnumerableAdapter.cs" />
271271
<Compile Include="Sync\AsyncCursorEnumeratorAdapter.cs" />
272272
<Compile Include="AggregateUnwindOptions.cs" />
273+
<Compile Include="TextSearchOptions.cs" />
273274
<Compile Include="UpdateDefinitionBuilder.cs" />
274275
<Compile Include="OfTypeMongoCollection.cs" />
275276
<Compile Include="IFilteredMongoCollection.cs" />
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Copyright 2015 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+
namespace MongoDB.Driver
17+
{
18+
/// <summary>
19+
/// Represents text search options.
20+
/// </summary>
21+
public class TextSearchOptions
22+
{
23+
// private fields
24+
private bool? _caseSensitive;
25+
private bool? _diacriticSensitive;
26+
private string _language;
27+
28+
// public properties
29+
/// <summary>
30+
/// Gets or sets whether a text search should be case sensitive.
31+
/// </summary>
32+
public bool? CaseSensitive
33+
{
34+
get { return _caseSensitive; }
35+
set { _caseSensitive = value; }
36+
}
37+
38+
/// <summary>
39+
/// Gets or sets whether a text search should be diacritic sensitive.
40+
/// </summary>
41+
public bool? DiacriticSensitive
42+
{
43+
get { return _diacriticSensitive; }
44+
set { _diacriticSensitive = value; }
45+
}
46+
47+
/// <summary>
48+
/// Gets or sets the language for a text search.
49+
/// </summary>
50+
public string Language
51+
{
52+
get { return _language; }
53+
set { _language = value; }
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)