1.方式一

1.1 依赖

【不使用 spring-boot-starter-data-elasticsearch 就可以脱离 springboot 版本的限制,可以自行选择ES的版本】我用的是 springboot 2.2.5.RELEASE 版本,ES部署文件为 elasticsearch-6.4.3.tar.gz,这里只贴出主要依赖:

<!-- ElasticSearch Server -->
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>6.4.3</version>
</dependency>
<!-- Java High Level REST Client -->
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.4.3</version>
</dependency>

1.2 配置信息

这里根据elasticsearch服务端的地址进行配置:

@Configuration
public class ElasticSearchConfig {@Beanpublic RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));return client;}
}

1.3 客户端使用

@SpringBootTest
class EsApiApplicationTests {@Autowired@Qualifier(value = "restHighLevelClient")private RestHighLevelClient client;/*1.创建索引*/@Testvoid createIndex() throws IOException {// 创建请求CreateIndexRequest request = new CreateIndexRequest("yz_index");// 执行请求,获取响应CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);System.out.println("creatIndex--" + response);}/*2.获取索引,判断是否存在*/@Testvoid getIndex() throws IOException {// 创建请求GetIndexRequest request = new GetIndexRequest("yz_index");// 执行请求,获取响应boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println("getIndex--exists--" + exists);}/*3.删除索引*/@Testvoid deleteIndex() throws IOException {// 创建请求DeleteIndexRequest request = new DeleteIndexRequest("yz_index");// 执行请求AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println("deleteIndex--" + response);}/*4.添加文档*/@Testvoid addDocument() throws IOException {// 创建对象User user = new User("Java虚拟机", 30);// 创建索引,即获取索引IndexRequest indexRequest = new IndexRequest("yz_index");// 添加规则 /index/_doc/idindexRequest.id("1");indexRequest.timeout(TimeValue.timeValueSeconds(1));// 存入对象indexRequest.source(JSON.toJSONString(user), XContentType.JSON);// 发送请求IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);System.out.println("addDocument--" + response);}/*5.获取文档是否存在*/@Testvoid getDocument() throws IOException {// 创建get请求GetRequest getRequest = new GetRequest("yz_index", "1");// 不获取返回的_source的上下文getRequest.fetchSourceContext(new FetchSourceContext(false));getRequest.storedFields("_none_");// 发送请求boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);System.out.println("addDocument--" + exists);/*6.获取文档信息*/if (exists) {// 创建请求GetRequest getRequest1 = new GetRequest("yz_index", "1");// 发送请求GetResponse response = client.get(getRequest1, RequestOptions.DEFAULT);System.out.println("source--" + response.getSource());System.out.println("getDocument--" + response.getSourceAsString());}/*7.更新文档信息*/if (exists) {// 创建请求UpdateRequest updateRequest = new UpdateRequest("yz_index", "1");updateRequest.timeout("1s");// 修改内容User user = new User("小米", 10);updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT);System.out.println("updateDocument--" + response.status());}/*8.删除文档信息*/if (exists) {// 创建请求DeleteRequest deleteRequest = new DeleteRequest("yz_index", "1");deleteRequest.timeout("1s");// 修改内容DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println("deleteDocument--" + response.status());}}/*9.批量添加文档*/@Testvoid batchAddDocument() throws IOException {// 批量请求BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("10s");// 创建对象ArrayList<User> userArrayList = new ArrayList<>();userArrayList.add(new User("小米1", 1));userArrayList.add(new User("小米2", 2));userArrayList.add(new User("小米3", 3));userArrayList.add(new User("小米4", 4));userArrayList.add(new User("小米5", 5));// 添加请求for (int i = 0; i < userArrayList.size(); i++) {bulkRequest.add(new IndexRequest("yz_index").id("" + (i + 2)).source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON));}// 执行请求BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);System.out.println("batchAddDocument--" + response.status());}/*10.查询*/@Testvoid search() throws IOException {// 创建请求SearchRequest searchRequest = new SearchRequest("jd_index");// 构建搜索条件SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "杜伽");// 精确查询searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));searchRequest.source(searchSourceBuilder);// 执行请求SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);// 解析查询结果System.out.println("search--getHists--" + JSON.toJSONString(response.getHits()));for (SearchHit documentFields : response.getHits()) {System.out.println("search--getHist--" + documentFields.getSourceAsMap());}}
}

2.方式二

2.1 依赖

我用的是 springboot 2.5.4【所以spring-boot-starter-data-elasticsearch 的版本也是 2.5.4】此时对应的 elasticsearch 服务端和客户端的版本是 7.12.1 那要部署的ES版本也要是 7.12.1:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2.2 配置信息

spring:elasticsearch:rest:uris: localhost:9200

2.3 客户端使用

@SpringBootTest
class EsStarterApplicationTests {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testvoid contextLoads() {// 由于不同版本的API不同【没有参考价值】,这里不再贴出测试代码。}}

3.注意事项

不同的 elasticsearch 版本有不同的 API 这就给升级造成了阻碍,为了避免这种情况,我们可以使用elasticsearch的 HTTP 客户端 Jest,下篇文章会详细说明

【SpringBoot集成ElasticSearch 01】2️⃣ 种方式的高级客户端 RestHighLevelClient 使用(依赖+配置+客户端API测试源码)相关推荐

