如何使用spring data elasticsearch创建具有指定名称的映射索引?

Posted

技术标签:

【中文标题】如何使用spring data elasticsearch创建具有指定名称的映射索引?【英文标题】:How to create mapped index with specified name using spring data elasticsearch? 【发布时间】:2021-11-03 14:50:08 【问题描述】:

我正在尝试使用 indexOperations 创建索引。我有以下代码:

val indexOperations = template.indexOps(SmartStoreNew::class.java)
indexOperations.create()

其中 SmarStoreNew 是 @Document 对象。 所以我的问题是如何使用这种构造指定索引名称?

【问题讨论】:

【参考方案1】:

索引名称取自SmartStoreNew 类的@Document 注解。如果您想指定与此注释中的索引名称不同的索引名称,您可以这样做

val indexOperations = template.indexOps(IndexCoordinates.of("index-name"))
indexOperations.create()

编辑 07.09.2021

索引名称“smart_store_new_2021-09-07T22:10:58.541117”。无效,请检查 (https://www.elastic.co/guide/en/elasticsearch/reference/7.10/indices-create-index.html#indices-create-api-path-params) 索引名称中允许使用哪些字符。

除此之外,您可以在 @Document 注释中使用 SpEL 表达式,请查看文档 (https://docs.spring.io/spring-data/elasticsearch/docs/4.2.4/reference/html/#elasticsearch.mapping.meta-model.annotations) 或阅读我的博客文章 (https://www.sothawo.com/2020/07/how-to-provide-a-dynamic-index-name-in-spring-data-elasticsearch-using-spel/)

【讨论】:

感谢您的回复。我的想法是根据当前时间创建索引名称,例如:index.name == "smart_store_new_2021-09-07T22:10:58.541117"。所以我想知道如何在创建之前或之后使用时间戳指定索引名称。

以上是关于如何使用spring data elasticsearch创建具有指定名称的映射索引?的主要内容,如果未能解决你的问题,请参考以下文章

使用 spring-data-jpa 获取这些数据如何更正确?

如何在 Spring Boot 应用程序的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库?

如何在 spring-boot 中禁用 spring-data-mongodb 自动配置

如何使用 spring4+spring-data-jpa(hibernateJpaVendorAdapter)+multidatasource+one entityManager+jpaReposit

Spring-data-redis + Lettuce 如何使用 Pipeline

如何使用 Spring Boot 和 Spring Data 访问实体管理器