Es7.x使用RestHighLevelClient进行查询操作

Posted OkidoGreen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Es7.x使用RestHighLevelClient进行查询操作相关的知识,希望对你有一定的参考价值。

  1. 数据准备
  2. 查询操作
    2.1 match查询
    2.2 bool查询
    2.3 constant_score查询
    2.4 matchPhraseQuery搜索
    2.5 range范围查询

1. 数据准备

数据准备:接入方式详见Es7.x使用RestHighLevelClient进行增删改和批量操作

PUT test_demo

PUT test_demo/_mapping

  "properties":
    "id":
      "type":"long"
    ,
    "title":
      "type":"text",
      "analyzer": "ik_max_word"
    ,
    "tag":
      "type":"keyword"
    ,
    "publishTime":
      "type":"date",
      "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    
  

批量存储数据

    public static void testBatch() 
        List<IndexRequest> indexRequests = new ArrayList<>();

        ArrayList<DemoDto> demoDtos = new ArrayList<>();


        demoDtos.add(new DemoDto(1001L, "英国新冠疫情严重", "疫情", new Date()));
        demoDtos.add(new DemoDto(1002L, "疫情影响毕业生找工作", "疫情", new Date()));
        demoDtos.add(new DemoDto(1003L, "公安子女赴美签证被拒?中方回应", "教育", new Date()));
        demoDtos.add(new DemoDto(1004L, "澳军舰疑撞死两头濒危鲸鱼", "军事", new Date()));
        demoDtos.add(new DemoDto(1005L, "2021全国高校毕业生909万", "教育", new Date()));
        demoDtos.add(new DemoDto(1006L, "英国航母补给舰起火", "军事", new Date()));


        demoDtos.forEach(e -> 
            IndexRequest request = new IndexRequest("test_demo");
            //填充id
            request.id(e.getId() + "");
            //先不修改id
            request.source(JSON.toJSONString(e), XContentType.JSON);
            request.opType(DocWriteRequest.OpType.CREATE);
            indexRequests.add(request);
        );
        indexRequests.forEach(bulkProcessor::add);
    

执行结果:

17:33:15.759 [elasticsearch[scheduler][T#1]] INFO com.tellme.Test.EsTest - 1. 【beforeBulk】批次[1] 携带 6 请求数量
17:33:16.083 [I/O dispatcher 1] INFO com.tellme.Test.EsTest - 2. 【afterBulk-成功】批量 [1] 完成在 308 ms

2. 查询操作

es7.x(9)— match query的参数

2.1 match查询

    /**
     * match查询
     */
    public static void testMatch() throws Exception 
        RestHighLevelClient restHighLevelClient = EsUtil.getRestHighLevelClient();
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("test_demo");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("title", "英国");
        //忽略格式错误
        matchQueryBuilder.lenient();
        searchSourceBuilder.query(matchQueryBuilder);
        //放入Request对象中
        log.info("dsl:" + searchSourceBuilder.toString());

        //组装语句
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        //获取到结果
        SearchHits hits = response.getHits();
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) 
            log.info("输出数据:" + iterator.next().getSourceAsString());
        
    

转换后的语句:

GET test_demo/_search

    "query":
        "match":
            "title":
                "query":"英国",
                "operator":"OR",
                "prefix_length":0,
                "max_expansions":50,
                "fuzzy_transpositions":true,
                "lenient":false,
                "zero_terms_query":"NONE",
                "auto_generate_synonyms_phrase_query":true,
                "boost":"1.0"
            
        
    

2.2 bool查询

   public static void testBool() throws IOException 
        SearchRequest searchRequest=new SearchRequest();
        //构建dsl语句
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();

        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("title","英国"));

        boolQueryBuilder.filter(QueryBuilders.termQuery("title","疫情"));

        searchSourceBuilder.query(boolQueryBuilder);
        log.info("dsl:"+searchSourceBuilder.toString());
        //填充到Request对象中
        searchRequest.indices("test_demo");
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = EsUtil.getRestHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) 
            log.info("输出数据:" + iterator.next().getSourceAsString());
        

    

转换后的dsl:

GET test_demo/_search

    "query":
        "bool":
            "filter":[
                
                    "term":
                        "title":
                            "value":"疫情",
                            "boost":"1.0"
                        
                    
                
            ],
            "must_not":[
                
                    "match":
                        "title":
                            "query":"英国",
                            "operator":"OR",
                            "prefix_length":0,
                            "max_expansions":50,
                            "fuzzy_transpositions":true,
                            "lenient":false,
                            "zero_terms_query":"NONE",
                            "auto_generate_synonyms_phrase_query":true,
                            "boost":"1.0"
                        
                    
                
            ],
            "adjust_pure_negative":true,
            "boost":"1.0"
        
    

filter和must_not属于Filter Context,不会对_score结果产生影响,但是Es提供了constant_score。对于filter可以设置常量分数。

2.3 constant_score查询