  1. Spring 依赖注入三种方式的实现,及循环依赖问题的解决(源码+XML配置)

    搬砖啦,搬砖啦,这几天在看Spring相关的书,下面给大家分享一下这几天的心得与收获,Go Go Go! Spring支持两种依赖注入方式,分别是属性注入,构造函数注入.除此之外,Spring还支持工 ...

  2. 【SpringBoot高级篇】SpringBoot集成Elasticsearch搜索引擎

    [SpringBoot高级篇]SpringBoot集成Elasticsearch搜索引擎 1. 什么是Elasticsearch? 2. 安装并运行Elasticsearch 2.1 拉取镜像 2.2 ...

  3. SpringBoot集成ElasticSearch在启动时报availableProcessors is already set to [8], rejecting [8]

    背景 项目基于SpringBoot并且集成ElasticSearch,今天在编写测试类准备进行单元测试时,报了如下这个错误. Caused by: org.springframework.beans. ...

  4. springboot——集成elasticsearch进行搜索并高亮关键词

    目录 1.elasticsearch概述 3.springboot集成elasticsearch 4.实现搜索并高亮关键词 1.elasticsearch概述 (1)是什么: Elasticsearc ...

  5. springboot 注册dao层 service 层的三种方式,高级内容详解

    可以使用三种注解来引入DAO层的接口到spring容器中. 1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包 ...

  6. elasticsearch数据存储结构,springboot集成elasticsearch

    一.数据存储结构 结合数据库的结构理解起来就会比较清楚: 1)索引(Index)=>数据库(Database). 2)类型(Type)=>表(Table). 3)文档(Document)= ...

  7. SpringBoot定时任务实现的两种方式介绍

    今天给大家介绍SpringBoot定时任务实现的几种方式,希望对大家能有所帮助! 1.SpringTask 用法 框架介绍:SpringTask是Spring自带的轻量级定时任务工具,相比于Quart ...

  8. SpringBoot 配置文件加密的两种方式

    SpringBoot配置文件加密的两种方式 jasypt使用方式 用法一: 1.Application.java上增加注解@EnableEncryptableProperties(jasypt-spr ...

  9. 史上最简单的Elasticsearch教程:SpringBoot集成Elasticsearch 实时流量监测平台

    SpringBoot集成Elasticsearch 实时流量监测平台 目录: 第一章:初尝 Elasticsearch 第二章:玩转 Kibana 第三章:开发原生 Elasticsearch 案例 ...

最新文章

  1. K-近邻算法之kd树
  2. js用.和[]获取属性的区别
  3. Abp太重了?轻量化Abp框架
  4. HDU 5145 - NPY and girls
  5. dhcpd.conf配置的有关说明
  6. C++ 求指定函数的定积分问题
  7. web课程设计网页规划与设计 :DW旅游主题网页设计——凤阳智慧旅游官方-地方旅游网站模板html源码HTML+CSS+JavaScript
  8. 医药电商平台解决方案
  9. machine learning measurements
  10. 经典~吸引力法则:你相信什么,就会吸引什么,获得什么
  11. 2021年微软研究博士奖研金名单出炉!三位华人博士生入选,每人42000美元
  12. 详解电商订单逻辑流程图
  13. 解决tomcat报limt 字节长度限制 tomcat web.xml里配置mappedfile
  14. 如何使用ArcGIS将Excel数据转换为shp格式
  15. 用计算机弹音乐我们一起猫叫,抖音上面我们一起学猫叫一起喵喵喵是什么歌 抖音学猫叫歌曲歌词...
  16. 基于node实现qq空间点赞
  17. 全球最大 IPO,我们能否赚笔养老钱?
  18. 利用环境监测设备做好这几点,菠菜出苗快、产量高
  19. mybatis plus 执行 原始的sql 语句
  20. A4988/DRV8825步进电机驱动拓展板(HW-434)资料

热门文章

  1. distable:table-cell
  2. 为什么需要自己实现前端框架
  3. Struts1 中的 global-forward
  4. oracle shared_pool_size 0,Oracle shared_pool_reserved_size参数设置说明
  5. python列表常用操作_Python列表(list)常用操作方法小结
  6. django language_Django基础学习-创建第一个Django项目
  7. sql server 链接服务器 改访问接口_跨服务器链接数据库?其实很简单!(上)
  8. html5 建筑物模型,基于HTML5的建筑物阴影实时模拟
  9. python抓取股票数据_Python股票处理之一_获取国内股票数据
  10. map集合遍历_java---map集合获取元素与存储元素