Handle auto index on field path. (#1169)
Closes #1166. Co-authored-by: mikereiche <michael.reiche@couchbase.com>
This commit is contained in:
@@ -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 org.springframework.data.mapping.context.MappingContextEvent;
|
||||
import com.couchbase.client.core.error.IndexExistsException;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
* @author Michael Reiche
|
||||
*/
|
||||
public class CouchbasePersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent<?, ?>> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CouchbasePersistentEntityIndexCreator.class);
|
||||
@@ -95,7 +99,8 @@ public class CouchbasePersistentEntityIndexCreator implements ApplicationListene
|
||||
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(")");
|
||||
|
||||
|
||||
@@ -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.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Michael Nitschinger
|
||||
* @author Michael Reiche
|
||||
*/
|
||||
public class CouchbasePersistentEntityIndexResolver implements QueryIndexResolver {
|
||||
|
||||
private final MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext;
|
||||
@@ -103,7 +107,8 @@ public class CouchbasePersistentEntityIndexResolver implements QueryIndexResolve
|
||||
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 @@ public class CouchbasePersistentEntityIndexResolver implements QueryIndexResolve
|
||||
|
||||
return indexAnnotations.stream().map(ann -> {
|
||||
List<String> 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);
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.data.couchbase.core.mapping.Document;
|
||||
|
||||
@Document
|
||||
@CompositeQueryIndex(fields = { "id", "name desc" })
|
||||
@CompositeQueryIndex(fields = { "id.something", "name desc" })
|
||||
public class Airline extends ComparableEntity {
|
||||
@Id String id;
|
||||
|
||||
|
||||
@@ -62,6 +62,14 @@ public class CouchbaseRepositoryAutoQueryIndexIntegrationTests extends ClusterAw
|
||||
assertTrue(foundIndex.get().condition().get().contains("_class"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsCompositeIndexWithPath() {
|
||||
Optional<QueryIndex> 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 {
|
||||
|
||||
Reference in New Issue
Block a user