查找了很多方法都是通过Spring EL表达式实现 @Document(IndexName="#{demo.getIndexName}")
这种方式的问题在于没法解决数据库里生成的序号,例如我希望通过公司ID生成索引编号。后来在外网上找到一个大佬提出的解决方案,这位大佬把两种方案都实现了一遍。
通过entityId自定义index
Use an index name defined by the entity to store data in Spring Data Elasticsearch 4.0
通过SpEL动态生成index
How to provide a dynamic index name in Spring Data Elasticsearch using SpEL
我这里按照entity的方式实现了需求
elasticsearch 版本7.17.3
springboot版本2.7.1
Bean
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Document(indexName = ElasticSearchIndices.ES_INDEX_TROUBLES_COMPANY+"_*",createIndex = fal

本文介绍了如何在Spring Boot 2.7.1应用中,利用Elasticsearch 7.17.3动态创建索引。通过实体ID自定义索引名称,避免了使用Spring Expression Language (SpEL)方式无法处理数据库生成序号的问题。文中详细展示了配置Bean、接口定义、实现类及调用方法。
最低0.47元/天 解锁文章
462

被折叠的 条评论
为什么被折叠?



