9 RestClient客户端操作文档

1. match_all

@GetMapping("matchAll")
    public void matchAll() throws IOException {
        //1. 准备request
        SearchRequest 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. 准备request
        SearchRequest 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. 准备request
        SearchRequest 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. 准备request
        SearchRequest 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. 准备request
        SearchRequest 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. 准备request
        SearchRequest 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

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值