滚动索引(动态索引名称)在Spring Data Elasticsearch中

Vis*_*kla 14 elasticsearch spring-data

我有一个用例,其中我想根据特定条件在单独的索引中索引我的文档.例如,我想将发票文档存储到部门名称后缀的索引.

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

    // fields
    @Id
    private String id;
    @Field
    private String department;
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用Spring Data实现这一目标?

如果没有,是否计划在即将发布的Spring Data版本中?

Ton*_*ony 5

至于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{}
Run Code Online (Sandbox Code Playgroud)

您可以更改 bean 的属性以更改要保存/搜索的索引:

    invoiceRepo.save(new Invoice(...));
    department.setName("newName");
    invoiceRepo.save(new Invoice(...));
Run Code Online (Sandbox Code Playgroud)

需要注意的是不要在多个线程中共享这个bean,这可能会弄乱你的索引。

  • 这个方法仍然是最新的吗?我听说模板方法在 repo 初始化期间只工作一次。真的吗?你会推荐什么来实现线程安全? (3认同)

Moh*_*sen 0

维沙尔,

目前spring data elasticsearch不支持该功能。我们已经有了功能请求(拉取请求),该功能请求将很快在下一个版本中添加。

看看这个拉取请求, https://github.com/spring-projects/spring-data-elasticsearch/pull/56