【问题标题】:How to give Index Alias in Domain class instead of index name in Spring-dat-elasticsearch如何在域类中给出索引别名而不是 Spring-data-elasticsearch 中的索引名称
【发布时间】:2018-08-02 08:25:53
【问题描述】:

我当前的项目需要有一个弹性搜索索引,该索引将每月滚动。索引名称将类似于Indexname-%{+YYYY.MM}

现在的问题是我正在使用 Spring data elasticsearch 与弹性搜索进行通信并执行 crud 操作。目前在我的域类中我已经注释如下,

@Document(indexName = "indexname-2018.08", type = "Node")
public class Node {
......}

目前我正在给出静态索引名称。 我的问题是如何使用索引的别名而不是直接索引名称,例如下面的内容,

@Document(indexName = "indexname-current”, type = "Node")

其中 indexname-current 是我的所有Indexname-%{+YYYY.MM} 类型索引的别名。

我真的被困在这里了。任何帮助将不胜感激。

【问题讨论】:

    标签: spring-data-elasticsearch


    【解决方案1】:

    Spring数据支持在索引名中使用Spring表达式SPEL

    您可以创建一个包含翻转策略的 bean。

    @Component("rollover")
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class RolloverStrategy {
    
        private String monthlyDateFormat;
    
        @PostConstruct
        void after(){
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            monthlyDateFormat = formatter.format(date);
        }
    
        public String getMonthlyDateFormat() {
            return monthlyDateFormat;
        }
    
        public void setMonthlyDateFormat(String monthlyDateFormat) {
            this.monthlyDateFormat = monthlyDateFormat;
        }
    }
    

    然后在你的类中使用 SPEL 引用它

    @Document(indexName = "indexname-#{rollover.monthlyDateFormat}", type = "Node")
    public class Node {}
    

    【讨论】:

      【解决方案2】:

      使用 SPEL 不会使用 IMO 会更好的 rollover API。您几乎没有其他选择:

      1. 将翻转 API 与 Java 客户端/spring-data-es 一起使用(只需创建一个创建翻转请求的日常例程 - 阅读更多:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-rollover-index.html

      2. 创建一个别名 - 与您在示例中所做的一样,让翻转 API 调用由脚本进行,例如使用 .json 向 server:9200/index/_rollover 发出 PUT 请求:

        {
        "conditions": {
                "max_age": "1d",
                "max_docs" : 1000000
            }
        }
        

      每天使用 cron 作业或通过应用程序 (https://www.elastic.co/blog/managing-time-based-indices-efficiently) 运行。

      使用时

      @Document(indexName=yourAlias==Indexname, type=yourType)
      

      spring-data-es 将使用您创建的别名将新文档保存到现有活动索引中,这意味着任何新文档都将保存到活动索引中,这将是您之前的翻转 API 调用创建的最新文档.

      【讨论】:

        【解决方案3】:

        我使用 Elasticsearch 7 中提供的 Index Lifecycle Management (ILM) 解决了这个问题。您可以让 Elastic 为您自动执行翻转,只需引用 Spring Data Elastic 代码的别名。我用以下方式注释了我的实体:

        @Document(indexName = "myAlias", createIndex = false)
        

        然后我添加了一些代码来在启动时调用 ILM API 以确保一切都已设置和创建:

        @Configuration
        public class ElasticIndexConfiguration {
        
            private RestHighLevelClient elasticsearchClient;
        
            public ElasticIndexConfiguration(RestHighLevelClient elasticsearchClient) {
                this.elasticsearchClient = elasticsearchClient;
            }
        
            @PostConstruct
            public void postConstruct() {
                // See ILM tutorial to:
                //   Create/update a lifecycle policy
                //   Create/update an index template that applies the lifecycle policy
                //   Bootstrap the initial time-series index / create the alias
            }
        }
        

        我参考了 ILM 教程和高级 REST 客户端的 Java API:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-04
          • 2016-04-09
          • 1970-01-01
          相关资源
          最近更新 更多