test

博客园 首页 新随笔 联系 订阅 管理
    public Long getEventGroupDivceCont(LogRequest req) {

        String indexName;
        if (req.getAppId() == null) {
            indexName = indexNameGenerator.structuredLogPrefixWithoutAppId() + "*";
        } else {
            indexName = indexNameGenerator.structuredLogPrefix() + "*";
        }
        // 创建搜索对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        setQuery(boolQueryBuilder, req);

        SearchRequest searchRequest = new SearchRequest(indexName);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.size(0);


        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("deviceCode1").field("deviceCode.keyword").size(100000);
//        TermsAggregationBuilder dataSourceAgg = AggregationBuilders.terms("dataSourceId").field("dataSourceId");
//        TermsAggregationBuilder systemIdAgg = AggregationBuilders.terms("systemId").field("systemId");
//        dataSourceAgg.subAggregation(systemIdAgg);
//        aggregationBuilder.subAggregation(dataSourceAgg);

        searchSourceBuilder.aggregation(aggregationBuilder);
        searchRequest.source(searchSourceBuilder);

        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            // 处理响应结果
            SearchHits searchHits = searchResponse.getHits();
            // 总条数
            return searchHits.getTotalHits().value;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0L;
    }

 

posted on 2023-11-01 19:37  testgogogo  阅读(125)  评论(0编辑  收藏  举报