elasticsearchjava客户端
elasticsearch java客户端
1.引用maven配置
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
2.定义elasticsearch的配置文件到springboot的application.properties
#dev
#config.es.address=127.0.0.1:9200
3.将application.properties配置文件中的配置抽取到配置文件解析类ElasticSearchProperty中
ElasticSearchProperty.java
@Data
@Configuration
@PropertySource(value = "classpath:config/application.properties")
@ConfigurationProperties(prefix = "config.es")
public class ElasticSearchProperty {
/**
* 连接地址,格式:IP:端口
* 多个逗号分隔
* 示例:127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
*/
private String address="";
/**
* 用户名
*/
private String userName;
/**
* 密码
*/
private String password;
/**
* 连接超时时间
* 默认10s
*/
private int connectTimeout = 10000;
/**
* socket超时时间
* 默认10s
*/
private int socketTimeout = 60000;
/**
* 请求连接超时时间
* 默认10s
*/
private int connectionRequestTimeout = 10000;
/**
* 最大分页size
* 默认10000
*/
private int maxPageSize;
}
4.elasticsearch配置类中,加载es客户端
import net.bytebuddy.implementation.bytecode.Throw;
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.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.logging.log4j.util.Strings;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* ElasticSearch Rest client 配置
*/
@Configuration
public class ElasticSearchConfig {
private static final Logger log = LoggerFactory.getLogger(ElasticSearchConfig.class);
@Resource
private ElasticSearchProperty elasticSearchProperty;
public ElasticSearchProperty getElasticSearchProperty() {
return elasticSearchProperty;
}
public void setElasticSearchProperty(ElasticSearchProperty elasticSearchProperty) {
this.elasticSearchProperty = elasticSearchProperty;
}
// 定义restClient生成的相关builder
@Bean
public RestClientBuilder restClientBuilder() {
Assert.notNull(elasticSearchProperty, "elasticSearchProperty cannot null ");
Assert.notNull(elasticSearchProperty.getAddress(), "address hosts cannot null ");
//ElasticSearch 连接地址地址
HttpHost[] httpHosts = this.getElasticSearchHttpHosts();
return RestClient.builder(httpHosts).setRequestConfigCallback(requestConfigBuilder -> {
//设置连接超时时间
requestConfigBuilder.setConnectTimeout(elasticSearchProperty.getConnectTimeout());
requestConfigBuilder.setSocketTimeout(elasticSearchProperty.getSocketTimeout());
requestConfigBuilder.setConnectionRequestTimeout(elasticSearchProperty.getConnectionRequestTimeout());
return requestConfigBuilder;
}).setHttpClientConfigCallback(httpClientBuilder -> {
//Commented by Allen 启用Auth的话,此属性会导致认证不通过
//httpClientBuilder.disableAuthCaching();
//设置账密
return getHttpAsyncClientBuilder(httpClientBuilder);
});
}
/**
* ElasticSearch Rest client 配置,单例client并引用上面定义的clientBuilder
*
* @return RestHighLevelClient
*/
@Bean
public RestHighLevelClient restHighLevelClient(@Qualifier("restClientBuilder") RestClientBuilder restClientBuilder) {
return new RestHighLevelClient(restClientBuilder);
}
/**
* ElasticSearch 连接地址
* 多个逗号分隔,es都连接都连接
* 示例:127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
*/
private HttpHost[] getElasticSearchHttpHosts() {
String[] hosts = elasticSearchProperty.getAddress().split(",");
HttpHost[] httpHosts = new HttpHost[hosts.length];
for (int i = 0; i < httpHosts.length; i++) {
String host = hosts[i];
host = host.replaceAll("http://", "").replaceAll("https://", "");
Assert.isTrue(host.contains(":"), String.format("your host:[%s] format error , Please refer to [ 127.0.0.1:9200 ] ,", host));
httpHosts[i] = new HttpHost(host.split(":")[0], Integer.parseInt(host.split(":")[1]), "http");
}
return httpHosts;
}
private HttpAsyncClientBuilder getHttpAsyncClientBuilder(HttpAsyncClientBuilder httpClientBuilder) {
if (Strings.isBlank(elasticSearchProperty.getUserName()) || Strings.isBlank(elasticSearchProperty.getPassword())) {
return httpClientBuilder;
}
//账密设置
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//es账号密码(一般使用,用户elastic)
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticSearchProperty.getUserName(), elasticSearchProperty.getPassword()));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpClientBuilder;
}
}
5.使用highLevelApi的client(ElasticSearchRestApiClient 引用了RestHighLevelClient)
ElasticSearchRestApiClient
@Slf4j
@Component
public class ElasticSearchRestApiClient {
private static final String SEARCH_BY_AGG_UMPKEY = "delta.dao.es.searchByAgg";
public static final String SCROLL_UMPKEY = "delta.dao.es.scroll";
@Resource
private ElasticSearchProperty elasticSearchProperty;
@Autowired
private RestHighLevelClient restHighLevelClient;
public RestHighLevelClient getRestHighLevelClient() {
return restHighLevelClient;
}
public AsyncSearchClient getAsyncClient() {
return restHighLevelClient.asyncSearch();
}
public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}
/**
* 默认主分片数
*/
private static final int DEFAULT_SHARDS = 5;
/**
* 默认副本分片数
*/
private static final int DEFAULT_REPLICAS = 1;
/**
* 判断索引是否存在
*
* @param index 索引
* @return 返回 true,表示存在
*/
public boolean existsIndex(String index) throws ElasticSearchRuntimeException {
try {
GetIndexRequest request = new GetIndexRequest(index);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.error("[ elasticsearch ] >> get index exists exception ,index:{} ", index, e);
throw new ElasticSearchRuntimeException("[ elasticsearch ] >> get index exists exception:" + e.getMessage(), e);
}
}
}
原创:做时间的朋友