Imagine, you have a MongoDB collection that maps to a simple entity in Spring Boot app:
@Document
data class Question(
@Id val id: String,
@TextIndexed
val topic: String
)
Now you want to add a new field and include it in the full-text search. You make a simple modification:
@Document
data class Question(
@Id val id: String,
@TextIndexed
val topic: String,
@TextIndexed
val description: String
)
To verify this change, you start the MongoDB database, start your app, fingers crossed, and BOOM!!! you get the following exception on init:
Caused by: org.springframework.dao.DataIntegrityViolationException:
Cannot create index for '' in collection 'question' with keys 'Document{{topic=text, description=text}}' and options 'Document{{name=Question_TextIndex}}'. Index already defined as 'IndexInfo [indexFields=[IndexField [ key: topic, direction: null, type: TEXT, weight: 1.0]], name=Question_TextIndex, unique=false, sparse=false, language=english, partialFilterExpression=null, collation=null]'.;
nested exception is com.mongodb.MongoCommandException:
Command failed with error 85: 'Index with name: Question_TextIndex already exists with different options' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Index with name: Question_TextIndex already exists with different options", "code" : 85, "codeName" : "IndexOptionsConflict" }
...
It looks like Spring Data automatically creates a new text index that conflicts with the existing one by name. How to "tell" Spring Data that I need to modify (replace / alter) the existing index? Or maybe somehow create an index with a new name? What is a good way to resolve such conflicts in Spring Data?