# How can I include specific facet values in an aggregation?

**URL:** <https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819>\
**Category:** Elasticsearch\
**Created:** [December 29, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819 "2020-12-29T18:36:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Catalin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catalin/32/65314_2.png) [@Catalin](https://discuss.elastic.co/u/Catalin)\
**Post date:** [December 29, 2020, 6:36pm UTC](https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819/1 "2020-12-29T18:36:18Z")

</div>

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:

```auto
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 ?

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [December 29, 2020, 10:26pm UTC](https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819/2 "2020-12-29T22:26:09Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_4), or you could use a Terms agg + Filters agg to get the Top 5 values + specific values.

---

<div class="post-metadata">

**Author:** ![Catalin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/catalin/32/65314_2.png) [@Catalin](https://discuss.elastic.co/u/Catalin)\
**Post date:** [December 30, 2020, 3:08pm UTC](https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819/3 "2020-12-30T15:08:58Z")

</div>

- 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.

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [December 30, 2020, 3:37pm UTC](https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819/4 "2020-12-30T15:37:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 27, 2021, 3:37pm UTC](https://discuss.elastic.co/t/how-can-i-include-specific-facet-values-in-an-aggregation/259819/5 "2021-01-27T15:37:24Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
