启动时调用IndexOperations.createWithMapping时出现SpringElasticSearch异常?

weylhg0b  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(278)

我有下面的代码,它是在启动时创建的:

@EventListener(ApplicationReadyEvent.class)
  public void createIndexMappingsOnStartup() {
    final IndexOperations indexOperations = this.elasticsearchOperations.indexOps(Person.class);
    indexOperations.createWithMapping();
}

我收到以下异常:

Elasticsearch exception [type=resource_already_exists_exception, reason=index [person/71QDnf0oSqW9q32hDRm8DQQ] already exists]",

但是如果我使用下面的代码,我不会收到任何错误:

final IndexOperations indexOperations = this.elasticsearchOperations.indexOps(Person.class);
indexOperations.putMapping();

有人能告诉我这是怎么回事吗?我需要在启动时创建字段Map。

j5fpnvbx

j5fpnvbx1#

这个错误清楚地说明你想创建一个已经存在的索引,你不能在Elasticsearch中多次创建一个索引。
请注意,当您重新启动应用程序时,无法将Map再次写入索引,也无法更新Map。

相关问题