CouchBase Tries to Create Indexes Every Time When Application Rerun

223 views Asked by At

I use Java SDK 3.0.3 as a CouchBase client. Index configuration is handled using following @Configuration

@Bean
public void createIndexes() {
QueryIndexManager indexManager = couchbaseCluster.queryIndexes();

    indexManager.createPrimaryIndex(couchbaseProperties.getBucketName(), 
            CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions());
    indexManager.createIndex(couchbaseProperties.getBucketName(), 
            "user_idx", Collections.singletonList("userId"), 
            CreateQueryIndexOptions.createQueryIndexOptions());

}

Every time when I rerun the application, as you may notice that it tries to create existing indexes again and throws IndexAlreadyExist Exception.

Simply, I used if statement to check whether index are created or not but it doesn't seem like the best practise of it.

Covered bunch of docs which are emphasizing IF NOT EXISTS keyword but I can't find correct syntax or CouchBase doesn't support this feature.

What would be the best approach to create indexes by avoiding the problem described? Thanks for helping me out.

1

There are 1 answers

0
dnault On BEST ANSWER

QueryIndexOptions and PrimaryQueryIndexOptions both have an ignoreIfExists property you can enable to get the behavior you want.

CreateQueryIndexOptions.createQueryIndexOptions()
  .ignoreIfExists(true)