diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java index 7f2ee8403..da2bbfde5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/CompoundIndex.java @@ -22,6 +22,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.data.mongodb.core.mapping.Document; /** * Mark a class to use compound indexes.
*

@@ -166,17 +167,19 @@ public @interface CompoundIndex { String partialFilter() default ""; /** - * The actual collation definition in JSON format or a {@link org.springframework.expression.spel.standard.SpelExpression - * template expression} resolving to either a JSON String or a {@link org.bson.Document}. The keys of the JSON - * document are configuration options for the collation (language-specific rules for string comparison). - *

- * TODO write code documentation & example!!! - *
+ * The actual collation definition in JSON format or a + * {@link org.springframework.expression.spel.standard.SpelExpression template expression} resolving to either a JSON + * String or a {@link org.bson.Document}. The keys of the JSON document are configuration options for the collation + * (language-specific rules for string comparison) to be applied on string properties being part of the index. + *

+ * NOTE: Overrides {@link Document#collation()}. + *

+ * * * @return empty String by default. * @see https://www.mongodb.com/docs/manual/reference/collation/ - * @since 3.4 + * "https://www.mongodb.com/docs/manual/reference/collation/">https://www.mongodb.com/docs/manual/reference/collation/ + * @since 4.0 */ String collation() default ""; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java index ca82e95dc..bc94563a6 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/Indexed.java @@ -20,6 +20,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.data.mongodb.core.mapping.Document; /** * Mark a field to be indexed using MongoDB's indexing feature. * @@ -176,12 +177,16 @@ public @interface Indexed { String partialFilter() default ""; /** - * Apply collation configuration for field
+ * The actual collation definition in JSON format or a + * {@link org.springframework.expression.spel.standard.SpelExpression template expression} resolving to either a JSON + * String or a {@link org.bson.Document}. The keys of the JSON document are configuration options for the collation + * (language-specific rules for string comparison) applied to the indexed based on the field value. + *

+ * NOTE: Overrides {@link Document#collation()}. * * @return empty by default. - * @see https://www.mongodb.com/docs/manual/reference/collation// - * @since 3.1 + * @see https://www.mongodb.com/docs/manual/reference/collation/ + * @since 4.0 */ String collation() default ""; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index 4e4f308c6..dc60253d9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -455,7 +455,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { } if (StringUtils.hasText(index.collation())) { - indexDefinition.collation(Collation.parse(index.collation())); + indexDefinition.collation(evaluateCollation(index.collation(), entity)); } return new IndexDefinitionHolder(dotPath, indexDefinition, collection); @@ -578,7 +578,7 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver { } if (StringUtils.hasText(index.collation())) { - indexDefinition.collation(Collation.parse(index.collation())); + indexDefinition.collation(evaluateCollation(index.collation(), persistentProperty.getOwner())); } return new IndexDefinitionHolder(dotPath, indexDefinition, collection); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java index f0d642911..ff7c7c765 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexInfoUnitTests.java @@ -93,7 +93,7 @@ public class IndexInfoUnitTests { .contains(new Document("fieldA", 0).append("fieldB.fieldC", 0)); } - @Test // DATAMONGO-2133 + @Test // GH-3002 public void collationParsedCorrectly() { assertThat(getIndexInfo(INDEX_WITH_COLLATION).getCollation()) .contains(Document.parse("{ \"locale\": \"en_US\", \"strength\": 2 }")); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 3e4e4226b..2cbd2436d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -700,7 +700,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { org.bson.Document.parse("{'value': {'$exists': true}}")); } - @Test // DATAMONGO-2133 + @Test // GH-3002 public void compoundIndexWithCollation() { List indexDefinitions = prepareMappingContextAndResolveIndexForType( @@ -1419,7 +1419,7 @@ public class MongoPersistentEntityIndexResolverUnitTests { assertThat(indexDefinitions).hasSize(1); } - @Test // DATAMONGO-2133 + @Test // GH-3002 public void indexedWithCollation() { List indexDefinitions = prepareMappingContextAndResolveIndexForType(