【问题标题】:Spring Data MongoDB: @TextIndexed Index Already Exists With Different OptionsSpring Data MongoDB:@TextIndexed 索引已经存在不同的选项
【发布时间】:2020-06-01 13:15:51
【问题描述】:

想象一下,你有一个 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 !!!你在初始化时得到以下异常:

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 中解决此类冲突的好方法是什么?

【问题讨论】:

标签: mongodb kotlin spring-data-mongodb full-text-indexing name-collision


【解决方案1】:

要在 spring-data mongodb 中跳过索引创建,您可以将自动索引创建标志设置为 false,

您可以尝试将以下属性定义放在您的属性文件下:

  spring.data.mongodb.auto-index-creation: false

【讨论】:

  • 如果我跳过索引创建,我将有一个使用过时索引的数据库。这对我来说没有意义,因为那时我无法测试索引中的变化。我正在寻找更新(替换/更改)现有索引的方法。
  • 我明白你的意思,但恐怕在 spring-data 中没有直接的重新创建/更改方式。我会在测试环境中遵循弹簧分析并应用这些步骤:确保索引已创建,然后将其删除然后重新创建它(docs.spring.io/spring-data/mongodb/docs/current/api/org/…
猜你喜欢
  • 2019-01-11
  • 2017-04-30
  • 2015-07-25
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2021-06-11
相关资源
最近更新 更多