es的date类型字段按照原生格式进行分组聚合

server/2025/1/24 14:43:16/
PUT student2
{
"mappings": {"properties": {"name": {"type": "text","analyzer": "standard"  // 使用标准分析器,适合姓名字段},"birthday": {"type": "date","format": "yyyy||yyyy-MM||yyyy-MM-dd"  // 日期格式,可以根据需求调整},"age": {"type": "integer"  // 年龄字段,使用整型},"gender": {"type": "keyword"  // 性别字段,使用 keyword 类型来避免分词},"email": {"type": "keyword"  // 使用 keyword 类型来存储不需要分词的电子邮件地址},"address": {"type": "text"  // 地址字段,适合使用全文本分词},"registration_date": {"type": "date","format": "yyyy-MM-dd'T'HH:mm:ss"  // 注册日期,包含时间},"is_active": {"type": "boolean"  // 是否激活标记,使用布尔类型}}
}
} 

插入数据

POST student1/_doc/1
{"name": "Alice","birthday": "2000","age": 24,"gender": "female","email": "alice@example.com","address": "123 Maple Street, Springfield","registration_date": "2022-08-01T09:30:00","is_active": true
}POST student1/_doc/2
{"name": "Bob","birthday": "1998-11","age": 26,"gender": "male","email": "bob@example.com","address": "456 Oak Avenue, Shelbyville","registration_date": "2023-01-10T14:00:00","is_active": true
}POST student1/_doc/3
{"name": "Charlie","birthday": "1995-03-25","age": 29,"gender": "male","email": "charlie@example.com","address": "789 Birch Road, Capital City","registration_date": "2021-07-15T11:00:00","is_active": false
}POST student1/_doc/4
{"name": "Diana","birthday": "1999","age": 25,"gender": "female","email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true
}

执行查询

GET student2/_search
{"size": 0,"aggs": {"birthday_by_original_format": {"terms": {"script": {"source": """if (params['_source']['birthday'] != null) {return params['_source']['birthday'].toString();}return null;"""},"size": 10}}}
}

得到结果:

{"took": 25,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"birthday_by_original_format": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "1995","doc_count": 1},{"key": "1998-11","doc_count": 1},{"key": "1999-07-20","doc_count": 1},{"key": "2000-05-15","doc_count": 1}]}}
}

