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

发布于:2025-02-10 ⋅ 阅读:(116) ⋅ 点赞:(0)
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/9
{
  "name": "DD",
  "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,
   
}

POST student1/_doc/10
{
            "name": "EE",
            "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
        }

根据嵌套子字段的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是什么

查询语句:

GET student4/_search
{
  "size": 0,
  "aggs": {
    "gender_groups": {
      "terms": {
        "field": "gender",
        "missing": "N/A_GENDER",
        "size": 10
      },
      "aggs": {
        "nested_events": {
          "nested": {
            "path": "events"
          },
          "aggs": {
            "event_date_groups": {
              "terms": {
                "field": "events.event_date.raw",
                "missing": "N/A_EVENT_DATE",
                "size": 10
              },
              "aggs": {
                "back_to_parent": {
                  "reverse_nested": {},
                  "aggs": {
                    "email_groups": {
                      "terms": {
                        "field": "email",
                        "missing": "N/A_EMAIL",
                        "size": 10
                      },
                      "aggs": {
                        "names": {
                          "terms": {
                            "field": "name",
                            "size": 10
                          }
                        }
                      }
                    }
                  }
                }
              }
            },
            "missing_event_date": {
              "filter": {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "events.event_date"
                    }
                  }
                }
              },
              "aggs": {
                "back_to_parent": {
                  "reverse_nested": {},
                  "aggs": {
                    "email_groups": {
                      "terms": {
                        "field": "email",
                        "missing": "N/A_EMAIL",
                        "size": 10
                      },
                      "aggs": {
                        "names": {
                          "terms": {
                            "field": "name",
                            "size": 10
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "docs_without_events": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "nested": {
                    "path": "events",
                    "query": {
                      "exists": {
                        "field": "events"
                      }
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "email_groups": {
              "terms": {
                "field": "email",
                "missing": "N/A_EMAIL",
                "size": 10
              },
              "aggs": {
                "names": {
                  "terms": {
                    "field": "name",
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

查询结果

{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 9,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "gender_groups": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "female",
          "doc_count": 5,
          "nested_events": {
            "doc_count": 5,
            "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,
                    "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": "2022",
                  "doc_count": 1,
                  "back_to_parent": {
                    "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",
                  "doc_count": 1,
                  "back_to_parent": {
                    "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": "N/A_EVENT_DATE",
                  "doc_count": 1,
                  "back_to_parent": {
                    "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
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            },
            "missing_event_date": {
              "doc_count": 1,
              "back_to_parent": {
                "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,
            "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
                      }
                    ]
                  }
                }
              ]
            }
          }
        },
        {
          "key": "N/A_GENDER",
          "doc_count": 2,
          "nested_events": {
            "doc_count": 1,
            "event_date_groups": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "N/A_EVENT_DATE",
                  "doc_count": 1,
                  "back_to_parent": {
                    "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
                              }
                            ]
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            },
            "missing_event_date": {
              "doc_count": 1,
              "back_to_parent": {
                "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
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            }
          },
          "docs_without_events": {
            "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": "EE",
                        "doc_count": 1
                      }
                    ]
                  }
                }
              ]
            }
          }
        },
        {
          "key": "male",
          "doc_count": 2,
          "nested_events": {
            "doc_count": 4,
            "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,
                    "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-02-20",
                  "doc_count": 1,
                  "back_to_parent": {
                    "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,
                    "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-04-05",
                  "doc_count": 1,
                  "back_to_parent": {
                    "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": 0,
              "back_to_parent": {
                "doc_count": 0,
                "email_groups": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": []
                }
              }
            }
          },
          "docs_without_events": {
            "doc_count": 0,
            "email_groups": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": []
            }
          }
        }
      ]
    }
  }
}

网站公告

今日签到

点亮在社区的每一天
去签到