Skip to content

Handle auto index on field path. #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<MappingContextEvent<?, ?>> {

private static final Logger LOGGER = LoggerFactory.getLogger(CouchbasePersistentEntityIndexCreator.class);
Expand Down Expand Up @@ -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(")");

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext;
Expand Down Expand Up @@ -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));
}
Expand All @@ -126,8 +131,8 @@ protected List<IndexDefinitionHolder> createCompositeQueryIndexDefinitions(final

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

@Document
@CompositeQueryIndex(fields = { "id", "name desc" })
@CompositeQueryIndex(fields = { "id.something", "name desc" })
public class Airline extends ComparableEntity {
@Id String id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ void createsCompositeIndex() {
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 {
Expand Down