查询语句写为java:

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;
import org.apache.http.HttpHost;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.Script;import java.io.IOException;
import java.util.Collections;
import java.util.List;public class ElasticsearchAggregationExample {public static void main(String[] args) {// 创建 Elasticsearch 客户端RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"))  // 替换为你的 Elasticsearch 地址);try {// 构建查询请求SearchRequest searchRequest = new SearchRequest("student2");  // 替换为你的索引名称SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();// 构建聚合String scriptSource = "if (params['_source']['birthday'] != null) { " +"  return params['_source']['birthday'].toString(); " +"} " +"return null;";TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("birthday_by_original_format").script(new Script(ScriptType.INLINE, "painless", scriptSource, Collections.emptyMap())).size(10);  // 设置返回的桶数量// 将聚合添加到查询中sourceBuilder.aggregation(aggregationBuilder);sourceBuilder.size(0);  // 不返回具体文档,只返回聚合结果searchRequest.source(sourceBuilder);// 执行查询SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);// 解析聚合结果Terms terms = searchResponse.getAggregations().get("birthday_by_original_format");List<? extends Terms.Bucket> buckets = terms.getBuckets();for (Terms.Bucket bucket : buckets) {String key = bucket.getKeyAsString();  // 获取聚合的 key(原始的 birthday 值)long docCount = bucket.getDocCount();  // 获取文档数量System.out.println("Key: " + key + ", Doc Count: " + docCount);}} catch (IOException e) {e.printStackTrace();} finally {// 关闭客户端try {client.close();} catch (IOException e) {e.printStackTrace();}}}
}

终极解决方案:
给mapping的所有date类型字段添加子类型

PUT /student3
{"mappings": {"properties": {"address": {"type": "text"},"age": {"type": "integer"},"birthday": {"type": "date","format": "yyyy||yyyy-MM||yyyy-MM-dd","fields": {                "raw": {                "type": "keyword"}}},"email": {"type": "keyword"},"events": {"type": "nested","properties": {"event_date": {"type": "date","format": "yyyy||yyyy-MM||yyyy-MM-dd","fields": {                "raw": {                "type": "keyword"}}},"event_type": {"type": "keyword"}}},"gender": {"type": "keyword"},"is_active": {"type": "boolean"},"name": {"type": "keyword","analyzer": "standard"},"registration_date": {"type": "date","format": "yyyy-MM-dd'T'HH:mm:ss","fields": {                "raw": {                "type": "keyword"}}}}}
}

插入数据:

POST student1/_doc/1
{"name": "Alice","birthday": "2000","age": 24,"gender": "female","email": "alice@example.com","address": "123 Maple Street, Springfield","registration_date": "2022-08-01T09:30:00","is_active": true,"events": [{"event_date": "2022","event_type": "graduation"},{"event_date": "2023","event_type": "birthday"}]
}POST student1/_doc/2
{"name": "Bob","birthday": "1998-11","age": 26,"gender": "male","email": "bob@example.com","address": "456 Oak Avenue, Shelbyville","registration_date": "2023-01-10T14:00:00","is_active": true,"events": [{"event_date": "2022-06","event_type": "meeting"}]
}POST student1/_doc/3
{"name": "Charlie","birthday": "1995-03-25","age": 29,"gender": "male","email": "charlie@example.com","address": "789 Birch Road, Capital City","registration_date": "2021-07-15T11:00:00","is_active": false,"events": [{"event_date": "2021-10-10","event_type": "conference"},{"event_date": "2022-02-20","event_type": "workshop"},{"event_date": "2023-04-05","event_type": "meeting"}]
}POST student1/_doc/4
{"name": "Diana","birthday": "1999-07-20","age": 25,"gender": "female","email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_date": "2022-11-10","event_type": "webinar"}]
}POST student1/_doc/5
{"name": "jfid","birthday": "1999-07-20","age": 25,"gender": "female","email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_type": "webinar"}]
}POST student1/_doc/6
{"name": "HH","birthday": "1999-07-20","age": 25,"email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_date": "2022-11-10","event_type": "webinar"}]
}POST student1/_doc/6
{"name": "AA","birthday": "1999-07-20","age": 25,"email": "diana@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_type": "webinar"}]
}POST student1/_doc/7
{"name": "BB","birthday": "1999-07-20","age": 25,"gender": "female","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_date": "2022-11-10","event_type": "webinar"}]
}POST student1/_doc/8
{"name": "CC","birthday": "1999-07-20","age": 25,"address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,"events": [{"event_date": "2022-11-10","event_type": "webinar"}]
}POST student1/_doc/8
{"name": "CC","birthday": "1999-07-20","age": 25,"gender": "female","email": "CC@example.com","address": "101 Pine Lane, Townsville","registration_date": "2020-12-05T08:00:00","is_active": true,}

根据嵌套子字段的date类型字段聚合

GET /student3_new/_search
{"size": 0,"aggs": {"nested_events": {"nested": {"path": "events"},"aggs": {"event_date_groups": {"terms": {"field": "events.event_date.raw", "size": 10}}}}}
}

聚合结果:

{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"nested_events": {"doc_count": 7,"event_date_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "2021-10-10","doc_count": 1},{"key": "2022","doc_count": 1},{"key": "2022-02-20","doc_count": 1},{"key": "2022-06","doc_count": 1},{"key": "2022-11-10","doc_count": 1},{"key": "2023","doc_count": 1},{"key": "2023-04-05","doc_count": 1}]}}}
}

五、

现在要对 event_date 进行分组聚合,查询结果要能知道不同的组对应的name是什么,也要知道没有event_date 的时候对应的name是什么

GET student4/_search
{"size": 0,"aggs": {"nested_events": {"nested": {"path": "events"},"aggs": {// 按 event_date.raw(keyword类型)分组"event_date_groups": {"terms": {"field": "events.event_date.raw",  // 使用 keyword 类型字段"missing": "N/A",                  // 处理缺失值"size": 10},"aggs": {"back_to_parent": {"reverse_nested": {},"aggs": {"names": {"terms": {"field": "name","size": 10}}}}}},// 补充:单独统计完全缺失 event_date 的文档(无任何事件包含该字段)"missing_event_date": {"filter": {"bool": {"must_not": {"exists": {"field": "events.event_date"}}}},"aggs": {"back_to_parent": {"reverse_nested": {},"aggs": {"names": {"terms": {"field": "name","size": 10}}}}}}}}}
}

查询结果:

{"took": 6,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 5,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"nested_events": {"doc_count": 8,"event_date_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "2021-10-10","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}},{"key": "2022","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Alice","doc_count": 1}]}}},{"key": "2022-02-20","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}},{"key": "2022-06","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Bob","doc_count": 1}]}}},{"key": "2022-11-10","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Diana","doc_count": 1}]}}},{"key": "2023","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Alice","doc_count": 1}]}}},{"key": "2023-04-05","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}},{"key": "N/A","doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "jfid","doc_count": 1}]}}}]},"missing_event_date": {"doc_count": 1,"back_to_parent": {"doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "jfid","doc_count": 1}]}}}}}
}

