且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Spring-Data Elasticsearch 在 Elasticsearch 中动态创建索引名称

更新时间:2023-02-18 23:32:33

我在我的应用程序上所做的是我使用 ElasticSearchTemplate 创建我的动态索引名称,然后我将别名指向我创建的新索引,然后放下旧的.

What I am doing on my application is that I use ElasticSearchTemplate to create my dynamic index name and then I point an alias to the new index I have created and then drop the old one.

esTemplate.createIndex(newIndexName, loadfromFromFile(settingsFileName));
esTemplate.putMapping(newIndexName, "MYTYPE", loadfromFromFile(mappingFileName));

我没有使用我班级的映射和设置,因为我需要它是动态的.

I'm not using the mapping and settings from my class since I need it to be dynamic.

    protected String loadFromFile(String fileName) throws IllegalStateException {
       StringBuilder buffer = new StringBuilder(2048);
       try {
           InputStream is = getClass().getResourceAsStream(fileName);
           LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));
           while (reader.ready()) {
               buffer.append(reader.readLine());
               buffer.append(' ');
           }
       } catch (Exception e) {
           throw new IllegalStateException("couldn't load file " + fileName, e);
       }
       return buffer.toString();
   }