要連接ES集群,你可以按照以下步驟進行配置:
pom.xml
文件中添加ES的依賴:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
application.properties
文件中配置ES集群的連接信息,例如:spring.data.elasticsearch.cluster-nodes=host1:port1,host2:port2,host3:port3
ElasticsearchConfig.java
,用于配置ES的連接信息和其他相關配置: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("host1:port1", "host2:port2", "host3:port3")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
ElasticsearchTemplate
或ElasticsearchRepository
進行ES操作。現在你的Spring Boot應用程序就可以連接到ES集群了。你可以使用ElasticsearchTemplate
或ElasticsearchRepository
來進行索引、搜索和其他ES操作。