9 RestClient客户端操作文档

2024-06-24 08:28

本文主要是介绍9 RestClient客户端操作文档,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. match_all

@GetMapping("matchAll")public void matchAll() throws IOException {//1. 准备requestSearchRequest request = new SearchRequest("hotel");//2. 组织DSL参数request.source().query(QueryBuilders.matchAllQuery());SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);//3.解析结果SearchHits searchHits = response.getHits();//4. 查询总条数long value = searchHits.getTotalHits().value;System.out.println("总条数: " + value);//5.  数据SearchHit[] hits = searchHits.getHits();for (SearchHit hit: hits) {String json = hit.getSourceAsString();System.out.println(json);}}
2024-06-24 00:57:38.000  INFO 8968 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-24 00:57:38.000  INFO 8968 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-24 00:57:38.000  INFO 8968 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
总条数: 4
{"address":"柳州东路1号","brand":"如家","business":"弘阳商圈","city":"南京市浦口区","id":1,"location":"33.33,131.33","name":"如家","pic":"http://www.bai.com/images/rujia.png","price":189,"score":7,"starName":"二星"}
{"address":"月华西路2号","brand":"7天","business":"江宁商圈","city":"南京市江宁区","id":2,"location":"33.33,131.35","name":"7天","pic":"http://www.bai.com/images/7.png","price":188,"score":7,"starName":"二星"}
{"address":"洪武北路1号","brand":"四季","business":"新街口商圈","city":"南京市玄武区","id":3,"location":"33.35,131.36","name":"四季","pic":"http://www.bai.com/images/7.png","price":489,"score":8,"starName":"3星"}
{"address":"是单独发2号","brand":"四季如春","business":"新街口商圈","city":"南京市鼓楼区","id":4,"location":"66.66,133.36","name":"四季如春","pic":"http://www.qiniu.com/images/xxx.png","price":999,"score":9,"starName":"五星"}

2.全文检索查询

单字段查询
//单字段查询@GetMapping("singleMatch")public void singleMatch() throws IOException {//1. 准备requestSearchRequest request = new SearchRequest("hotel");//2. 组织DSL参数request.source().query(QueryBuilders.matchQuery("name","四季如春"));SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);//3.解析结果SearchHits searchHits = response.getHits();//4. 查询总条数long value = searchHits.getTotalHits().value;System.out.println("总条数: " + value);//5.  数据SearchHit[] hits = searchHits.getHits();for (SearchHit hit: hits) {String json = hit.getSourceAsString();System.out.println(json);}}

name搜索字段参与了分词,因此会查询出两条记录

2024-06-24 01:12:58.956  INFO 7784 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-24 01:12:58.957  INFO 7784 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
总条数: 2
{"address":"是单独发2号","brand":"四季如春","business":"新街口商圈","city":"南京市鼓楼区","id":4,"location":"66.66,133.36","name":"四季如春","pic":"http://www.qiniu.com/images/xxx.png","price":999,"score":9,"starName":"五星"}
{"address":"洪武北路1号","brand":"四季","business":"新街口商圈","city":"南京市玄武区","id":3,"location":"33.35,131.36","name":"四季","pic":"http://www.bai.com/images/7.png","price":489,"score":8,"starName":"3星"}
2024-06-24 01:13:29.036 ERROR 7784 --- [rRegistryThread] c.xxl.job.core.util.XxlJobRemotingUtil   : Connection refused: connect

多字段查询

//多字段查询@GetMapping("multiMatch")public void multiMatch() throws IOException {//1. 准备requestSearchRequest request = new SearchRequest("hotel");//2. 组织DSL参数request.source().query(QueryBuilders.multiMatchQuery("四季如春","name","brand"));SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);//3.解析结果SearchHits searchHits = response.getHits();//4. 查询总条数long value = searchHits.getTotalHits().value;System.out.println("总条数: " + value);//5.  数据SearchHit[] hits = searchHits.getHits();for (SearchHit hit: hits) {String json = hit.getSourceAsString();System.out.println(json);}}
2024-06-24 01:15:22.728  INFO 4552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-24 01:15:22.730  INFO 4552 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
总条数: 2
{"address":"是单独发2号","brand":"四季如春","business":"新街口商圈","city":"南京市鼓楼区","id":4,"location":"66.66,133.36","name":"四季如春","pic":"http://www.qiniu.com/images/xxx.png","price":999,"score":9,"starName":"五星"}
{"address":"洪武北路1号","brand":"四季","business":"新街口商圈","city":"南京市玄武区","id":3,"location":"33.35,131.36","name":"四季","pic":"http://www.bai.com/images/7.png","price":489,"score":8,"starName":"3星"}
2024-06-24 01:15:43.546 ERROR 4552 --- [rRegistryThread] c.xxl.job.core.util.XxlJobRemotingUtil   : Connection refused: connect

term查询

@GetMapping("term")public void term() throws IOException {//1. 准备requestSearchRequest request = new SearchRequest("hotel");//2. 组织DSL参数request.source().query(QueryBuilders.termQuery("city","南京市浦口区"));SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);//3.解析结果SearchHits searchHits = response.getHits();//4. 查询总条数long value = searchHits.getTotalHits().value;System.out.println("总条数: " + value);//5.  数据SearchHit[] hits = searchHits.getHits();for (SearchHit hit: hits) {String json = hit.getSourceAsString();System.out.println(json);}}
2024-06-24 01:19:07.854  INFO 6212 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-24 01:19:07.854  INFO 6212 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-24 01:19:07.855  INFO 6212 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
总条数: 1
{"address":"柳州东路1号","brand":"如家","business":"弘阳商圈","city":"南京市浦口区","id":1,"location":"33.33,131.33","name":"如家","pic":"http://www.bai.com/images/rujia.png","price":189,"score":7,"starName":"二星"}

range范围查询

@GetMapping("range")public void range() throws IOException {//1. 准备requestSearchRequest request = new SearchRequest("hotel");//2. 组织DSL参数request.source().query(QueryBuilders.rangeQuery("price").gte(200).lt(500));SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);//3.解析结果SearchHits searchHits = response.getHits();//4. 查询总条数long value = searchHits.getTotalHits().value;System.out.println("总条数: " + value);//5.  数据SearchHit[] hits = searchHits.getHits();for (SearchHit hit: hits) {String json = hit.getSourceAsString();System.out.println(json);}}
2024-06-24 01:21:50.603  INFO 8916 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-24 01:21:50.604  INFO 8916 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
总条数: 1
{"address":"洪武北路1号","brand":"四季","business":"新街口商圈","city":"南京市玄武区","id":3,"location":"33.35,131.36","name":"四季","pic":"http://www.bai.com/images/7.png","price":489,"score":8,"starName":"3星"}

bool复合查询

@GetMapping("boolQuery")public void boolQuery() throws IOException {//1. 准备requestSearchRequest request = new SearchRequest("hotel");//2. 组织DSL参数BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();//添加must条件boolQuery.must(QueryBuilders.termQuery("city","南京市浦口区"));//添加filter条件boolQuery.filter(QueryBuilders.rangeQuery("price").lt(200));request.source().query(boolQuery);SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);//3.解析结果SearchHits searchHits = response.getHits();//4. 查询总条数long value = searchHits.getTotalHits().value;System.out.println("总条数: " + value);//5.  数据SearchHit[] hits = searchHits.getHits();for (SearchHit hit: hits) {String json = hit.getSourceAsString();System.out.println(json);}}
2024-06-24 01:26:50.886  INFO 15052 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-24 01:26:50.886  INFO 15052 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
总条数: 1
{"address":"柳州东路1号","brand":"如家","business":"弘阳商圈","city":"南京市浦口区","id":1,"location":"33.33,131.33","name":"如家","pic":"http://www.bai.com/images/rujia.png","price":189,"score":7,"starName":"二星"}
2024-06-24 01:27:15.288 ERROR 15052 --- [rRegistryThread] c.xxl.job.core.util.XxlJobRemotingUtil   : Connection refused: connect

这篇关于9 RestClient客户端操作文档的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089628

相关文章

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

使用JavaScript操作本地存储

《使用JavaScript操作本地存储》这篇文章主要为大家详细介绍了JavaScript中操作本地存储的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录本地存储:localStorage 和 sessionStorage基本使用方法1. localStorage

使用JavaScript将PDF页面中的标注扁平化的操作指南

《使用JavaScript将PDF页面中的标注扁平化的操作指南》扁平化(flatten)操作可以将标注作为矢量图形包含在PDF页面的内容中,使其不可编辑,DynamsoftDocumentViewer... 目录使用Dynamsoft Document Viewer打开一个PDF文件并启用标注添加功能扁平化

JavaScript DOM操作与事件处理方法

《JavaScriptDOM操作与事件处理方法》本文通过一系列代码片段,详细介绍了如何使用JavaScript进行DOM操作、事件处理、属性操作、内容操作、尺寸和位置获取,以及实现简单的动画效果,涵... 目录前言1. 类名操作代码片段代码解析2. 属性操作代码片段代码解析3. 内容操作代码片段代码解析4.

SpringBoot3集成swagger文档的使用方法

《SpringBoot3集成swagger文档的使用方法》本文介绍了Swagger的诞生背景、主要功能以及如何在SpringBoot3中集成Swagger文档,Swagger可以帮助自动生成API文档... 目录一、前言1. API 文档自动生成2. 交互式 API 测试3. API 设计和开发协作二、使用

SpringBoot使用Apache POI库读取Excel文件的操作详解

《SpringBoot使用ApachePOI库读取Excel文件的操作详解》在日常开发中,我们经常需要处理Excel文件中的数据,无论是从数据库导入数据、处理数据报表,还是批量生成数据,都可能会遇到... 目录项目背景依赖导入读取Excel模板的实现代码实现代码解析ExcelDemoInfoDTO 数据传输

Python使用asyncio实现异步操作的示例

《Python使用asyncio实现异步操作的示例》本文主要介绍了Python使用asyncio实现异步操作的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录1. 基础概念2. 实现异步 I/O 的步骤2.1 定义异步函数2.2 使用 await 等待异

SpringBoot实现websocket服务端及客户端的详细过程

《SpringBoot实现websocket服务端及客户端的详细过程》文章介绍了WebSocket通信过程、服务端和客户端的实现,以及可能遇到的问题及解决方案,感兴趣的朋友一起看看吧... 目录一、WebSocket通信过程二、服务端实现1.pom文件添加依赖2.启用Springboot对WebSocket

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核