• java 通过聚合查询实现elasticsearch的group by


    通过聚合查询获取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;
        }
  • 相关阅读:
    jQuery 参考手册
    jQuery 参考手册
    ASP.NET框架例举
    判断checkbox是否选中
    ajax动态添加的li不能绑定click事件
    缓存代码备用
    关于ajax伪实时动态下拉显示最新数据
    JavaScript中,格式化DateTime
    jquery判断id是否存在
    SSI-Server Side Inclued
  • 原文地址:https://www.cnblogs.com/asksk/p/15480933.html
Copyright © 2020-2023  润新知