Skip to content

Commit 8820b1f

Browse files
committed
CSHARP-1412: Add support for text search options (1.x API).
1 parent b27ea35 commit 8820b1f

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

MongoDB.Driver/Builders/QueryBuilder.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ public static IMongoQuery WithinRectangle(string name, double lowerLeftX, double
817817
/// <returns>An IMongoQuery that represents the text search.</returns>
818818
public static IMongoQuery Text(string searchString)
819819
{
820-
return Text(searchString, null);
820+
return Text(searchString, new TextSearchOptions());
821821
}
822822

823823
/// <summary>
@@ -833,10 +833,29 @@ public static IMongoQuery Text(string searchString, string language)
833833
{
834834
throw new ArgumentNullException("searchString");
835835
}
836+
var options = new TextSearchOptions { Language = language };
837+
return Text(searchString, options);
838+
}
839+
840+
/// <summary>
841+
/// Generate a text search query that tests whether the given search string is present using the specified language's rules.
842+
/// Specifies use of language appropriate stop words, stemming rules etc.
843+
/// </summary>
844+
/// <param name="searchString">The search string.</param>
845+
/// <param name="options">The text search options.</param>
846+
/// <returns>An IMongoQuery that represents the text search for the particular language.</returns>
847+
public static IMongoQuery Text(string searchString, TextSearchOptions options)
848+
{
849+
if (options == null)
850+
{
851+
throw new ArgumentNullException("options");
852+
}
836853
var condition = new BsonDocument
837854
{
838855
{ "$search", searchString },
839-
{ "$language", language, language != null }
856+
{ "$language", options.Language, options.Language != null },
857+
{ "$caseSensitive", () => options.CaseSensitive.Value, options.CaseSensitive.HasValue },
858+
{ "$diacriticSensitive", () => options.DiacriticSensitive.Value, options.DiacriticSensitive.HasValue }
840859
};
841860
return new QueryDocument("$text", condition);
842861
}

MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="Operations\ListIndexesOperation.cs" />
9898
<Compile Include="Operations\ParallelScanOperation.cs" />
9999
<Compile Include="ParallelScanArgs.cs" />
100+
<Compile Include="TextSearchOptions.cs" />
100101
<Compile Include="WriteConcernError.cs" />
101102
<Compile Include="BulkWriteError.cs" />
102103
<Compile Include="BulkWriteResult.cs" />

MongoDB.Driver/TextSearchOptions.cs

Lines changed: 56 additions & 0 deletions
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+
}

MongoDB.DriverUnitTests/Builders/QueryBuilderTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,25 @@ public void TestTextWithLanguageQueryGeneration()
847847
[Test]
848848
public void TestTextQueryGenerationWithNullLanguage()
849849
{
850-
var query = Query.Text("foo", null);
850+
var query = Query.Text("foo", (string)null);
851851
var expected = "{ \"$text\" : { \"$search\" : \"foo\" } }";
852852
Assert.AreEqual(expected, query.ToJson());
853853
}
854854

855+
[Test]
856+
public void TestTextWithOptionsQueryGeneration()
857+
{
858+
var options = new TextSearchOptions
859+
{
860+
Language = "norwegian",
861+
CaseSensitive = true,
862+
DiacriticSensitive = true
863+
};
864+
var query = Query.Text("foo", options);
865+
var expected = "{ \"$text\" : { \"$search\" : \"foo\", \"$language\" : \"norwegian\", \"$caseSensitive\" : true, \"$diacriticSensitive\" : true } }";
866+
Assert.AreEqual(expected, query.ToJson());
867+
}
868+
855869
[Test]
856870
public void TestText()
857871
{

0 commit comments

Comments
 (0)