1、版本依赖

注意对 transport client不了解先阅读官方文档:

transport client(传送门)

这里需要版本匹配,如失败查看官网或百度。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jiatp</groupId>
<artifactId>springboot-03-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-03-rest</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.2</version>
</dependency>
<!-- Java Low Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.3.2</version>
</dependency>
<!-- Java High Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</version>
</dependency>
<!-- json转换 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.配置客户端

ElasticsearchConfig.java
package com.jiatp.springboot.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.io.IOException; @Configuration
public class ElasticsearchConfig { @Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Value("${elasticsearch.schema}")
private String schema;
@Value("${elasticsearch.connectTimeOut}")
private int connectTimeOut;
@Value("${elasticsearch.socketTimeOut}")
private int socketTimeOut;
@Value("${elasticsearch.connectionRequestTimeOut}")
private int connectionRequestTimeOut;
@Value("${elasticsearch.maxConnectNum}")
private int maxConnectNum;
@Value("${elasticsearch.maxConnectPerRoute}")
private int maxConnectPerRoute;
private HttpHost httpHost;
private boolean uniqueConnectTimeConfig = true;
private boolean uniqueConnectNumConfig = true;
private RestClientBuilder builder;
private RestHighLevelClient client; /**
* 返回一个RestHighLevelClient
*
* @return
*/
@Bean(autowire = Autowire.BY_NAME, name = "restHighLevelClient")
public RestHighLevelClient client() {
httpHost= new HttpHost(host, port, schema);
builder = RestClient.builder(httpHost);
if (uniqueConnectTimeConfig) {
setConnectTimeOutConfig();
}
if (uniqueConnectNumConfig) {
setMutiConnectConfig();
}
client = new RestHighLevelClient(builder);
return client;
} /**
* 异步httpclient的连接延时配置
*/
public void setConnectTimeOutConfig() {
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
} /**
* 异步httpclient的连接数配置
*/
public void setMutiConnectConfig() {
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
return httpClientBuilder;
}
});
} /**
* 关闭连接
*/
public void close() {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

application.yml

elasticsearch:
host: 192.168.x.x
port: 9200
schema: http
connectTimeOut: 1000
socketTimeOut: 30000
connectionRequestTimeOut: 500
maxConnectNum: 100
maxConnectPerRoute: 100

3、测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03RestApplicationTests { @Qualifier(value = "restHighLevelClient")
@Autowired
RestHighLevelClient restHighLevelClient; String indexName="student";
String esType="msg"; @Test
public void contextLoads() throws IOException{
RestClient restClient = RestClient.builder(
new HttpHost("192.168.56.101", 9200, "http")).build();
//(1) 执行一个基本的方法,验证es集群是否搭建成功
Response response = restClient.performRequest("GET", "/", Collections.singletonMap("pretty", "true"));
System.out.println(EntityUtils.toString(response.getEntity())); }

当现实create时则表明没问题。

其它测试:


@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03RestApplicationTests { @Qualifier(value = "restHighLevelClient")
@Autowired
RestHighLevelClient restHighLevelClient; String indexName="student";
String esType="msg"; @Test
public void contextLoads() throws IOException{
RestClient restClient = RestClient.builder(
new HttpHost("192.168.56.101", 9200, "http")).build();
//(1) 执行一个基本的方法,验证es集群是否搭建成功
Response response = restClient.performRequest("GET", "/", Collections.singletonMap("pretty", "true"));
System.out.println(EntityUtils.toString(response.getEntity())); } //创建索引
@Test
public void createIndex(){ //index名必须全小写,否则报错
String index ="book";
CreateIndexRequest request = new CreateIndexRequest(index);
try {
CreateIndexResponse indexResponse = restHighLevelClient.indices().create(request);
if (indexResponse.isAcknowledged()) {
System.out.println("创建索引成功"); } else {
System.out.println("创建索引失败");
}
} catch (IOException e) {
e.printStackTrace();
} } //检查索引
@Test
public void findIndex()throws Exception{ try {
Response response = restHighLevelClient.getLowLevelClient().performRequest("HEAD", "book");
boolean exist = response.getStatusLine().getReasonPhrase().equals("OK");
System.out.println(exist);
} catch (IOException e) {
e.printStackTrace();
} }
//插入数据
@Test
public void addData(){ JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 3);
jsonObject.put("age", 26);
jsonObject.put("name", "wangwu");
jsonObject.put("date", new Date());
IndexRequest indexRequest = new IndexRequest(indexName, esType, "2").source(jsonObject); try {
IndexResponse indexResponse = restHighLevelClient.index(indexRequest);
System.out.println(indexResponse.getId());
} catch (Exception e) {
e.printStackTrace();
} }
/*
* 使用XContentBuilder添加数据
* */
@Test
public void addData1() throws Exception{ XContentBuilder builder = jsonBuilder();
builder.startObject();
{
builder.field("user", "jiatp");
builder.timeField("postDate", new Date());
builder.field("message", "trying out Elasticsearch");
}
builder.endObject();
IndexRequest indexRequest = new IndexRequest("twitter", "t_doc", "3")
.source(builder).routing("my_route");//可以添加指定路由
IndexResponse response = restHighLevelClient.index(indexRequest);
System.out.println(response.status().name()); }
/*
* 使用Object key-pairs对象键
* */
@Test
public void addData2() throws Exception{ IndexRequest indexRequest = new IndexRequest("twitter", "t_doc", "3")
.source("user", "kimchy",
"postDate", new Date(),
"message", "trying out Elasticsearch");
IndexResponse response = restHighLevelClient.index(indexRequest);
System.out.println(response.status().name()); }
//异步方式
@Test
public void testAddAsync() throws InterruptedException {
ActionListener listener = new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
System.out.println("Async:" + indexResponse.status().name());
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
// Todo
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
// Todo
}
// 处理成功分片小于总分片的情况
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
// Todo
}
} @Override
public void onFailure(Exception e) {
System.out.println("AsyncFailure:" + e.getMessage());
e.printStackTrace();
}
}; IndexRequest indexRequest = new IndexRequest("twitter", "t_doc", "4")
.source("user", "luxi",
"postDate", new Date(),
"message", "trying out Elasticsearch"); restHighLevelClient.indexAsync(indexRequest, listener); // 异步方式
Thread.sleep(2000); } /*
* 查询
*
* */
// 指定routing的数据,查询也要指定
@Test
public void searchRoute()throws Exception{ GetRequest request = new GetRequest("twitter", "t_doc", "3").routing("my_route"); // 指定routing的数据,查询也要指定
GetResponse response = restHighLevelClient.get(request);
System.out.println(response.getSourceAsString());
}
//查询-额外参数 异步获取
@Test
public void getOneOp() throws IOException, InterruptedException {
ActionListener<GetResponse> listener = new ActionListener<GetResponse>() {
@Override
public void onResponse(GetResponse documentFields) {
System.out.println(documentFields.getSourceAsString());
} @Override
public void onFailure(Exception e) {
System.out.println("Error:" + e.getMessage());
e.printStackTrace();
}
}; GetRequest request = new GetRequest("twitter", "t_doc", "2");
String[] includes = new String[]{"message", "*Date"}; // 包含的字段
String[] excludes = Strings.EMPTY_ARRAY; // 排除的字段
FetchSourceContext fetchSourceContext =
new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext);
restHighLevelClient.getAsync(request,listener);
Thread.sleep(2000); } //查询所有
@Test
public void searchAll(){
HttpEntity entity = new NStringEntity(
"{ \"query\": { \"match_all\": {}}}",
ContentType.APPLICATION_JSON);
String endPoint = "/" + indexName + "/" + esType + "/_search";
try {
Response response = restHighLevelClient.getLowLevelClient()
.performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch(IOException e) {
e.printStackTrace();
} } //条件查询 姓名:李四
@Test
public void test(){
try {
String endPoint = "/" + indexName + "/" + esType + "/_search"; IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder;
try {
builder = JsonXContent.contentBuilder()
.startObject()
.startObject("query")
.startObject("match")
.field("name.keyword", "lisi")
.endObject()
.endObject()
.endObject();
indexRequest.source(builder);
} catch (IOException e) {
e.printStackTrace();
} String source = indexRequest.source().utf8ToString();
System.out.println(source); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON); Response response = restHighLevelClient.getLowLevelClient().performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity);
System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) {
e.printStackTrace();
} } //条件查询 叫kimchy的
@Test
public void testSearch(){
try {
SearchRequest searchRequest = new SearchRequest("twitter");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
sourceBuilder.from(0);
sourceBuilder.size(5);
searchRequest.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest);
System.out.println("Hits:" + response.getHits().totalHits);
response.getHits().forEach(e -> {
System.out.println(e.getSourceAsString()); }); } catch(IOException e) {
e.printStackTrace();
} } /**
* * 查询名字等于 lisi
* 并且年龄在20和40之间
*/
@Test
public void serarchFuhe(){
try {
String endPoint = "/" + indexName + "/" + esType + "/_search"; IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder;
try { builder = JsonXContent.contentBuilder()
.startObject()
.startObject("query")
.startObject("bool")
.startObject("must")
.startObject("match")
.field("name.keyword", "lisi")
.endObject()
.endObject()
.startObject("filter")
.startObject("range")
.startObject("age")
.field("gte", "20")
.field("lte", "40")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
indexRequest.source(builder);
} catch (IOException e) {
e.printStackTrace();
} String source = indexRequest.source().utf8ToString();
System.out.println(source); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON); Response response = restHighLevelClient.getLowLevelClient().performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
} }
/**
* 存在即更新【输出:OK】
* OK
* {"C":"Carambola","A":"Apple","B":"Banana"}
* 不存在则创建【输出:CREATED】
* CREATED
* {"C":"Carambola"}
* 开启scriptedUpsert【在文档不存在情况下输出:CREATED】
* {"A" : "Apple","B" : "Banana","C" : "Carambola"}
*/
@Test
public void testUpdate() throws IOException {
UpdateRequest request = new UpdateRequest("twitter", "t_doc", "7")
.script(new Script(ScriptType.INLINE,"painless",
"ctx._source.A='Apple';ctx._source.B='Banana'",Collections.EMPTY_MAP))
// 如果文档不存在,使用upsert方法定义一些内容,这些内容将作为新文档插入
.upsert(jsonBuilder()
.startObject()
.field("C","Carambola")
.endObject());
request.timeout(TimeValue.timeValueSeconds(2)); // 2秒超时
//request.scriptedUpsert(true); // 无论文档是否存在,脚本都必须运行
UpdateResponse update = restHighLevelClient.update(request);
System.out.println(update.status().name()); } //删除
@Test
public void delete(){ String endPoint = "/" + indexName + "/" + esType + "/_delete_by_query"; /**
* 删除条件
*/
IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder;
try {
builder = JsonXContent.contentBuilder()
.startObject()
.startObject("query")
.startObject("term")
//name中包含deleteText
.field("name.keyword", "wangwu")
.endObject()
.endObject()
.endObject();
indexRequest.source(builder);
} catch (IOException e) {
e.printStackTrace();
} String source = indexRequest.source().utf8ToString(); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
try {
Response response = restHighLevelClient.getLowLevelClient().performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity);
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
} } }

