当前位置 博文首页 > 文章内容

    java 通过聚合查询实现elasticsearch的group by后的数量

    作者:shunshunshun18 栏目:未分类 时间:2021-12-28 15:24:01

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    通过聚合查询获取group by 后的数量

    /**
         * 获取key的个数
         *
         * @param key   要group by的字段名
         * @param index 索引名称
         * @return id的个数
         */
        public static int getKeyCount(String key, String index) {
            int count = 0;
            TransportClient client = null;
            try {
                client = connectionPool.getConnection();
                if (client == null) {
                    throw new Exception("没有获取到连接!");
                }
                SearchRequestBuilder search = client.prepareSearch(index);
                //cardinality聚合查询,相当于groupby字段名
                SearchResponse sr = search.addAggregation(AggregationBuilders.cardinality(key + "_count").field(key)).execute().actionGet();
                //从返回数据提取id总数
                Cardinality result = sr.getAggregations().get(key + "_count");
                long value = result.getValue();
                count = (int) value;
            } catch (InterruptedException e) {
            } catch (Exception e) {
                logger.error("getKeyCount错误", e);
            } finally {
                connectionPool.releaseConnection(client);
            }
            return count;
        }

    获取group by后的所有key值

    /**
         * 获取所有key
         *
         * @param key   被group by的字段名
         * @param index 索引名称
         * @return 所有id
         */
        public static List<String> getAllKey(String key, String index) {
            int keyCount = getKeyCount(key, index);
            List<String> strings = new ArrayList<>();
            TransportClient client = null;
            try {
                client = connectionPool.getConnection();
                if (client == null) {
                    throw new Exception("没有获取到数据库连接!");
                }
                SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
                //使用聚合,实现去重查询
                SearchResponse searchResponse = searchRequestBuilder.
                        addAggregation(AggregationBuilders.terms("models").field(key).size(keyCount)).execute().actionGet();
                Terms term = searchResponse.getAggregations().get("models");
                List<? extends Terms.Bucket> buckets = term.getBuckets();
                //遍历结果,提取出id
                for (Terms.Bucket bucket : buckets) {
                    String keyAsString = bucket.getKeyAsString();
                    strings.add(keyAsString);
                }
                buckets.clear();
            } catch (InterruptedException e) {
    
            } catch (Exception e) {
                logger.error("getAllKey错误", e);
            } finally {
                connectionPool.releaseConnection(client);
            }
    
            return strings;
        }