How to add field that is only present in some documents?

IMO there are 2 more features that you should take into account when designing your solution:

  1. specify person in the group_by clause and set missing_bucket to true so that the buckets without a person field are also returned

You can read more about missing_bucket in the docs: Composite aggregation | Elasticsearch Guide [8.11] | Elastic

  1. specify person in the aggregations clause but use top_metrics aggregation instead of terms.

You can read more about top_metrics in this blog:

2 Likes