How can I include specific facet values in an aggregation?

I have the following field and values for my documents:

publication_year : 2000, 2001, 2002, ..., 2020

I'd like to aggregate by publication_year , return the top 5 buckets, but also add specific buckets to the result (selected by the user), even if they are not among the top 5 buckets.

Desired result:

2001 (100)
2002 (88)
2003 (5) - selected
2007 (77)
2014 (15)

Basically, how can I include one or more particular facet values among the returned buckets ?

If you want to do this in a single aggregation, you would increase the size of the agg to include all possible unique values. Alternatively, you could use a Terms agg with size 5 + a Terms agg with the include parameter, or you could use a Terms agg + Filters agg to get the Top 5 values + specific values.

  • Terms agg + Terms agg with the include parameter
  • Terms agg + Filters agg

I presume these 2 are equally fast in terms of performance, so there wouldn't be a preferred one from this perspective.

I don't have a lot more insight into the performance than you do, as I'm primarily a Kibana developer. The biggest performance factor is the number of shards that will be included in the request, and I think both of these queries need to collect information from all the shards.