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

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

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

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

【问题讨论】:

    标签: kotlin elasticsearch indexing spring-data-elasticsearch


    【解决方案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"。所以我想知道如何在创建之前或之后使用时间戳指定索引名称。
    猜你喜欢
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2014-04-15
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多