当前位置: 首页 > news >正文

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
http://www.lryc.cn/news/380858.html

相关文章:

  • 『Z-Weekly Feed 08』加密资产观 | FHE应用前景 | OPAL协议
  • 酒店预定系统
  • Redis的实战常用一、验证码登录(解决session共享问题)(思路、意识)
  • 基于Spring Boot的智能分析平台
  • HTML(13)——显示模式
  • 【Spring】Spring Boot 快速入门
  • Go自定义数据的序列化流程
  • 贪心算法练习题(2024/6/18)
  • 4.1 四个子空间的正交性
  • RabbitMQ实践——使用WebFlux响应式方式实时返回队列中消息
  • SpringBoot前后端传递数据时常用的JSON格式数据是什么?【讲解JSON概念、语法、以及Java对象互转】
  • mysql学习——SQL中的DQL和DCL
  • windows系统上nginx搭建文件共享
  • 星闪指向遥控,做家电交互的破壁人
  • SpringBoot使用AutoConfigure实现依赖库自动导入配置
  • QT中利用动画弄一个侧边栏窗口,以及贴条效果
  • win10免安装配置MySQL8.4.0
  • VS Code安装及环境配置(超详细)
  • shell脚本通过解析日志使用串口开关屏知识点整理
  • 速盾:视频cdn和网站cdn的相同点与不同点
  • 37.自定义协议
  • 【React Native】measureInWindow在安卓上无法正确获取View在屏幕上的布局信息
  • C++ 教程 - 04 类的使用
  • excel按模板文件导出多个文件并压缩为ZIP格式返回前端
  • 自动驾驶仿真测试用例表格示例 ACC ELK FCW
  • 数组 (java)
  • 时序预测 | Matlab基于Transformer多变量时间序列多步预测
  • suuk-s.php.jpg-python 库劫持
  • python3GUI--ktv点歌软件By:PyQt5(附下载地址)
  • opencascade AIS_InteractiveContext源码学习2