六、
现在升级需求,要先对 event_date 进行分组聚合,然后再对 gender 进行分组聚合,查询结果要能知道不同的组对应的name是什么,也要知道没有event_date 或者没有 gender 的时候对应的name是什么

GET student4/_search
{"size": 0,"aggs": {"nested_events": {"nested": {"path": "events"  // 进入嵌套文档上下文},"aggs": {"event_date_groups": {"terms": {"field": "events.event_date.raw",  // 使用 keyword 类型字段避免日期解析错误"missing": "N/A",                  // 处理 event_date 缺失的事件"size": 10},"aggs": {"back_to_parent": {"reverse_nested": {},  // 返回父文档上下文"aggs": {"gender_groups": {"terms": {"field": "gender",          // 按 gender 分组"missing": "missing_gender",// 处理 gender 缺失的文档"size": 10},"aggs": {"names": {"terms": {"field": "name",  // 收集对应的 name"size": 10}}}}}}}}}}}
}

查询结果:

{"took": 9,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"nested_events": {"doc_count": 9,"event_date_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "N/A","doc_count": 2,"back_to_parent": {"doc_count": 2,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "jfid","doc_count": 1}]}},{"key": "missing_gender","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "AA","doc_count": 1}]}}]}}},{"key": "2021-10-10","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}]}}},{"key": "2022","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Alice","doc_count": 1}]}}]}}},{"key": "2022-02-20","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}]}}},{"key": "2022-06","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Bob","doc_count": 1}]}}]}}},{"key": "2022-11-10","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Diana","doc_count": 1}]}}]}}},{"key": "2023","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Alice","doc_count": 1}]}}]}}},{"key": "2023-04-05","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}]}}}]}}}
}

七、
现在升级需求,要先对 event_date 进行分组聚合,然后再对 gender 进行分组聚合,然后再对 email 进行分组聚合 ,查询结果要能知道不同的组对应的name是什么,也要知道没有event_date 或者没有 gender 或者没有 email 的时候对应的name是什么

查询语句 :

GET student4/_search
{"size": 0,"aggs": {"nested_events": {"nested": {"path": "events"},"aggs": {// 第一层聚合:按 event_date.raw 分组"event_date_groups": {"terms": {"field": "events.event_date.raw","missing": "N/A_EVENT_DATE","size": 10},"aggs": {// 返回父文档上下文"back_to_parent": {"reverse_nested": {},"aggs": {// 第二层聚合:按 gender 分组"gender_groups": {"terms": {"field": "gender","missing": "N/A_GENDER","size": 10},"aggs": {// 第三层聚合:按 email 分组"email_groups": {"terms": {"field": "email","missing": "N/A_EMAIL","size": 10},"aggs": {// 收集对应的 name"names": {"terms": {"field": "name","size": 10}}}}}}}}}},// 补充:处理 events 中完全缺失 event_date 的事件"missing_event_date": {"filter": {"bool": {"must_not": {"exists": {"field": "events.event_date"}}}},"aggs": {"back_to_parent": {"reverse_nested": {},"aggs": {"gender_groups": {"terms": {"field": "gender","missing": "N/A_GENDER","size": 10},"aggs": {"email_groups": {"terms": {"field": "email","missing": "N/A_EMAIL","size": 10},"aggs": {"names": {"terms": {"field": "name","size": 10}}}}}}}}}}}},// 修正:使用 filter 聚合替代 missing 聚合,精准匹配无事件记录的文档"docs_without_events": {"filter": {"bool": {"must_not": [{"nested": {"path": "events","query": {"exists": {"field": "events"  // 存在至少一个事件记录}}}}]}},"aggs": {"gender_groups": {"terms": {"field": "gender","missing": "N/A_GENDER","size": 10},"aggs": {"email_groups": {"terms": {"field": "email","missing": "N/A_EMAIL","size": 10},"aggs": {"names": {"terms": {"field": "name","size": 10}}}}}}}}}
}

查询结果:

{"took": 4,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 8,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"nested_events": {"doc_count": 10,"event_date_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "2022-11-10","doc_count": 2,"back_to_parent": {"doc_count": 2,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 2,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "N/A_EMAIL","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "BB","doc_count": 1}]}},{"key": "diana@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Diana","doc_count": 1}]}}]}}]}}},{"key": "N/A_EVENT_DATE","doc_count": 2,"back_to_parent": {"doc_count": 2,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "N/A_GENDER","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "diana@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "AA","doc_count": 1}]}}]}},{"key": "female","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "diana@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "jfid","doc_count": 1}]}}]}}]}}},{"key": "2021-10-10","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "charlie@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}]}}]}}},{"key": "2022","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "alice@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Alice","doc_count": 1}]}}]}}]}}},{"key": "2022-02-20","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "charlie@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}]}}]}}},{"key": "2022-06","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "bob@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Bob","doc_count": 1}]}}]}}]}}},{"key": "2023","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "alice@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Alice","doc_count": 1}]}}]}}]}}},{"key": "2023-04-05","doc_count": 1,"back_to_parent": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "male","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "charlie@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Charlie","doc_count": 1}]}}]}}]}}}]},"missing_event_date": {"doc_count": 2,"back_to_parent": {"doc_count": 2,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "N/A_GENDER","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "diana@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "AA","doc_count": 1}]}}]}},{"key": "female","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "diana@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "jfid","doc_count": 1}]}}]}}]}}}},"docs_without_events": {"doc_count": 1,"gender_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "female","doc_count": 1,"email_groups": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "CC@example.com","doc_count": 1,"names": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "CC","doc_count": 1}]}}]}}]}}}
}

