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.
This commit is contained in:
Christoph Strobl
2016-07-11 12:30:09 +02:00
committed by Mark Paluch
parent 116baf9a92
commit ac55f5e77f
3 changed files with 66 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,6 +36,8 @@ public class TextCriteria implements CriteriaDefinition {
private final List<Term> terms;
private String language;
private Boolean caseSensitive;
private Boolean diacriticSensitive;
/**
* Creates a new {@link TextCriteria}.
@@ -63,8 +65,8 @@ public class TextCriteria implements CriteriaDefinition {
}
/**
* For a full list of supported languages see the mongodb reference manual for <a
* href="http://docs.mongodb.org/manual/reference/text-search-languages/">Text Search Languages</a>.
* For a full list of supported languages see the mongodb reference manual for
* <a href="http://docs.mongodb.org/manual/reference/text-search-languages/">Text Search Languages</a>.
*
* @param language
* @return
@@ -167,6 +169,32 @@ public class TextCriteria implements CriteriaDefinition {
return this;
}
/**
* Optionally enable or disable case sensitive search.
*
* @param caseSensitive boolean flag endable/disable.
* @return never {@literal null}.
* @since 1.10
*/
public TextCriteria caseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
return this;
}
/**
* Optionallly enable or disable diacritic sensitive search against version 3 text indexes.
*
* @param diacriticSensitive boolean flag endable/disable.
* @return never {@literal null}.
* @since 1.10
*/
public TextCriteria diacriticSensitive(boolean diacriticSensitive) {
this.diacriticSensitive = diacriticSensitive;
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getKey()
@@ -193,6 +221,14 @@ public class TextCriteria implements CriteriaDefinition {
builder.add("$search", join(terms));
}
if (caseSensitive != null) {
builder.add("$caseSensitive", caseSensitive);
}
if (diacriticSensitive != null) {
builder.add("$diacriticSensitive", diacriticSensitive);
}
return new BasicDBObject("$text", builder.get());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import org.junit.Test;
import org.springframework.data.mongodb.core.DBObjectTestUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
@@ -115,6 +116,29 @@ public class TextCriteriaUnitTests {
IsEqual.<DBObject> equalTo(new BasicDBObject("$search", "-\"coffee cake\"")));
}
/**
* @see DATAMONGO-1455
*/
@Test
public void caseSensitiveOperatorShouldBeSetCorrectly() {
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").caseSensitive(true);
Assert.assertThat(DBObjectTestUtils.getAsDBObject(criteria.getCriteriaObject(), "$text"), IsEqual
.<DBObject> equalTo(new BasicDBObjectBuilder().add("$search", "coffee").add("$caseSensitive", true).get()));
}
/**
* @see DATAMONGO-1456
*/
@Test
public void diacriticSensitiveOperatorShouldBeSetCorrectly() {
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").diacriticSensitive(true);
Assert.assertThat(DBObjectTestUtils.getAsDBObject(criteria.getCriteriaObject(), "$text"),
IsEqual.<DBObject> equalTo(
new BasicDBObjectBuilder().add("$search", "coffee").add("$diacriticSensitive", true).get()));
}
private DBObject searchObject(String json) {
return new BasicDBObject("$text", JSON.parse(json));
}

View File

@@ -1356,6 +1356,8 @@ TextQuery.searching(new TextCriteria().matching("\"coffee cake\""));
TextQuery.searching(new TextCriteria().phrase("coffee cake"));
----
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.
include::../{spring-data-commons-docs}/query-by-example.adoc[leveloffset=+1]
include::query-by-example.adoc[leveloffset=+1]