elasticsearch 查询API

elasticsearch 查询API

Transport Client

Client的连接方式可以参考我的前两篇文章。
 
本次操作以TransportClient的Java API 查询操作。
 
    public void testTransportClient() {
        Map<String, Object> params = new HashMap<>();
        String endtmStart = "2019-03-07 00:00";
        String endtmEnd = "2019-03-09 23:59";
        String removezonecode = "852RA";
        String[] array = new String[]{"a","b"};


        //查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders
                .boolQuery()
                //字段匹配
                .must(QueryBuilders.termQuery("removezonecode", removezonecode))
                //匹配多个值
                .must(QueryBuilders.termsQuery("removezonecode", array))
                // 范围
                .must(new RangeQueryBuilder("endtm").gte(endtmStart).lte(endtmEnd));
        boolQueryBuilder.must(new RangeQueryBuilder("endtm").gte(endtmStart).lte(endtmEnd));

        TransportClient client = TransportClientFactory.getInstance().getTransportClient();

        SearchSourceBuilder ssb = new SearchSourceBuilder();

        SearchResponse searchResponse = client
                .prepareSearch(esIndexName).setSearchType(SearchType.DEFAULT)
                .setSize(5).setQuery(boolQueryBuilder).execute()
                .actionGet();
//        System.out.println(searchResponse.getHits().totalHits);
        for (SearchHit searchHit : searchResponse.getHits()) {
            String jsonStr = searchHit.getSourceAsString();
            System.out.println(jsonStr);
        }
    }
 
 
 
 
 

Rest Client

本次操作以RestClient的Java API 查询操作
 
public void testRestHighClient() {
    String endtmStart = "2019-03-07 00:00";
    String endtmEnd = "2019-03-09 23:59";
    String removezonecode = "852RA";
    String[] array = new String[]{"a","b"};

    //条件查找
    BoolQueryBuilder boolQueryBuilder = QueryBuilders
            .boolQuery()
            //字段匹配
            .must(QueryBuilders.termQuery("removezonecode", removezonecode))
            //匹配多个值
            .must(QueryBuilders.termsQuery("removezonecode", array))
            // 范围
            .must(new RangeQueryBuilder("endtm").gte(endtmStart).lte(endtmEnd));


    //聚合
    AggregationBuilder tb1 = AggregationBuilders.terms("src_dist_code_list").field("src_dist_code").size(10000);

    TermsAggregationBuilder tb2 = AggregationBuilders.terms("dest_area_code_list").field("dest_area_code").size(10000);
    TermsAggregationBuilder tb3 = AggregationBuilders.terms("src_city_code_list").field("src_city_code").size(10000);

    SumAggregationBuilder tbSum = AggregationBuilders.sum("meterage_weight_qty_sum").field("meterage_weight_qty");


    tb3.subAggregation(tbSum);
    tb2.subAggregation(tb3);
    tb1.subAggregation(tb2);




    SearchSourceBuilder ssb = new SearchSourceBuilder();
    ssb.query(boolQueryBuilder);
    ssb.aggregation(tb1);

    //查询请求
    SearchRequest sr = new SearchRequest();
    sr.indices(esIndexName);
    sr.source(ssb);
    

    System.out.println(sr.source());

    RestHighLevelClient client = RestHighLevelClientFactory.getInstance().getHighLevelClient();
    try {
        SearchResponse response = client.search(sr);
        System.out.println(response.getHits().getTotalHits());

        for (SearchHit searchHit : response.getHits().getHits()) {
            String jsonStr = searchHit.getSourceAsString();
            System.out.println(jsonStr);
        }

        RestHighLevelClientFactory.getInstance().close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
 
 
【深度分页问题】
在使用查询的时候会设置size,但是由于ES本身的限制,如果查询超过10000条数据,可能会查不出来。报 Result window is too large 的错误。
解决:
1.增大max_result_window的值,最大值受服务器配置的限制
2.使用scroll分页
 
解决参考:
https://blog.csdn.net/lisongjia123/article/details/79041402
 
 
   
   
   
   
 
 
 
 
欢迎关注我公众号:

发表回复

您的电子邮箱地址不会被公开。

粤ICP备17041560号-2