八、现在加大难度,要先对 gender 进行分组聚合,然后再对 event_date 进行分组聚合,,然后再对 email 进行分组聚合 ,查询结果要能知道不同的组对应的name是什么,也要知道没有event_date 或者没有 gender 或者没有 email 的时候对应的name是什么


http://www.ppmy.cn/server/161043.html

相关文章

日常梳理-网络架构

1.F5Nginx F5 Nginx 架构是一种常见的高性能、高可用的网络架构设计&#xff0c;广泛应用于企业级和互联网场景中。 高可用性与负载均衡 F5 是一种高性能的硬件负载均衡器&#xff0c;通常用于四层负载均衡&#xff08;基于 IP 和端口&#xff09;&#xff0c;能够快速分发流…

Flink中的时间和窗口

在批处理统计中&#xff0c;我们可以等待一批数据都到齐后&#xff0c;统一处理。但是在实时处理统计中&#xff0c;我们是来一条就得处理一条&#xff0c;那么我们怎么统计最近一段时间内的数据呢&#xff1f;引入“窗口”。 所谓的“窗口”&#xff0c;一般就是划定的一段时…

jenkins平台使用Login Theme、Customizable Header插件定制修改登陆页图片文字及首页标题

文章目录 前言一、需求二、Login Theme插件1.插件信息2.登陆框上增加文字3.登录页图片的修改 二、Customizable Header插件1.插件信息2.首页标题 总结 前言 使用Login Theme、Customizable Header插件定制修改登陆页logo及首页标题 一、需求 我的使用需求比较简单&#xff0c…

JDK长期支持版本(LTS)

https://blogs.oracle.com/java/post/the-arrival-of-java-23 jdk长期支持版本&#xff08;LTS&#xff09;&#xff1a;JDK 8、11、17、21&#xff1a;

浅谈机器学习之基于RNN进行充值的油费预测

浅谈机器学习之基于RNN进行充值的油费预测 引言 随着智能交通和物联网技术的发展&#xff0c;油费预测已成为研究的热点之一。准确的油费预测不仅能帮助车主合理规划出行成本&#xff0c;还可以为油价波动提供参考依据。近年来&#xff0c;递归神经网络&#xff08;RNN&#…

MyBatis和JPA区别详解

文章目录 MyBatis和JPA区别详解一、引言二、设计理念与使用方式1、MyBatis&#xff1a;半自动化的ORM框架1.1、代码示例 2、JPA&#xff1a;全自动的ORM框架2.1、代码示例 三、性能优化与适用场景1、MyBatis&#xff1a;灵活的SQL控制1.1、适用场景 2、JPA&#xff1a;开发效率…

qml Dialog详解

1、概述 Dialog是QML&#xff08;Qt Modeling Language&#xff09;中用于显示对话框的组件&#xff0c;它提供了一个模态窗口&#xff0c;通常用于与用户进行重要交互&#xff0c;如确认操作、输入信息或显示警告等。Dialog组件具有灵活的布局和样式选项&#xff0c;可以轻松…

STM32项目分享:智能厨房安全检测系统

目录 一、前言 二、项目简介 1.功能详解 2.主要器件 三、原理图设计 四、PCB硬件设计 PCB图 五、程序设计 六、实验效果 七、资料内容 项目分享 一、前言 项目成品图片&#xff1a; 哔哩哔哩视频链接&#xff1a; STM32智能厨房安全检测系统 &#xff08;资料分…