ElasticSearch学习5-- 使用RestClient查询文档

1、查询基本步骤

1、创建SearchRequest对象
2、准备Request.source(),也就是DSL。
        QueryBuilders来构建查询条件
       传入Request.source() 的 query() 方法
3、发送请求,得到结果
4、解析结果(参考JSON结果,从外到内,逐层解析)

RestAPI中其中构建DSL是通过HighLevelRestClient中的resource()返回的SearchSourceBuilder来实现的,其中包含了查询、排序、分页、高亮等所有功能

SearchSourceBuilder 进行查询、排序、分页、高亮

ElasticSearch学习5-- 使用RestClient查询文档_第1张图片

RestAPI中其中构建查询条件的核心部分是由一个名为QueryBuilders的工具类提供的,其中包含了各种查询方法。

QueryBuilders 进行match_all /match /multi_match /term /range/boolmatch等查询

ElasticSearch学习5-- 使用RestClient查询文档_第2张图片

查询所有

    @Test
    public void testMatchAll() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是所有查询
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        //将查询条件赋值给查询语句
        request.source().query(matchAllQueryBuilder);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);
        
    }

2、多种检索查询

match、multi_match查询

    @Test
    public void testMatchQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是所有查询
        MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("fileId","赎吧");
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);

    }
@Test
    public void testMultiMatchQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是所有查询
        MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("B端赎吧","fileId","caseDes");
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);

    }

term 、range查询

@Test
    public void testTermQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是所有查询
        TermQueryBuilder queryBuilder = QueryBuilders.termQuery("oa","lilan04");
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);

    }

    @Test
    public void testRangeQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("test_1");
        //构建查询条件,下边是所有查询
        RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age").gte(10).lte(20);
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);

    }

布尔查询

 @Test
    public void testBooleanQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是所有查询
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));
        queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));
        queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);

    }

分页、排序、高亮查询

@Test
    public void testSortQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是布尔查询
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));
        queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));
        queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //排序
        request.source().sort("editTime", SortOrder.ASC);
        //分页
        request.source().from(1).size(10);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);

    }

    @Test
    public void testHightLightQuery() throws IOException {
        //创建查询请求,指定要查询的索引库
        SearchRequest request = new SearchRequest("case_management");
        //构建查询条件,下边是布尔查询
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        queryBuilder.must(QueryBuilders.matchQuery("oa","lilan04"));
        queryBuilder.should(QueryBuilders.rangeQuery("editTime").gte(1684740000000L).lte(1684740070219l));
        queryBuilder.filter(QueryBuilders.termQuery("isDelete",1));
        //将查询条件赋值给查询语句
        request.source().query(queryBuilder);
        //自定义高亮 查找
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("");
        highlightBuilder.postTags("");
        highlightBuilder.field("oa");
        // highlightBuilder.requireFieldMatch(false); //多字段时,需要设置为false
        // request.source().highlighter(highlightBuilder);
        //排序
        request.source().sort("editTime", SortOrder.ASC);
        //分页
        request.source().from(1).size(10);
        //进行查询
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        System.out.println("respon话费时间 " + response.getTook());
        //对结果进行处理
        SearchHits hits = response.getHits();
        //返回结果列表
        SearchHit[] hitsList = hits.getHits();
        //总数
        long totalHits = hits.getTotalHits();
        System.out.println(totalHits);
        SearchHit documentFields = hitsList[0];
        String json = documentFields.getSourceAsString();
        System.out.println(json);
        //获取到高亮字段
        System.out.println(documentFields.getHighlightFields());

    }

3、错误解决

场景

    ES对text类型的字段进行聚合操作的时候, 报 Fielddata is disabled on text fields by default. Set fielddata=true on [make] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

分析

    官方文档
    sort, aggregate, or access values from a script on a text field 这些行为会需要从内存中加载数据, 但是text类型的数据可能会消耗非常多内存, 另外可能会导致查询延迟, 所以默认不允许对text字段这样操作
所以最好对非text类型字段进行排序等操作

你可能感兴趣的:(ElasticSearch,微服务相关,elasticsearch,学习,大数据)