Skip to content

Commit 92d8d55

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: #375.
1 parent 280377e commit 92d8d55

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

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

+37-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.
@@ -33,6 +33,8 @@ public class TextCriteria implements CriteriaDefinition {
3333

3434
private final List<Term> terms;
3535
private String language;
36+
private Boolean caseSensitive;
37+
private Boolean diacriticSensitive;
3638

3739
/**
3840
* Creates a new {@link TextCriteria}.
@@ -164,6 +166,32 @@ public TextCriteria matchingPhrase(String phrase) {
164166
return this;
165167
}
166168

169+
/**
170+
* Optionally enable or disable case sensitive search.
171+
*
172+
* @param caseSensitive boolean flag endable/disable.
173+
* @return never {@literal null}.
174+
* @since 1.10
175+
*/
176+
public TextCriteria caseSensitive(boolean caseSensitive) {
177+
178+
this.caseSensitive = caseSensitive;
179+
return this;
180+
}
181+
182+
/**
183+
* Optionallly enable or disable diacritic sensitive search against version 3 text indexes.
184+
*
185+
* @param diacriticSensitive boolean flag endable/disable.
186+
* @return never {@literal null}.
187+
* @since 1.10
188+
*/
189+
public TextCriteria diacriticSensitive(boolean diacriticSensitive) {
190+
191+
this.diacriticSensitive = diacriticSensitive;
192+
return this;
193+
}
194+
167195
/*
168196
* (non-Javadoc)
169197
* @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getKey()
@@ -190,6 +218,14 @@ public Document getCriteriaObject() {
190218
document.put("$search", join(terms));
191219
}
192220

221+
if (caseSensitive != null) {
222+
document.put("$caseSensitive", caseSensitive);
223+
}
224+
225+
if (diacriticSensitive != null) {
226+
document.put("$diacriticSensitive", diacriticSensitive);
227+
}
228+
193229
return new Document("$text", document);
194230
}
195231

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

+33-10
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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.core.query;
1717

18+
import static org.hamcrest.core.IsEqual.*;
19+
1820
import org.bson.Document;
1921
import org.hamcrest.core.IsEqual;
2022
import org.junit.Assert;
@@ -35,7 +37,7 @@ public class TextCriteriaUnitTests {
3537
public void shouldNotHaveLanguageField() {
3638

3739
TextCriteria criteria = TextCriteria.forDefaultLanguage();
38-
Assert.assertThat(criteria.getCriteriaObject(), IsEqual.equalTo(searchObject("{ }")));
40+
Assert.assertThat(criteria.getCriteriaObject(), equalTo(searchObject("{ }")));
3941
}
4042

4143
/**
@@ -45,7 +47,7 @@ public void shouldNotHaveLanguageField() {
4547
public void shouldNotHaveLanguageForNonDefaultLanguageField() {
4648

4749
TextCriteria criteria = TextCriteria.forLanguage("spanish");
48-
Assert.assertThat(criteria.getCriteriaObject(), IsEqual.equalTo(searchObject("{ \"$language\" : \"spanish\" }")));
50+
Assert.assertThat(criteria.getCriteriaObject(), equalTo(searchObject("{ \"$language\" : \"spanish\" }")));
4951
}
5052

5153
/**
@@ -55,7 +57,7 @@ public void shouldNotHaveLanguageForNonDefaultLanguageField() {
5557
public void shouldCreateSearchFieldForSingleTermCorrectly() {
5658

5759
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("cake");
58-
Assert.assertThat(criteria.getCriteriaObject(), IsEqual.equalTo(searchObject("{ \"$search\" : \"cake\" }")));
60+
Assert.assertThat(criteria.getCriteriaObject(), equalTo(searchObject("{ \"$search\" : \"cake\" }")));
5961
}
6062

6163
/**
@@ -65,8 +67,7 @@ public void shouldCreateSearchFieldForSingleTermCorrectly() {
6567
public void shouldCreateSearchFieldCorrectlyForMultipleTermsCorrectly() {
6668

6769
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("bake", "coffee", "cake");
68-
Assert.assertThat(criteria.getCriteriaObject(),
69-
IsEqual.equalTo(searchObject("{ \"$search\" : \"bake coffee cake\" }")));
70+
Assert.assertThat(criteria.getCriteriaObject(), equalTo(searchObject("{ \"$search\" : \"bake coffee cake\" }")));
7071
}
7172

7273
/**
@@ -87,7 +88,7 @@ public void shouldCreateSearchFieldForPhraseCorrectly() {
8788
public void shouldCreateNotFieldCorrectly() {
8889

8990
TextCriteria criteria = TextCriteria.forDefaultLanguage().notMatching("cake");
90-
Assert.assertThat(criteria.getCriteriaObject(), IsEqual.equalTo(searchObject("{ \"$search\" : \"-cake\" }")));
91+
Assert.assertThat(criteria.getCriteriaObject(), equalTo(searchObject("{ \"$search\" : \"-cake\" }")));
9192
}
9293

9394
/**
@@ -97,8 +98,7 @@ public void shouldCreateNotFieldCorrectly() {
9798
public void shouldCreateSearchFieldCorrectlyForNotMultipleTermsCorrectly() {
9899

99100
TextCriteria criteria = TextCriteria.forDefaultLanguage().notMatchingAny("bake", "coffee", "cake");
100-
Assert.assertThat(criteria.getCriteriaObject(),
101-
IsEqual.equalTo(searchObject("{ \"$search\" : \"-bake -coffee -cake\" }")));
101+
Assert.assertThat(criteria.getCriteriaObject(), equalTo(searchObject("{ \"$search\" : \"-bake -coffee -cake\" }")));
102102
}
103103

104104
/**
@@ -109,7 +109,30 @@ public void shouldCreateSearchFieldForNotPhraseCorrectly() {
109109

110110
TextCriteria criteria = TextCriteria.forDefaultLanguage().notMatchingPhrase("coffee cake");
111111
Assert.assertThat(DBObjectTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"),
112-
IsEqual.<Document> equalTo(new Document("$search", "-\"coffee cake\"")));
112+
equalTo(new Document("$search", "-\"coffee cake\"")));
113+
}
114+
115+
/**
116+
* @see DATAMONGO-1455
117+
*/
118+
@Test
119+
public void caseSensitiveOperatorShouldBeSetCorrectly() {
120+
121+
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").caseSensitive(true);
122+
123+
assertThat(DBObjectTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"),
124+
equalTo(Document.parse("{ \"$search\" : \"coffee\", \"$caseSensitive\" : true }")));
125+
}
126+
127+
/**
128+
* @see DATAMONGO-1456
129+
*/
130+
@Test
131+
public void diacriticSensitiveOperatorShouldBeSetCorrectly() {
132+
133+
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").diacriticSensitive(true);
134+
assertThat(DBObjectTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"),
135+
equalTo(Document.parse("{ \"$search\" : \"coffee\", \"$diacriticSensitive\" : true }")));
113136
}
114137

115138
private Document searchObject(String json) {

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)