想象一下,你有一个 MongoDB 集合,它映射到 Spring Boot 应用程序中的一个简单实体:
@Document
data class Question(
@Id val id: String,
@TextIndexed
val topic: String
)
现在您要添加一个新字段并将其包含在全文搜索中。你做一个简单的修改:
@Document
data class Question(
@Id val id: String,
@TextIndexed
val topic: String,
@TextIndexed
val description: String
)
要验证此更改,您启动 MongoDB 数据库,启动您的应用程序,手指交叉,然后 BOOM !!!您在 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" }
...
看起来 Spring Data 会自动创建一个新的文本索引,该索引与现有的名称冲突。如何“告诉”我需要修改(替换/更改)现有索引的 Spring Data?或者也许以某种方式创建一个新名称的索引?在 Spring Data 中解决此类冲突的好方法是什么?