ES-针对某个字段去重后-获取某个字段值的所有值

针对上面表的数据,现在想根据age分组,并获取每个分组后的name有哪些(去重后)。

select age, GROUP_CONCAT(DISTINCT(name)) from testtable group by age ;

结果:

如果想要增加排序:

SELECT 
    age, 
    GROUP_CONCAT(DISTINCT name) AS names,
    COUNT(DISTINCT name) AS name_count
FROM 
    testtable 
GROUP BY 
    age
ORDER BY 
    name_count DESC;

结果:

使用ES实现类似上面的SQL效果:

SearchRequest searchRequest = new SearchRequest("indexName");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
       
        //时间范围
         RangeQueryBuilder pubtimeBuilder = QueryBuilders.rangeQuery("pubtime").gte(stime).lte(etime);
            boolQueryBuilder.must(pubtimeBuilder);
     
        searchSourceBuilder.query(boolQueryBuilder);

        // 添加 cardinality 聚合 
           AggregationBuilder  cardinalityAggregationBuilder = AggregationBuilders
                    .cardinality("unique_organ_id")
                    .field("organ_id");

              // 添加 terms 聚合以获取去重后的具体数据
           AggregationBuilder  termsAggregationBuilder = AggregationBuilders
                    .terms("distinct_ids")
                    .field("organ_id")
                    .size(10000);//根据实际的确定

        searchSourceBuilder.aggregation(cardinalityAggregationBuilder);
        searchSourceBuilder.aggregation(termsAggregationBuilder);
        searchRequest.source(searchSourceBuilder);

        try {
            SearchResponse searchResponse = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);
            ParsedCardinality uniqueMcnOrganIdAgg = searchResponse.getAggregations().get("unique_organ_id");
            total = uniqueMcnOrganIdAgg.getValue();
            if(total > 0){
                Terms terms = searchResponse.getAggregations().get("distinct_ids");
                for (Terms.Bucket bucket : terms.getBuckets()) {
                    String mcnOrganId = bucket.getKeyAsString();
                    ids.add(mcnOrganId);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Result result = new Result();
        result.setIds(ids);
        result.setTotal(total);
        return result;
相关推荐
sdaxue.com3 分钟前
帝国CMS:如何去掉帝国CMS登录界面的认证码登录
数据库·github·网站·帝国cms·认证码
o(╥﹏╥)1 小时前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
阿里嘎多学长1 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
Yuan_o_1 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端
Sunyanhui11 小时前
牛客网 SQL36查找后排序
数据库·sql·mysql
老王笔记1 小时前
MHA binlog server
数据库·mysql
lovelin+v175030409662 小时前
安全性升级:API接口在零信任架构下的安全防护策略
大数据·数据库·人工智能·爬虫·数据分析
DT辰白3 小时前
基于Redis的网关鉴权方案与性能优化
数据库·redis·缓存
2401_871213303 小时前
mysql高阶语句
数据库·mysql