Java High Level REST Client 操作elasticsearch
1、添加maven依賴
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.0</version>
</dependency>
2合砂、初始化elasticsearch
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
* @Author tengpt
* @Date 2019/11/28 16:01
*/
@Slf4j
@Configuration
public class ElasticConfig {
String host = "172.17.0.1";
int port = 9200;
String clusterName = "elastic";
String password = "123456";
@Autowired
private SettingService settingService;
public ElasticConfig() {
}
@Bean(name = "elasticSearchClient")
public RestClientBuilder getRestClientBuilder() {
try {
List<Setting> hostList = settingService.findAllByName("ES_HOST");
List<Setting> portList = settingService.findAllByName("ES_PORT");
List<Setting> nameList = settingService.findAllByName("ES_USER");
List<Setting> pwdList = settingService.findAllByName("ES_PASSWORD");
if (!hostList.isEmpty()) {
host = hostList.get(0).getValue();
}
if (!portList.isEmpty()) {
port = Integer.valueOf(portList.get(0).getValue());
}
if (!nameList.isEmpty()) {
clusterName = nameList.get(0).getValue();
}
if (!pwdList.isEmpty()) {
password = pwdList.get(0).getValue();
}
} catch (Exception e) {
log.warn("es初始化配置信息獲取失斍嗳印:{}", e.getStackTrace());
}
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(clusterName, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
return builder;
}
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(getRestClientBuilder());
}
}
建議遇到問(wèn)題先翻閱官網(wǎng)。