是时候用 ES 拯救发际线啦

分组之后聚合,获取查询结果无法取出doc_count值 TermsAggregationBuilder FilterAggregationBuilder

Elasticsearch | 作者 rongdayouxin | 发布于2018年09月28日 | 阅读数:6296

系统提示:这个人太懒了,什么问题描述都没有写!

已邀请:

rongdayouxin

赞同来自:

BoolQueryBuilder _BoolQueryBuilder = QueryBuilders.boolQuery();
// RangeQueryBuilder dateTime = QueryBuilders.rangeQuery("smsmtsubmittime");
// dateTime.lte("1538017416280").gte("1538028316280").format("epoch_millis");
// _BoolQueryBuilder.must(dateTime);
QueryStringQueryBuilder _qQueryBuilder1 = QueryBuilders.queryStringQuery("status:0");
QueryStringQueryBuilder _qQueryBuilder2 = QueryBuilders.queryStringQuery("-status:0");
// QueryStringQueryBuilder _qQueryBuilder3 = QueryBuilders.queryStringQuery("0").field("status");
// QueryStringQueryBuilder _qQueryBuilder4 = QueryBuilders.queryStringQuery("0").field("status");
// QueryStringQueryBuilder _qQueryBuilder5 = QueryBuilders.queryStringQuery("0").field("status");
FilterAggregationBuilder _FilterAggregationBuilder1 = AggregationBuilders
.filter("sendSuccessCount", _qQueryBuilder1);
FilterAggregationBuilder _FilterAggregationBuilder2 = AggregationBuilders
.filter("sendFailCount", _qQueryBuilder2);
TermsAggregationBuilder aggregations = AggregationBuilders.terms("cpuserid").field("smsmtuserid").size(100);
aggregations.subAggregation(_FilterAggregationBuilder1).subAggregation(_FilterAggregationBuilder2);
// System.out.println(aggregations.toString());
Map<String, Object> _Map = new HashMap<String, Object>();
SearchResponse scrollResp = this.client.prepareSearch("home_platform_background")
.setTypes("sendallinfo").addSort("id", SortOrder.ASC).setFrom(0).setSize(5).
setQuery(QueryBuilders.matchAllQuery()).addAggregation(aggregations).get();
if (!scrollResp.isTimedOut()) {
Aggregations _Aggregations=scrollResp.getAggregations();
for (Aggregation aggregation : _Aggregations) {
// _log.info("查询的数据=" aggregation.toString());
System.out.println(aggregation.getName());
}
Terms _Terms =_Aggregations.get("cpuserid");
List<? extends Bucket> _BucketS = _Terms.getBuckets();
for (Terms.Bucket bucket : _BucketS) {
_Map.put(bucket.getKey() "", bucket.getDocCount());
_log.info("查询到的数据:" bucket.getKeyAsString() "   " bucket.getDocCount() "");
InternalAggregations _AggregationTemp =(InternalAggregations) bucket.getAggregations();
_AggregationTemp.asList();
for (Aggregation aggregation : _AggregationTemp) {
System.out.println(aggregation.getName());
System.out.println(aggregation.toString());
}
}
} else {
_log.info(scrollResp.status().getStatus() "");
}
 
aggregation 拿到这个值后,不能取出docCount的值

rongdayouxin

赞同来自:

终于试出来了

for (Aggregation aggregation : _AggregationTemp) {
InternalFilter_InternalFilter =(InternalFilter) aggregation;
_log.info("过滤分组"+_InternalFilter.getName()+"="+_InternalFilter.getDocCount()+"");
}
将aggregation强制转化成InternalFilter即可

要回复问题请先登录注册