Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling Index (Dynamic index name) In Spring Data Elasticsearch

I have a usecase wherein I want to index my document in a separate index based on certain condition. For example, I want to store invoice document to an index with department name suffixed with it.

@Document(indexName="store_{department}", indexStoreType="invoice")
public class InvoiceES{

    // fields
    @Id
    private String id;
    @Field
    private String department;
}

Is it possible to achieve this using Spring Data?

If not, is it planned in coming releases of Spring Data?

like image 226
Vishal Shukla Avatar asked Jun 20 '14 18:06

Vishal Shukla


2 Answers

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

@Bean
Department 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.

like image 128
Tony Avatar answered Oct 01 '22 22:10

Tony


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:

@Component
public 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

like image 26
Pavel Krutikhin Avatar answered Oct 03 '22 22:10

Pavel Krutikhin