天天看點

Elasticsearch 聚合查詢每年次數

因為記錄時沒有記錄年份,然後隻能從記錄時間裡取出“年份”來進行聚合統計。

查詢結果如下(特意給了兩個記錄在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
			}
		}
	}
}