可看api进行测试,https://blog.csdn.net/jatpen/article/details/102631110

或者查看官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-supported-apis.html

3.3_springBoot2.1.x检索之RestHighLevelClient方式的更多相关文章

  1. 实现首字母或拼音检索-sql语句方式

    create function [dbo].[fn_GetPY](@str nvarchar(max),@type int) returns nvarchar(max) as begin ) begi ...

  2. 攻城狮在路上(壹) Hibernate(十三)--- Hibernate的检索方式(上)

    Hibernate提供了以下几种检索对象的方式: A.导航对象图检索方式. B.OID检索方式.Session.get() load(); C.HQL检索方式.Query. D.QBC检索方式.Que ...

  3. Hibernate 检索方式

    概述 •Hibernate 提供了以下几种检索对象的方式 –导航对象图检索方式:  根据已经加载的对象导航到其他对象 –OID 检索方式:  按照对象的 OID 来检索对象 –HQL 检索方式: 使用 ...

  4. Hibernate的三种常用检索方式

    Hibernate 提供了以下几种检索对象的方式 ¨       导航对象图检索方式:  根据已经加载的对象导航到其他对象 ¨       OID 检索方式:  按照对象的 OID 来检索对象 ¨   ...

  5. Hibernate的检索方式--查询数据的方式

    Hibernate 提供了以下几种检索对象的方式1导航对象图检索方式: 根据已经加载的对象导航到其他对象(根据已经加载的对象,导航到其他对象-例如一对多的查询)2OID 检索方式: 按照对象的 OID ...

  6. Hibernate -- 检索方式 HQL

    Hibernate 提供了以下几种检索对象的方式 导航对象图检索方式:  根据已经加载的对象导航到其他对象 OID 检索方式: 按照对象的OID 来检索对象 HQL 检索方式:使用面向对象的HQL查询 ...

  7. Hibernate之检索方式

    时间:2017-1-22 16:09 --检索方式Hibernate中提供了以下几种检索对象的方式:    *   导航对象图检索方式        根据已经加载额对象导航到其他对象.        ...

  8. Hibernate检索策略(抓取策略)(Hibernate检索优化)

    一.查询方法中get方法采用策略是立即检索,而load方法采用策略是延迟检索,延迟检索是在使用数据时才发送SQL语句加载数据 获取延迟加载数据方式:1.使用的时候,如果Customer c=sessi ...

  9. Lucene 实例教程(四)之检索方法总结

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者: 永恒の_☆ 地址: http://blog.csdn.net/chenghui031 ...

