spring-projects / spring-data-couchbase

Provides support to increase developer productivity in Java when using Couchbase. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-couchbase
Apache License 2.0
274 stars 190 forks source link

Auto Index Creation creates indexes under _default scope #1927

Open furkantarikgocmen opened 5 months ago

furkantarikgocmen commented 5 months ago

Hi, i want to define indexes in my @Document entitys, but when ApplicationStartup, spring-data-couchbase creates indexes under myBucket._default, so i cant understand why, when i check spring data couchbase package, when index creating in CouchbasePersistentEntityIndexCreator class, createIndex function generates statement with couchbaseOperations.getBucketName(), i want to create index for collection, what is the best way for to do this ? Maybe a way for change query scope or something like that

Environment:

Java 21, spring-data-couchbase-5.2.1, couchbase capella 7.2.4 - couchbase sandbox 7.0.2 (same for both)

BaseDocument:

@Document
public class BaseDocument implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationStrategy.UNIQUE)
    protected String id;

    @CreatedDate
    protected Date documentCreationDate;
}

My Entity:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@TypeAlias("my-entity")
@Scope("logs")
@Collection("my-entity-logs")
@CompositeQueryIndexes({
        @CompositeQueryIndex(fields = {"fieldA", "updatedAt desc"}),
        @CompositeQueryIndex(fields = {"fieldB", "updatedAt asc"})
})
public class MyEntity extends BaseDocument {

    @QueryIndexed
    @Field
    private Long fieldA;

    @Field
    private String fieldB;

    @Field
    private String fieldC;

    @Field
    private String fieldD;

    @Field
    private Date updatedAt;

    @Field
    private String updatedBy;
}

Repository:

@Repository
public interface MyEntityRepository extends CouchbaseRepository<MyEntity, String>, DynamicProxyable<MyEntityRepository> {
}

Service:

@Service
@RequiredArgsConstructor
public class MyEntityService implements BaseService<MyEntity> {
    private final MyEntityRepository myEntityRepository;

    public void saveAll(List<MyEntity> myEntitys) {
        myEntityRepository.saveAll(myEntitys);
    }
}

Configuration:

@Configuration
@EnableCouchbaseRepositories
@EnableCouchbaseAuditing
@RequiredArgsConstructor
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
    private final CouchbaseProperties couchbaseProperties;

    @Override
    protected void configureEnvironment(final ClusterEnvironment.Builder builder) {
        builder.securityConfig().enableTls(couchbaseProperties.getEnableTls());
        builder.ioConfig().enableDnsSrv(couchbaseProperties.getEnableDnsSrv());
    }

    @Override
    public String getConnectionString() {
        return couchbaseProperties.getConnectionString();
    }

    @Override
    public String getUserName() {
        return couchbaseProperties.getUsername();
    }

    @Override
    public String getPassword() {
        return couchbaseProperties.getPassword();
    }

    @Override
    public String getBucketName() {
        return couchbaseProperties.getBucket();
    }

    @Override
    protected boolean autoIndexCreation() { return couchbaseProperties.getAutoIndexCreation(); }
}
mikereiche commented 5 months ago

i want to create index for collection, what is the best way for to do this ?

The best way? Do this manually outside of spring data. This @QueryIndexed annotation to start indexing on startup is problematic because it can delay startup, and also the application will complete startup before the indexing is complete giving unexpected results. Other spring data technologies that had this have removed it due to such complications.

The reason that it doesn't use the scope and collection is because the annotation was implemented before scopes and collections and not updated. I will investigate to see what is needed to update it for scopes and collections.

mikereiche commented 3 months ago

I did not get this into the May release. Sorry.