elasticsearch - Spring Data Elasticsearch 中的滚动索引(动态索引名称)

标签 elasticsearch spring-data

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

@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/

相关文章:

elasticsearch - 如何在Elasticsearch中将multi_match与索引一起使用?

python - 在删除记录时更新干草堆索引,而无需重新索引整个数据库

elasticsearch - Elasticsearch 部分快照如何工作

elasticsearch - Elasticsearch 多个 simple_query_string with boost

对 Oracle 的 spring-data-jdbc 支持

elasticsearch - 需要帮助以在字段上正确执行通配符搜索

java - 升级 spring 依赖项后出现错误 A JTA EntityManager cannot use getTransaction()

java - 实体需要什么状态才能触发【更新】操作?

java - 使用 @ManyToMany 的 Spring Data JPA 可分页

java - 带有 Java 8 Streams 的 Spring Data JPA 存储库