ES: spring boot中使用RestHighLevelClient
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200") // 替换为你的Elasticsearch节点地址
.build();
return RestClients.create(clientConfiguration).rest();
}
}
这段代码定义了一个配置类ElasticsearchConfig
,它扩展了AbstractElasticsearchConfiguration
并重写了elasticsearchClient
方法,在该方法中创建了一个RestHighLevelClient
Bean,用于与Elasticsearch集群进行通信。你需要替换"localhost:9200"
为你的Elasticsearch节点地址。这样,在Spring Boot应用中就可以通过依赖注入来使用RestHighLevelClient
了。
评论已关闭