介绍
Java Low Level REST Client: the official low-level client for Elasticsearch. It allows to communicate with an Elasticsearch cluster through http. Leaves requests marshalling and responses un-marshalling to users. It is compatible with all Elasticsearch versions.
Java Low Level REST Client:官方提供的低级客户端。该客户端通过http来连接Elasticsearch集群。用户在使用该客户端时需要将请求数据手动拼接成Elasticsearch所需JSON格式进行发送,收到响应时同样也需要将返回的JSON数据手动封装成对象。虽然麻烦,但是客户端兼容所有的Elasticsearch版本。低级客户端包含如下特性:
1.极少的依赖包
2.负载均衡访问所有可用节点
3.故障转移。当发生节点故障时会返回特定的状态码
4.连接失败处罚策略。(客户端是否会去重连一个连接失败的节点取决于该节点连续连接失败的次数,连接该节点失败次数越多,客户端下次重连它的时间就越长)
5.持久连接
6.跟踪记录请求和响应日志
7.可选的集群节点自动发现
Java High Level REST Client: the official high-level client for Elasticsearch. Based on the low-level client, it exposes API specific methods and takes care of requests marshalling and responses un-marshalling.
Java High Level REST Client:官方提供的高级客户端。该客户端基于低级客户端实现,它提供了很多便捷的API来解决低级客户端需要手动转换数据格式的问题(也就是说高级客户端,无需关系请求和响应数据转换的问题)
rest-low-level-client 方式
maven依赖
!-- Elasticsearch Dependencies -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.4</version>
</dependency>
操作起来比较繁琐(要结合es基础api),这里只讲了一个简单的例子,有兴趣的自己在深入研究…
在可视化工具中编写查询语句,验证没问题放入代码中:
在head插件中:
在kibana中
代码如下:
代码参考:https://github.com/472732787/com-spring-multiple-datasource/tree/master/spring-boot-es-RestLowLevelClient
语法参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl.html
rest-high-level-client 方式
maven依赖
<!-- Elasticsearch Dependencies -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.5.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.5.4</version>
</dependency>
配置文件:
spring:
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
repositories:
enabled: true
cluster-name: my-application
配置类:
package spring.boot.es.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @author liuli
*/
@Configuration
public class ElasticSearchConfiguration extends AbstractFactoryBean {
private static final Logger LOG = LoggerFactory.getLogger(ElasticSearchConfiguration.class);
@Value("${spring.data.elasticsearch.cluster-nodes}")
private String clusterNodes ;
@Value("${spring.data.elasticsearch.cluster-name}")
private String clusterName;
private RestHighLevelClient restHighLevelClient;
@Override
public void destroy() throws Exception {
// 关闭Client
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
}
@Override
public Class<RestHighLevelClient> getObjectType() {
return RestHighLevelClient.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
protected Object createInstance() throws Exception {
try {
List<HttpHost> list = new ArrayList<HttpHost>();
if (!"".equals(clusterNodes)){
for (String nodes:clusterNodes.split(",")) {
String InetSocket [] = nodes.split(":");
String host = InetSocket[0];
Integer port = Integer.valueOf(InetSocket[1]);
list.add(new HttpHost(host, port, "http"));
}
}
// 如果有多个节点,构建多个HttpHost
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(list.get(0),list.get(1),list.get(2))
);
} catch (Exception e) {
LOG.error(e.getMessage());
}
return restHighLevelClient;
}
}
源代码参考:
https://github.com/472732787/com-spring-multiple-datasource/tree/master/spring-boot-es-RestHighLevelClient
参考资料:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.5/index.html
https://www.elastic.co/guide/en/elasticsearch/reference/6.5/search.html