RestClient查询文档

一、快速入门

//RestClient查询文档
    @Test
    void testMatchAll() throws IOException {
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotel");
        // 2.准备DSL,QueryBuilders来构建查询条件,传入request.source()的query()方法
        request.source()
                .query(QueryBuilders.matchAllQuery());
        // 3.发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        // 4.解析响应
        handleResponse(response);
    }

    private void handleResponse(SearchResponse response) {
        // 4.解析响应
        SearchHits searchHits = response.getHits();
        // 4.1.获取总条数
        long total = searchHits.getTotalHits().value;
        System.out.println("共搜索到" + total + "条数据");
        // 4.2.文档数组
        SearchHit[] hits = searchHits.getHits();
        // 4.3.遍历
        for (SearchHit hit : hits) {
            // 获取文档source
            String json = hit.getSourceAsString();
            // 反序列化
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            System.out.println("hotelDoc = " + hotelDoc);
        }
    }

二、全文检索查询

RestClient查询文档_第1张图片

@Test
    void testMatch() throws IOException {
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotel");
        // 2.准备DSL,QueryBuilders来构建查询条件,传入request.source()的query()方法
        request.source()
                .query(QueryBuilders.matchQuery("all","如家"));
        // 3.发送请求,得到结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);

    }

解析结果和上例一样!

三、精确查询、复合查询

RestClient查询文档_第2张图片
RestClient查询文档_第3张图片

 @Test
    void testBool() throws IOException {
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotel");
        // 2.准备DSL,QueryBuilders来构建查询条件,传入request.source()的query()方法
        //2.1 准备BooleanQuery
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //2.2  添加term
        boolQuery.must(QueryBuilders.termQuery("city","上海"));
        //boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));
        request.source().query(boolQuery);
        // 3.发送请求,得到结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);

    }

四、排序和分页

RestClient查询文档_第4张图片

@Test
    void testPageAndSort() throws IOException {
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotel");
        // 2.准备DSL,QueryBuilders来构建查询条件,传入request.source()的query()方法
        //2.1 query
        request.source().query(QueryBuilders.matchAllQuery());

        //2.2 排序sort
        request.source().sort("price", SortOrder.ASC);

        //2.3 分页 from,size
        request.source().from(0).size(5);


        // 3.发送请求,得到结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);

    }

五、高亮显示

RestClient查询文档_第5张图片
RestClient查询文档_第6张图片

Test
    void testHighlight() throws IOException {
        // 1.准备Request
        SearchRequest request = new SearchRequest("hotel");
        // 2.准备DSL,QueryBuilders来构建查询条件,传入request.source()的query()方法
        //2.1 query
        request.source().query(QueryBuilders.matchQuery("all","如家"));
        //2.2 高亮
        request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));

        // 3.发送请求,得到结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        handleResponse(response);

    }

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

            }

            System.out.println("hotelDoc = " + hotelDoc);
        }
    }

RestClient查询文档_第7张图片

你可能感兴趣的:(ElasticSearch,后端,elasticsearch)