From ac55f5e77f12488f12bf2153b757a2560b6b56c8 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 11 Jul 2016 12:30:09 +0200 Subject: [PATCH] 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. --- .../data/mongodb/core/query/TextCriteria.java | 42 +++++++++++++++++-- .../core/query/TextCriteriaUnitTests.java | 26 +++++++++++- src/main/asciidoc/reference/mongodb.adoc | 2 + 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/TextCriteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/TextCriteria.java index 61bd20958..c2637e35a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/TextCriteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/TextCriteria.java @@ -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 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 Text Search Languages. + * For a full list of supported languages see the mongodb reference manual for + * Text Search Languages. * * @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()); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextCriteriaUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextCriteriaUnitTests.java index 9a14e7d42..36a44a898 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextCriteriaUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextCriteriaUnitTests.java @@ -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. 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 + . 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. equalTo( + new BasicDBObjectBuilder().add("$search", "coffee").add("$diacriticSensitive", true).get())); + } + private DBObject searchObject(String json) { return new BasicDBObject("$text", JSON.parse(json)); } diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index e6697721a..42a4075bf 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -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]