本文介绍: 所有搜索DSL的构建记住一个API : SearchRequestsource()方法高亮结果解析是参考JSON结果,逐层解析

目录

一、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);
    }

四、排序分页

    @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<String, HighlightField> 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);
        }
    }

原文地址:https://blog.csdn.net/icbbm/article/details/134628779

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_10385.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注