Rolling Index (Dynamic index name) In Spring Data Elasticsearch Rolling Index (Dynamic index name) In Spring Data Elasticsearch elasticsearch elasticsearch

Rolling Index (Dynamic index name) In Spring Data Elasticsearch


As far as the spring-boot-starter-data-elasticsearch-1.5, you can achieve it by spring el expression:

@BeanDepartment department() {    return new Department();}@Document(indexName="store_#{department.name()}", indexStoreType="invoice")public class InvoiceES{}

You can change the bean's property to change the index you want to save/search:

    invoiceRepo.save(new Invoice(...));    department.setName("newName");    invoiceRepo.save(new Invoice(...));

What should be noticed is not to share this bean in multiple thread, which may mess up your index.


I had to solve similar problem to store data with dynamic index name, consisting of current date: "Index-name-YYYY.MM.DD".

I made a component which has getter of formatted date string:

@Componentpublic class ElasticIndex {    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");    public String getIndexDate() {        return formatter.format(LocalDate.now());    }}

Thus, your document class will be like that:

@Document(indexName = "my_data-#{elasticIndex.getIndexDate()}", type = "DataDoc")public class DataDoc {    @Id    private String id;    private String dataToStore;    ....}

So each time you save/delete/etc new document to your elasticsearch repository, it gets index name from getIndexDate method of ElasticIndex bean.

Works on Spring Boot 2.2.5, Spring Data Elasticsearch 3.2.5


Vishal,

Currently spring data elasticsearch does not support this feature. we already have feature request(pull request) which will be added soon with next release.

Have a look on this pull request,https://github.com/spring-projects/spring-data-elasticsearch/pull/56