已客戶customer為例
我想查詢每日的客戶數(shù)币旧。
先按照日期分桶秕岛,然后在桶內(nèi)按照 姓名來去重 來計(jì)算客戶數(shù)(實(shí)際會(huì)按照客戶id 來區(qū)分客戶)
測(cè)試數(shù)據(jù)見 文章末尾
一共是9條數(shù)據(jù)帘睦, 名字分別為:
river Lucy 1 Lucy frank tom lily lily tom tom
不同的名字是 6 個(gè)疾党。
先看看 es 的 query 怎么寫
GET /es-customer/_search
{
"size" : 0,
"aggs" : {
"days" : {
"date_histogram": {
"field": "createTime",
"interval": "day"
},
"aggs": {
"distinct_name" : {
"cardinality" : {
"field" : "firstName"
}
}
}
}
}
}
查詢結(jié)果為:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 0,
"hits": []
},
"aggregations": {
"days": {
"buckets": [
{
"key_as_string": "2019-04-10 00:00:00",
"key": 1554854400000,
"doc_count": 9,
"distinct_name": {
"value": 6
}
}
]
}
}
}
2019-04-10 當(dāng)天查出了9條數(shù)據(jù)档泽,去重后是6條俊戳。
現(xiàn)在就可以根據(jù) 查詢寫java代碼了
@Test
public void testAggAndDistinct(){
//獲取注解,通過注解可以得到 indexName 和 type
Document document = Customer.class.getAnnotation(Document.class);
// dateHistogram Aggregation 是時(shí)間柱狀圖聚合馆匿,按照天來聚合 抑胎, dataAgg 為聚合結(jié)果的名稱,createTime 為字段名稱
// cardinality 用來去重
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(SearchType.QUERY_THEN_FETCH)
.withIndices(document.indexName()).withTypes(document.type())
.addAggregation(AggregationBuilders.dateHistogram("dataAgg").field("createTime").dateHistogramInterval(DateHistogramInterval.DAY)
.subAggregation(AggregationBuilders.cardinality("nameAgg").field("firstName")))
.build();
// 聚合的結(jié)果
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, response -> response.getAggregations());
Map<String, Aggregation> results = aggregations.asMap();
Histogram histogram = (Histogram) results.get("dataAgg");
// 將bucket list 轉(zhuǎn)換成 map 渐北, key -> 名字 value-> 出現(xiàn)次數(shù)
histogram.getBuckets().stream().forEach(t->{
Histogram.Bucket histogram1 = t;
System.out.println(histogram1.getKeyAsString());
Cardinality cardinality = histogram1.getAggregations().get("nameAgg");
System.out.println(cardinality.getValue());
});
}
打印結(jié)果為
時(shí)間:2019-04-10 00:00:00
總數(shù) :9
去重后數(shù)量:6
這是我們期望的結(jié)果阿逃。
測(cè)試數(shù)據(jù)
GET /es-customer/_search
{
"query": {
"match_all": {}
}
}
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 1,
"hits": [
{
"_index": "es-customer",
"_type": "customer",
"_id": "_z8_BmoB7Iqmj8bUCgie",
"_score": 1,
"_source": {
"id": null,
"firstName": "Lucy 1",
"lastName": "001",
"valid": null,
"age": 13,
"createTime": "2019-04-10 07:55:55"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "Aj8_BmoB7Iqmj8bUCwkY",
"_score": 1,
"_source": {
"id": null,
"firstName": "tom",
"lastName": "001",
"valid": null,
"age": 44,
"createTime": "2019-04-10 07:55:56"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "Az8_BmoB7Iqmj8bUCwk6",
"_score": 1,
"_source": {
"id": null,
"firstName": "lily",
"lastName": "002",
"valid": null,
"age": 56,
"createTime": "2019-04-10 07:55:56"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "BD8_BmoB7Iqmj8bUCwlc",
"_score": 1,
"_source": {
"id": null,
"firstName": "lily",
"lastName": "004",
"valid": null,
"age": 53,
"createTime": "2019-04-10 07:55:56"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "_j8_BmoB7Iqmj8bUCghV",
"_score": 1,
"_source": {
"id": null,
"firstName": "river",
"lastName": "007",
"valid": null,
"age": 12,
"createTime": "2019-04-10 07:55:55"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "AD8_BmoB7Iqmj8bUCgnH",
"_score": 1,
"_source": {
"id": null,
"firstName": "Lucy",
"lastName": "002",
"valid": null,
"age": 22,
"createTime": "2019-04-10 07:55:55"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "AT8_BmoB7Iqmj8bUCgnv",
"_score": 1,
"_source": {
"id": null,
"firstName": "frank",
"lastName": "001",
"valid": null,
"age": 33,
"createTime": "2019-04-10 07:55:56"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "BT8_BmoB7Iqmj8bUCwmC",
"_score": 1,
"_source": {
"id": null,
"firstName": "tom",
"lastName": "002",
"valid": null,
"age": 66,
"createTime": "2019-04-10 07:55:56"
}
},
{
"_index": "es-customer",
"_type": "customer",
"_id": "Bj8_BmoB7Iqmj8bUCwmp",
"_score": 1,
"_source": {
"id": null,
"firstName": "tom",
"lastName": "005",
"valid": null,
"age": 33,
"createTime": "2019-04-10 07:55:56"
}
}
]
}
}