因为记录时没有记录年份,然后只能从记录时间里取出“年份”来进行聚合统计。
查询结果如下(特意给了两个记录在hits里面):
{
"took": 2016,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 9.875289,
"hits": [{
"_index": "download_log_index",
"_type": "_doc",
"_id": "5fqhhXsBJlv9oXn4saQJ",
"_score": 9.875289,
"_source": {
"lngid": "666112663",
"operateid": "206",
"pub_year": "2015",
"downdate": "2016-04-12 02:06:25",
"clc_no": ["TM621.2"],
"gch5": "71976",
"userid": "35865"
}
}, {
"_index": "download_log_index",
"_type": "_doc",
"_id": "__qhhXsBJlv9oXn4saQJ",
"_score": 9.875289,
"_source": {
"lngid": "666112677",
"operateid": "206",
"pub_year": "2015",
"downdate": "2017-04-12 02:06:34",
"clc_no": ["TU311.3"],
"gch5": "71976",
"userid": "35865"
}
}]
},
"aggregations": {
"date_histogram#download": {
"buckets": [{
"key_as_string": "2020",
"key": 1577836800000,
"doc_count": 37
}, {
"key_as_string": "2019",
"key": 1546300800000,
"doc_count": 57
}, {
"key_as_string": "2018",
"key": 1514764800000,
"doc_count": 65
}, {
"key_as_string": "2017",
"key": 1483228800000,
"doc_count": 80
}, {
"key_as_string": "2016",
"key": 1451606400000,
"doc_count": 15411
}]
}
}
}
下面可以看看我的代码:
RestHighLevelClient client = EsXParkUtil.getElasticClinet();
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("download_log_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(0);
searchSourceBuilder.size(5);
MatchQueryBuilder mqb = QueryBuilders.matchQuery("gch5", "71976");
searchSourceBuilder.query(mqb);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 按日期进行分组,然后指定排序规则
DateHistogramAggregationBuilder dateHis = AggregationBuilders.dateHistogram("download").field("downdate")
.calendarInterval(DateHistogramInterval.YEAR).format("yyyy").minDocCount(1)
.order(BucketOrder.key(false));
searchSourceBuilder.aggregation(dateHis);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Histogram agg = searchResponse.getAggregations().get("download");
for (Histogram.Bucket entry : agg .getBuckets()) {
entry.getDocCount();
entry.getKeyAsString();
//进行我们需要的操作
}
查询的条件“searchSourceBuilder”等于:
{
"from": 0,
"size": 5,
"timeout": "60s",
"query": {
"match": {
"gch5": {
"query": "71976",
"operator": "OR",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1.0
}
}
},
"aggregations": {
"download": {
"date_histogram": {
"field": "downdate",
"format": "yyyy",
"calendar_interval": "1y",
"offset": 0,
"order": {
"_key": "desc"
},
"keyed": false,
"min_doc_count": 1
}
}
}
}
|