es(2)—复杂的多条件查询(bool查询与constant_score查询)

    public static void testConstantScore() throws IOException 
        SearchRequest searchRequest=new SearchRequest();
        //构建dsl语句
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();

        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("title","英国"));
        /**
         * 设置constant_score搜索
         */
        ConstantScoreQueryBuilder constantScoreQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("title", "疫情"));
        /**
         * 放入bool查询的should中
         */
        boolQueryBuilder.should(constantScoreQueryBuilder);
        /**
         * 将bool查询放入到request对象中
         */
        searchSourceBuilder.query(boolQueryBuilder);
        log.info("dsl:"+searchSourceBuilder.toString());
        //填充到Request对象中
        remoteSearch(searchRequest, searchSourceBuilder);
    

    //远程调用
    private static void remoteSearch(SearchRequest searchRequest, SearchSourceBuilder searchSourceBuilder) throws IOException 
        searchRequest.indices("test_demo");
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = EsUtil.getRestHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) 
            SearchHit next = iterator.next();
            log.info("输出分数:" + next.getScore());
            log.info("输出数据:" + next.getSourceAsString());
        
    

转换dsl:


    "query":
        "bool":
            "must_not":[
                
                    "match":
                        "title":
                            "query":"英国",
                            "operator":"OR",
                            "prefix_length":0,
                            "max_expansions":50,
                            "fuzzy_transpositions":true,
                            "lenient":false,
                            "zero_terms_query":"NONE",
                            "auto_generate_synonyms_phrase_query":true,
                            "boost":"1.0"
                        
                    
                
            ],
            "should":[
                
                    "constant_score":
                        "filter":
                            "term":
                                "title":
                                    "value":"疫情",
                                    "boost":"1.0"
                                
                            
                        ,
                        "boost":"1.0"
                    
                
            ],
            "adjust_pure_negative":true,
            "boost":"1.0"
        
    

2.4 matchPhraseQuery搜索

    public static void testMatchPhraseQuery() throws IOException 
        SearchRequest searchRequest=new SearchRequest();
        //构建dsl语句
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();

        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("title", "英国新冠");
        matchPhraseQueryBuilder.slop(0);
        boolQueryBuilder.should(matchPhraseQueryBuilder);

        searchSourceBuilder.query(boolQueryBuilder);
        //填充到Request对象中
        remoteSearch(searchRequest, searchSourceBuilder);
    


    //远程调用
    private static void remoteSearch(SearchRequest searchRequest, SearchSourceBuilder searchSourceBuilder) throws IOException 
        searchRequest.indices("test_demo");
        searchRequest.source(searchSourceBuilder);
        log.info("dsl:"+searchSourceBuilder.toString());
        SearchResponse response = EsUtil.getRestHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) 
            SearchHit next = iterator.next();
            log.info("输出分数:" + next.getScore());
            log.info("输出数据:" + next.getSourceAsString());
        
    

2.5 range范围查询

    public static void testRange() throws IOException 
        SearchRequest searchRequest=new SearchRequest();
        //构建dsl语句
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();

        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("publishTime");
        rangeQueryBuilder.gt("2021-05-14 11:39:27");

        boolQueryBuilder.filter(rangeQueryBuilder);

        searchSourceBuilder.query(boolQueryBuilder);
        //填充到Request对象中
        remoteSearch(searchRequest, searchSourceBuilder);
    


    //远程调用
    private static void remoteSearch(SearchRequest searchRequest, SearchSourceBuilder searchSourceBuilder) throws IOException 
        searchRequest.indices("test_demo");
        searchRequest.source(searchSourceBuilder);
        log.info("dsl:"+searchSourceBuilder.toString());
        SearchResponse response = EsUtil.getRestHighLevelClient().search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();
        Iterator<SearchHit> iterator = hits.iterator();
        while (iterator.hasNext()) 
            SearchHit next = iterator.next();
            log.info("输出分数:" + next.getScore());
            log.info("输出数据:" + next.getSourceAsString());
        
    

转换后的dsl语句


    "query":
        "bool":
            "filter":[
                
                    "range":
                        "publishTime":
                            "from":"2021-05-14 11:39:27",
                            "to":null,
                            "include_lower":false,
                            "include_upper":true,
                            "boost":"1.0"
                        
                    
                
            ],
            "adjust_pure_negative":true,
            "boost":"1.0"
        
    

以上是关于Es7.x使用RestHighLevelClient进行查询操作的主要内容,如果未能解决你的问题,请参考以下文章

Es7.x使用RestHighLevelClient进行增删改和批量操作

es7.x—查询篇

Es7.x使用RestHighLevelClient进行增删改和批量操作

Elasticsearch - Java API 操作 ES7.15.0ES7.x 索引,文档;高级搜索

Es7.x使用RestHighLevelClient的3种分页实现

Elasticsearch - Java API 操作 ES7.16.0+ES8.x 索引,文档;高级搜索