滚动索引(动态索引名称)在Spring Data Elasticsearch中 [英] Rolling Index (Dynamic index name) In Spring Data Elasticsearch

查看:2248
本文介绍了滚动索引(动态索引名称)在Spring Data Elasticsearch中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

// fields
@Id
private String id;
@Field
私人字符串部门;
}

可以使用Spring Data来实现吗?



如果没有,是否在Spring数据的未来版本中计划?

解决方案

spring-boot-starter-data-elasticsearch-1.5,你可以通过spring el表达式来实现:

  @Bean 
部门部门(){
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,这可能会弄乱你的索引。


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.

这篇关于滚动索引(动态索引名称)在Spring Data Elasticsearch中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