随机推荐

  1. 【Shiro】一、Apache Shiro简介

    一.Apache Shiro简介 1.简介 一个安全性框架 特点:功能丰富.使用简单.运行独立 核心功能: Authentication(认证):你是谁? Authorization(授权):谁能干什 ...

  2. ubuntu终端仿真程序和文件管理程序

    1.SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单的说是Windows下登录UNIX或Linux服务器主机的软件.可以理解为ubuntu下的Terminal. 如果Sec ...

  3. 在Windows的控制台和Linux的终端中显示加载进度

    Windows中 #include <stdio.h> #include <windows.h> int main() { ;//任务完成总量 int i; ; i < ...

  4. 【SVN】提交报错:×××文件is not under version control

    解决方法:1.删除出错的文件,然后在出错文件所在文件夹执行还原操作 2.VS中可将文件先排除在项目外,再包含在项目内,即可正常提交

  5. 2016 ICPC Mid-Central USA Region J. Windy Path (贪心)

    比赛链接:2016 ICPC Mid-Central USA Region 题目链接:Windy Path Description Consider following along the path ...

  6. Kali Linux 2018 更新源配置

    查看添加更新源 编辑sources.list,将kali更新源加入其中 sudo vim /etc/apt/sources.list 国内更新源 #阿里云 deb http://mirrors.ali ...

  7. Quartz特点

    运行环境 Quartz 可以运行嵌入在另一个独立式应用程序 Quartz 可以在应用程序服务器(或servlet容器)内被实例化,并且参与XA事务 Quartz 可以作为一个独立的程序运行(其自己的J ...

  8. iptables默认规则

    iptables默认规则 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [34:4104] -A INPUT -m ...

  9. 令人清爽的异步函数async、await

    1.什么是async.await? async用于声明一个函数是异步的.而await从字面意思上是"等待"的意思,就是用于等待异步完成.并且await只能在async函数中使用; ...

  10. qt5.9.1 VS2017 qalgorithms.h

    qt5.9.1只有VS2017 64位支持, 在32位工程下会出现关于qalgorithms.h的错误,参考以下内容修改该头文件解决: https://codereview.qt-project.or ...