spring boot动态获取@Document中的indexName

spring boot动态获取@Document中的indexName

在做spring boot 整合kafka+es做日志存储时发生

本身是这样的

@Document(indexName = "xxx", type = "xxx", shards = 1,replicas = 0, refreshInterval = "-1")

但是这样是固定值,不可动态更改
后来改成这样

@Document(indexName = "#{@indexName}", type = "xxx", shards = 1,replicas = 0, refreshInterval = "-1")

当然不是只这样就行
要加这个

@Component
public class ConfigBean {
    @Value("${configBean.indexName}")
    private String indexName;
    @Bean
    public String indexName(){
        return indexName;
    }
}

解决办法取自动态的为ElasticSearch的@Document指定index
加了以后一直报错
Could not resolve placeholder 'configBean.indexNam

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,您可以使用Spring Boot集成Elasticsearch来实现文档搜索。首先,您需要在pom.xml文件添加Elasticsearch和Spring Data Elasticsearch的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.5.2</version> </dependency> ``` 然后,您需要配置Elasticsearch客户端: ``` @Configuration public class ElasticsearchConfig { @Value("${elasticsearch.host}") private String host; @Value("${elasticsearch.port}") private int port; @Bean public RestHighLevelClient elasticsearchClient() { return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port))); } } ``` 接下来,您需要定义一个Elasticsearch文档实体类,并使用`@Document`注解将其与Elasticsearch的索引关联起来: ``` @Document(indexName = "my_index", type = "my_type") public class MyDocument { @Id private String id; private String title; private String content; // getters and setters } ``` 然后,您可以使用`ElasticsearchRepository`接口提供的方法来实现搜索: ``` public interface MyDocumentRepository extends ElasticsearchRepository<MyDocument, String> { List<MyDocument> findByTitleOrContent(String title, String content); } ``` 最后,您可以在Spring Boot应用程序使用`MyDocumentRepository`来搜索文档: ``` @Autowired private MyDocumentRepository repository; public void searchDocuments(String query) { List<MyDocument> documents = repository.findByTitleOrContent(query, query); // do something with the documents } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值