• 关于elasticsearch聚合查询只有10条数据(java)


    业务:查询热点词汇

    实现:使用聚合函数,统计前100条高频词汇

    解决聚合只显示10条问题:

    在构建termsBuilder聚合查询对构建器时,在最后设置size值为100,完美解决 
     public ESReturn searchHotPoint(Integer day, Integer num) {
            ESReturn esReturn = new ESReturn();
            List<BucketsReturnVO> bucketsReturnVOList = new ArrayList<>();
            Client client = clientHelper.getClient();
            //分组聚合
            AggregationBuilder termsBuilder = AggregationBuilders.terms("by_searchKeyword").field("searchKeyword").size(100);
            AggregationBuilder countBuilder = AggregationBuilders.count("searchKeywordCount").field("searchKeyword");
            termsBuilder.subAggregation(countBuilder);
            SearchRequestBuilder sb = client.prepareSearch("search_history").setTypes("user_search_history");
            SearchResponse sr = sb.setQuery(QueryESUtil.getQueryBuildByDay(day))
                    .addAggregation(termsBuilder)
                    .get();
            SearchHits hits = sr.getHits();
            SearchHit[] lists = hits.getHits();
            logger.info("总命中数:" + hits.totalHits);
            logger.info("总长度:" + lists.length);
            Aggregation bySearchKeyword = sr.getAggregations().asMap().get("by_searchKeyword");
            //转成BySearchKeywordVO对象
            BySearchKeywordVO keywordVO = GsonUtil.GsonToBean(bySearchKeyword.toString(), BySearchKeywordVO.class);
            List<Buckets> buckets = keywordVO.getBy_searchKeyword().getBuckets();
            //整理返回数据
            for (Buckets bucket : buckets) {
                BucketsReturnVO bucketsReturnVO = new BucketsReturnVO();
                bucketsReturnVO.setKeyword(bucket.getKey());
                bucketsReturnVO.setDocCount(bucket.getDoc_count());
                bucketsReturnVOList.add(bucketsReturnVO);
            }
            esReturn.setTotal(hits.totalHits);
            esReturn.setData(bucketsReturnVOList);
            return esReturn;
        }
  • 相关阅读:
    (原创)C++ IOC框架
    【教训】为什么不作备份?!
    【教训】php pcntl_fork无法在web服务器环境下使用
    PHP多进程处理并行处理任务实例
    mysql数据库授权
    PHPUnit学习03使用Mock对象解决测试依赖
    [Inside] How to solve one problem?
    [Inside] What’s the assumptions you are making
    算法面试题解答(六)
    [Inside] System Thinking
  • 原文地址:https://www.cnblogs.com/mlzdev/p/11832798.html
Copyright © 2020-2023  润新知