Skip to content

Commit ac55f5e

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1455, DATAMONGO-1456 - Add support for $caseSensitive and $diacriticSensitive to $text.
We added methods to set values for $caseSensitive and $diacriticSensitive when using TextCriteria. Both operators are optional and will not be used until explicitly set. // { "$text" : { "$search" : "coffee" , "$caseSensitive" : true } } TextCriteria.forDefaultLanguage().matching("coffee").caseSensitive(true); // { "$text" : { "$search" : "coffee" , "$diacriticSensitive" : true } } TextCriteria.forDefaultLanguage().matching("coffee").diacriticSensitive(true); Original pull request: spring-projects#375.
1 parent 116baf9 commit ac55f5e

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/TextCriteria.java

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@ public class TextCriteria implements CriteriaDefinition {
3636

3737
private final List<Term> terms;
3838
private String language;
39+
private Boolean caseSensitive;
40+
private Boolean diacriticSensitive;
3941

4042
/**
4143
* Creates a new {@link TextCriteria}.
@@ -63,8 +65,8 @@ public static TextCriteria forDefaultLanguage() {
6365
}
6466

6567
/**
66-
* For a full list of supported languages see the mongodb reference manual for <a
67-
* href="http://docs.mongodb.org/manual/reference/text-search-languages/">Text Search Languages</a>.
68+
* For a full list of supported languages see the mongodb reference manual for
69+
* <a href="http://docs.mongodb.org/manual/reference/text-search-languages/">Text Search Languages</a>.
6870
*
6971
* @param language
7072
* @return
@@ -167,6 +169,32 @@ public TextCriteria matchingPhrase(String phrase) {
167169
return this;
168170
}
169171

172+
/**
173+
* Optionally enable or disable case sensitive search.
174+
*
175+
* @param caseSensitive boolean flag endable/disable.
176+
* @return never {@literal null}.
177+
* @since 1.10
178+
*/
179+
public TextCriteria caseSensitive(boolean caseSensitive) {
180+
181+
this.caseSensitive = caseSensitive;
182+
return this;
183+
}
184+
185+
/**
186+
* Optionallly enable or disable diacritic sensitive search against version 3 text indexes.
187+
*
188+
* @param diacriticSensitive boolean flag endable/disable.
189+
* @return never {@literal null}.
190+
* @since 1.10
191+
*/
192+
public TextCriteria diacriticSensitive(boolean diacriticSensitive) {
193+
194+
this.diacriticSensitive = diacriticSensitive;
195+
return this;
196+
}
197+
170198
/*
171199
* (non-Javadoc)
172200
* @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getKey()
@@ -193,6 +221,14 @@ public DBObject getCriteriaObject() {
193221
builder.add("$search", join(terms));
194222
}
195223

224+
if (caseSensitive != null) {
225+
builder.add("$caseSensitive", caseSensitive);
226+
}
227+
228+
if (diacriticSensitive != null) {
229+
builder.add("$diacriticSensitive", diacriticSensitive);
230+
}
231+
196232
return new BasicDBObject("$text", builder.get());
197233
}
198234

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextCriteriaUnitTests.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.data.mongodb.core.DBObjectTestUtils;
2222

2323
import com.mongodb.BasicDBObject;
24+
import com.mongodb.BasicDBObjectBuilder;
2425
import com.mongodb.DBObject;
2526
import com.mongodb.util.JSON;
2627

@@ -115,6 +116,29 @@ public void shouldCreateSearchFieldForNotPhraseCorrectly() {
115116
IsEqual.<DBObject> equalTo(new BasicDBObject("$search", "-\"coffee cake\"")));
116117
}
117118

119+
/**
120+
* @see DATAMONGO-1455
121+
*/
122+
@Test
123+
public void caseSensitiveOperatorShouldBeSetCorrectly() {
124+
125+
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").caseSensitive(true);
126+
Assert.assertThat(DBObjectTestUtils.getAsDBObject(criteria.getCriteriaObject(), "$text"), IsEqual
127+
.<DBObject> equalTo(new BasicDBObjectBuilder().add("$search", "coffee").add("$caseSensitive", true).get()));
128+
}
129+
130+
/**
131+
* @see DATAMONGO-1456
132+
*/
133+
@Test
134+
public void diacriticSensitiveOperatorShouldBeSetCorrectly() {
135+
136+
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").diacriticSensitive(true);
137+
Assert.assertThat(DBObjectTestUtils.getAsDBObject(criteria.getCriteriaObject(), "$text"),
138+
IsEqual.<DBObject> equalTo(
139+
new BasicDBObjectBuilder().add("$search", "coffee").add("$diacriticSensitive", true).get()));
140+
}
141+
118142
private DBObject searchObject(String json) {
119143
return new BasicDBObject("$text", JSON.parse(json));
120144
}

src/main/asciidoc/reference/mongodb.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,8 @@ TextQuery.searching(new TextCriteria().matching("\"coffee cake\""));
13561356
TextQuery.searching(new TextCriteria().phrase("coffee cake"));
13571357
----
13581358

1359+
The flags for `$caseSensitive` and `$diacriticSensitive` can be set via the according methods on `TextCriteria`. Please note that these two optional flags have been introduced in MongoDB 3.2 and will not be included in the query unless explicitly set.
1360+
13591361
include::../{spring-data-commons-docs}/query-by-example.adoc[leveloffset=+1]
13601362
include::query-by-example.adoc[leveloffset=+1]
13611363

0 commit comments

Comments
 (0)