diff --git a/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java b/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java index 39ca60b7f..f753bc3fd 100644 --- a/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java +++ b/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors + * Copyright 2012-2021 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. @@ -34,6 +34,10 @@ import com.couchbase.client.core.error.IndexExistsException; import com.couchbase.client.java.Cluster; +/** + * @author Michael Nitschinger + * @author Michael Reiche + */ public class CouchbasePersistentEntityIndexCreator implements ApplicationListener> { private static final Logger LOGGER = LoggerFactory.getLogger(CouchbasePersistentEntityIndexCreator.class); @@ -95,7 +99,8 @@ private void checkForAndCreateIndexes(final CouchbasePersistentEntity entity) private void createIndex(final IndexDefinitionHolder indexToCreate) { Cluster cluster = couchbaseOperations.getCouchbaseClientFactory().getCluster(); - StringBuilder statement = new StringBuilder("CREATE INDEX ").append(indexToCreate.getIndexName()).append(" ON `") + StringBuilder statement = new StringBuilder("CREATE INDEX `") + .append(indexToCreate.getIndexName()).append("` ON `") .append(couchbaseOperations.getBucketName()).append("` (") .append(String.join(",", indexToCreate.getIndexFields())).append(")"); diff --git a/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexResolver.java b/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexResolver.java index 4ef36a4de..a92369e6c 100644 --- a/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexResolver.java +++ b/src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors + * Copyright 2012-2021 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. @@ -32,6 +32,10 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; +/** + * @author Michael Nitschinger + * @author Michael Reiche + */ public class CouchbasePersistentEntityIndexResolver implements QueryIndexResolver { private final MappingContext, CouchbasePersistentProperty> mappingContext; @@ -103,7 +107,8 @@ protected IndexDefinitionHolder createFieldQueryIndexDefinition(final CouchbaseP String fieldName = index.name().isEmpty() ? property.getFieldName() : index.name(); fields.add(fieldName + (index.direction() == QueryIndexDirection.DESCENDING ? " DESC" : "")); - String indexName = "idx_" + StringUtils.uncapitalize(entity.getType().getSimpleName()) + "_" + fieldName; + String indexName = "idx_" + StringUtils.uncapitalize(entity.getType().getSimpleName()) + "_" + + fieldName.replace(".", "_"); return new IndexDefinitionHolder(fields, indexName, getPredicate(entityInfo)); } @@ -126,8 +131,8 @@ protected List createCompositeQueryIndexDefinitions(final return indexAnnotations.stream().map(ann -> { List fields = Arrays.asList(ann.fields()); - String fieldsIndexName = String.join("_", fields).toLowerCase().replace(" ", "").replace("asc", "") - .replace("desc", ""); + String fieldsIndexName = String.join("_", fields).toLowerCase().replace(".", "_").replace(" ", "") + .replace("asc", "").replace("desc", ""); String indexName = "idx_" + StringUtils.uncapitalize(entity.getType().getSimpleName()) + "_" + fieldsIndexName; return new IndexDefinitionHolder(fields, indexName, predicate); diff --git a/src/test/java/org/springframework/data/couchbase/domain/Airline.java b/src/test/java/org/springframework/data/couchbase/domain/Airline.java index 402412013..157669558 100644 --- a/src/test/java/org/springframework/data/couchbase/domain/Airline.java +++ b/src/test/java/org/springframework/data/couchbase/domain/Airline.java @@ -23,6 +23,7 @@ @Document @CompositeQueryIndex(fields = { "id", "name desc" }) +@CompositeQueryIndex(fields = { "id.something", "name desc" }) public class Airline extends ComparableEntity { @Id String id; diff --git a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java index 42bcd73be..375dc3555 100644 --- a/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java +++ b/src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java @@ -62,6 +62,14 @@ void createsCompositeIndex() { assertTrue(foundIndex.get().condition().get().contains("_class")); } + @Test + void createsCompositeIndexWithPath() { + Optional foundIndex = cluster.queryIndexes().getAllIndexes(bucketName()).stream() + .filter(i -> i.name().equals("idx_airline_id_something_name")).findFirst(); + + assertTrue(foundIndex.isPresent()); + assertTrue(foundIndex.get().condition().get().contains("_class")); + } @Configuration @EnableCouchbaseRepositories("org.springframework.data.couchbase") static class Config extends AbstractCouchbaseConfiguration {