DATAMONGO-1569 - Add support for partial filter expressions in compound index annotations

Original Pull Request: #738
This commit is contained in:
Dave Perryman
2019-04-09 15:20:55 +01:00
committed by Christoph Strobl
parent a254576a6e
commit 41e60e5c25
3 changed files with 37 additions and 1 deletions

View File

@@ -46,6 +46,7 @@ import java.lang.annotation.Target;
* @author Philipp Schneider
* @author Johno Crawford
* @author Christoph Strobl
* @author Dave Perryman
*/
@Target({ ElementType.TYPE })
@Documented
@@ -170,4 +171,13 @@ public @interface CompoundIndex {
*/
boolean background() default false;
/**
* Only index the documents in a collection that meet a specified {@link IndexFilter filter expression}.
*
* @return
* @see <a href=
* "https://docs.mongodb.com/manual/core/index-partial/">https://docs.mongodb.com/manual/core/index-partial/</a>
*/
String partial() default "";
}

View File

@@ -69,6 +69,7 @@ import org.springframework.util.StringUtils;
* @author Thomas Darimont
* @author Martin Macko
* @author Mark Paluch
* @author Dave Perryman
* @since 1.5
*/
public class MongoPersistentEntityIndexResolver implements IndexResolver {
@@ -380,6 +381,10 @@ public class MongoPersistentEntityIndexResolver implements IndexResolver {
indexDefinition.background();
}
if (StringUtils.hasText(index.partial())) {
indexDefinition.partial(PartialIndexFilter.of(org.bson.Document.parse(index.partial())));
}
return new IndexDefinitionHolder(dotPath, indexDefinition, collection);
}

View File

@@ -25,7 +25,7 @@ import java.lang.annotation.Target;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@@ -54,6 +54,7 @@ import org.springframework.data.util.ClassTypeInformation;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Dave Perryman
*/
@RunWith(Suite.class)
@SuiteClasses({ IndexResolutionTests.class, GeoSpatialIndexResolutionTests.class, CompoundIndexResolutionTests.class,
@@ -663,6 +664,21 @@ public class MongoPersistentEntityIndexResolverUnitTests {
indexDefinitions.get(1));
}
@Test // DATAMONGO-1569
public void singleIndexWithPartialFilter() {
List<IndexDefinitionHolder> indexDefinitions = prepareMappingContextAndResolveIndexForType(
SingleCompoundIndexWithPartialFilter.class);
assertThat(indexDefinitions).hasSize(1);
assertThat(indexDefinitions.get(0).getIndexKeys()).containsEntry("foo", 1).containsEntry("bar", -1);
assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("name", "compound_index_with_partial")
.containsEntry("unique", true).containsEntry("background", true);
assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("partialFilterExpression",
new org.bson.Document().append("bar", new org.bson.Document().append("$exists", true)));
}
@Document("CompoundIndexOnLevelOne")
static class CompoundIndexOnLevelOne {
@@ -733,6 +749,11 @@ public class MongoPersistentEntityIndexResolverUnitTests {
@CompoundIndex(name = "cmp-idx-one", def = "{'firstname': 1, 'lastname': -1}")
@CompoundIndex(name = "cmp-idx-two", def = "{'address.city': -1, 'address.street': 1}")
static class RepeatedCompoundIndex {}
@Document("SingleCompoundIndexWithPartialFilter")
@CompoundIndex(name = "compound_index_with_partial", def = "{'foo': 1, 'bar': -1}", background = true,
unique = true, partial = "{'bar': {$exists: true}}")
static class SingleCompoundIndexWithPartialFilter {}
}
public static class TextIndexedResolutionTests {