我有一个用例,其中我想根据特定条件在单独的索引中索引我的文档。例如,我想将发票文档存储到以部门名称为后缀的索引中。
@Document(indexName="store_{department}", indexStoreType="invoice")
public class InvoiceES{
// fields
@Id
private String id;
@Field
private String department;
}
是否可以使用 Spring Data 实现此目的?
如果没有,是否计划在即将发布的 Spring Data 中发布?
最佳答案
就spring-boot-starter-data-elasticsearch-1.5而言,可以通过spring el表达式来实现:
@Bean
Department department() {
return new Department();
}
@Document(indexName="store_#{department.name()}", indexStoreType="invoice")
public class InvoiceES{}
您可以更改 bean 的属性来更改您要保存/搜索的索引:
invoiceRepo.save(new Invoice(...));
department.setName("newName");
invoiceRepo.save(new Invoice(...));
应该注意的是不要在多线程中共享这个bean,这可能会弄乱你的索引。
关于elasticsearch - Spring Data Elasticsearch 中的滚动索引(动态索引名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24333327/