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

相关文章

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

Python使用DrissionPage中ChromiumPage进行自动化网页操作

《Python使用DrissionPage中ChromiumPage进行自动化网页操作》DrissionPage作为一款轻量级且功能强大的浏览器自动化库,为开发者提供了丰富的功能支持,本文将使用Dri... 目录前言一、ChromiumPage基础操作1.初始化Drission 和 ChromiumPage

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

Redis中管道操作pipeline的实现

《Redis中管道操作pipeline的实现》RedisPipeline是一种优化客户端与服务器通信的技术,通过批量发送和接收命令减少网络往返次数,提高命令执行效率,本文就来介绍一下Redis中管道操... 目录什么是pipeline场景一:我要向Redis新增大批量的数据分批处理事务( MULTI/EXE

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

Oracle存储过程里操作BLOB的字节数据的办法

《Oracle存储过程里操作BLOB的字节数据的办法》该篇文章介绍了如何在Oracle存储过程中操作BLOB的字节数据,作者研究了如何获取BLOB的字节长度、如何使用DBMS_LOB包进行BLOB操作... 目录一、缘由二、办法2.1 基本操作2.2 DBMS_LOB包2.3 字节级操作与RAW数据类型2.

JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)

《JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)》本文介绍了如何在Windows系统上配置多版本JDK(以JDK8和JDK17为例),并通过图文结合的方式给大家讲解了详细步骤,具有... 目录第一步 下载安装JDK第二步 配置环境变量第三步 切换JDK版本并验证可能遇到的问题前提:公司常