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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With