Skip to content

Commit e0b6873

Browse files
committed
Handle auto index on field path. (#1169)
Closes #1166. Co-authored-by: mikereiche <michael.reiche@couchbase.com>
1 parent e8f04ec commit e0b6873

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,10 @@
3434
import com.couchbase.client.core.error.IndexExistsException;
3535
import com.couchbase.client.java.Cluster;
3636

37+
/**
38+
* @author Michael Nitschinger
39+
* @author Michael Reiche
40+
*/
3741
public class CouchbasePersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent<?, ?>> {
3842

3943
private static final Logger LOGGER = LoggerFactory.getLogger(CouchbasePersistentEntityIndexCreator.class);
@@ -95,7 +99,8 @@ private void checkForAndCreateIndexes(final CouchbasePersistentEntity<?> entity)
9599
private void createIndex(final IndexDefinitionHolder indexToCreate) {
96100
Cluster cluster = couchbaseOperations.getCouchbaseClientFactory().getCluster();
97101

98-
StringBuilder statement = new StringBuilder("CREATE INDEX ").append(indexToCreate.getIndexName()).append(" ON `")
102+
StringBuilder statement = new StringBuilder("CREATE INDEX `")
103+
.append(indexToCreate.getIndexName()).append("` ON `")
99104
.append(couchbaseOperations.getBucketName()).append("` (")
100105
.append(String.join(",", indexToCreate.getIndexFields())).append(")");
101106

src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexResolver.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,10 @@
3232
import org.springframework.util.Assert;
3333
import org.springframework.util.StringUtils;
3434

35+
/**
36+
* @author Michael Nitschinger
37+
* @author Michael Reiche
38+
*/
3539
public class CouchbasePersistentEntityIndexResolver implements QueryIndexResolver {
3640

3741
private final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext;
@@ -103,7 +107,8 @@ protected IndexDefinitionHolder createFieldQueryIndexDefinition(final CouchbaseP
103107
String fieldName = index.name().isEmpty() ? property.getFieldName() : index.name();
104108
fields.add(fieldName + (index.direction() == QueryIndexDirection.DESCENDING ? " DESC" : ""));
105109

106-
String indexName = "idx_" + StringUtils.uncapitalize(entity.getType().getSimpleName()) + "_" + fieldName;
110+
String indexName = "idx_" + StringUtils.uncapitalize(entity.getType().getSimpleName()) + "_"
111+
+ fieldName.replace(".", "_");
107112

108113
return new IndexDefinitionHolder(fields, indexName, getPredicate(entityInfo));
109114
}
@@ -126,8 +131,8 @@ protected List<IndexDefinitionHolder> createCompositeQueryIndexDefinitions(final
126131

127132
return indexAnnotations.stream().map(ann -> {
128133
List<String> fields = Arrays.asList(ann.fields());
129-
String fieldsIndexName = String.join("_", fields).toLowerCase().replace(" ", "").replace("asc", "")
130-
.replace("desc", "");
134+
String fieldsIndexName = String.join("_", fields).toLowerCase().replace(".", "_").replace(" ", "")
135+
.replace("asc", "").replace("desc", "");
131136

132137
String indexName = "idx_" + StringUtils.uncapitalize(entity.getType().getSimpleName()) + "_" + fieldsIndexName;
133138
return new IndexDefinitionHolder(fields, indexName, predicate);

src/test/java/org/springframework/data/couchbase/domain/Airline.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
@Document
2525
@CompositeQueryIndex(fields = { "id", "name desc" })
26+
@CompositeQueryIndex(fields = { "id.something", "name desc" })
2627
public class Airline extends ComparableEntity {
2728
@Id String id;
2829

src/test/java/org/springframework/data/couchbase/domain/UserRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.util.List;
2020
import java.util.stream.Stream;
2121

22+
import com.couchbase.client.java.query.QueryScanConsistency;
2223
import org.springframework.data.couchbase.repository.CouchbaseRepository;
2324
import org.springframework.data.couchbase.repository.Query;
25+
import org.springframework.data.couchbase.repository.ScanConsistency;
2426
import org.springframework.data.repository.query.Param;
2527
import org.springframework.stereotype.Repository;
2628

@@ -37,6 +39,7 @@ public interface UserRepository extends CouchbaseRepository<User, String> {
3739

3840
List<User> findByFirstname(String firstname);
3941

42+
@ScanConsistency(query=QueryScanConsistency.REQUEST_PLUS)
4043
Stream<User> findByLastname(String lastname);
4144

4245
List<User> findByFirstnameIn(String... firstnames);

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryAutoQueryIndexIntegrationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ void createsCompositeIndex() {
6262
assertTrue(foundIndex.get().condition().get().contains("_class"));
6363
}
6464

65+
@Test
66+
void createsCompositeIndexWithPath() {
67+
Optional<QueryIndex> foundIndex = cluster.queryIndexes().getAllIndexes(bucketName()).stream()
68+
.filter(i -> i.name().equals("idx_airline_id_something_name")).findFirst();
69+
70+
assertTrue(foundIndex.isPresent());
71+
assertTrue(foundIndex.get().condition().get().contains("_class"));
72+
}
6573
@Configuration
6674
@EnableCouchbaseRepositories("org.springframework.data.couchbase")
6775
static class Config extends AbstractCouchbaseConfiguration {

0 commit comments

Comments
 (0)