二十七、RestClient查询文档

目录

一、MatchALL查询

二、Match查询

三、bool查询

四、排序和分页

五、高亮


一、MatchALL查询

    @Test
    void testMatchAll() throws IOException {
//      准备Request对象
        SearchRequest request = new SearchRequest("hotel");
//      准备DSL
        request.source().query(QueryBuilders.matchAllQuery());
//      发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//      解析响应
        SearchHits searchHits = response.getHits();
//      获取总条数
        long value = searchHits.getTotalHits().value;
        System.out.println("共搜索到"+value+"条数据!");
//      获取文档数组
        SearchHit[] hits = searchHits.getHits();
//      遍历
        for (SearchHit hit : hits) {
//          获取文档source
            String json = hit.getSourceAsString();
//          反序列化
            HotelDoc object = JSON.parseObject(json, HotelDoc.class);
            System.out.println("object = " + object);
        }
    }

二、Match查询

    @Test
    void testMatch() throws IOException {
//      准备Request对象
        SearchRequest request = new SearchRequest("hotel");
//      准备DSL
        request.source().query(QueryBuilders.matchQuery("name","连锁"));
//      发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//      解析响应
        SearchHits searchHits = response.getHits();
//      获取总条数
        long value = searchHits.getTotalHits().value;
        System.out.println("共搜索到"+value+"条数据!");
//      获取文档数组
        SearchHit[] hits = searchHits.getHits();
//      遍历
        for (SearchHit hit : hits) {
//          获取文档source
            String json = hit.getSourceAsString();
//          反序列化
            HotelDoc object = JSON.parseObject(json, HotelDoc.class);
            System.out.println("object = " + object);
        }
    }

三、bool查询

    @Test
    void testBool() throws IOException {
//      准备Request对象
        SearchRequest request = new SearchRequest("hotel");
//      准备DSL
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.termQuery("city","杭州"));
        boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));
        request.source().query(boolQuery);
//      发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        extracted(response);
    }

四、排序和分页

二十七、RestClient查询文档_第1张图片

    @Test
    void testMatchAll() throws IOException {
//      准备Request对象
        SearchRequest request = new SearchRequest("hotel");
//      准备DSL
        request.source().query(QueryBuilders.matchAllQuery());
//      分页
        request.source().from(0).size(5);
//      排序
        request.source().sort("price", SortOrder.ASC);
//      发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//      解析响应
        extracted(response);
    }

五、高亮

所有搜索DSL的构建,记住一个API    :   SearchRequest的source()方法。

高亮结果解析是参考JSON结果,逐层解析

     @Test
    void testHighLight() throws IOException {
//      准备Request对象
        SearchRequest request = new SearchRequest("hotel");
//      准备DSL
        request.source().query(QueryBuilders.matchQuery("name", "连锁"));
        request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
//      发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        extracted(response);
    }   


private void extracted(SearchResponse response) {
//      解析响应
        SearchHits searchHits = response.getHits();
//      获取总条数
        long value = searchHits.getTotalHits().value;
        System.out.println("共搜索到" + value + "条数据!");
//      获取文档数组
        SearchHit[] hits = searchHits.getHits();
//      遍历
        for (SearchHit hit : hits) {
//          获取文档source
            String json = hit.getSourceAsString();
//          反序列化
            HotelDoc object = JSON.parseObject(json, HotelDoc.class);
//          获取高亮结果
            Map highlightFields = hit.getHighlightFields();
            if (!CollectionUtils.isEmpty(highlightFields)){
//              根据字段名获取高亮结果
                HighlightField highlightField = highlightFields.get("name");
                if (highlightFields != null){
//                  获取高亮值
                    String name = highlightField.getFragments()[0].string();
//                  覆盖非高亮结果
                    object.setName(name);
                }
            }
            System.out.println("object = " + object);
        }
    }

你可能感兴趣的:(微服务学习,java,前端,servlet)