# How to combine similar items in aggregations

**URL:** <https://discuss.elastic.co/t/how-to-combine-similar-items-in-aggregations/181234>\
**Category:** Elasticsearch\
**Created:** [May 15, 2019, 3:54pm UTC](https://discuss.elastic.co/t/how-to-combine-similar-items-in-aggregations/181234 "2019-05-15T15:54:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andres\_512](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andres_512/32/39218_2.png) [@andres\_512](https://discuss.elastic.co/u/andres_512)\
**Post date:** [May 15, 2019, 3:54pm UTC](https://discuss.elastic.co/t/how-to-combine-similar-items-in-aggregations/181234/1 "2019-05-15T15:54:58Z")

</div>

I'm trying to create a color filter. The colors being returned are mostly normal but there's a bunch of other off ones. Example, my results show up like this:

```auto
{ key: "WHITE", doc_count: 300 }, 
{ key: "OFFWHITE", doc_count: 2 }, 
{ key: "SUPER WHITE", doc_count: 1 }

```

My aggregation looks like:

```auto

aggs: {
    Color: {
        terms: {
            field: 'colors.keyword',
            size: 100,
        },
    },
}

```

I want to combine anything that includes 'white' together into one bucket. Same with the other top colors. Is this possible and how?

---

<div class="post-metadata">

**Author:** ![whatgeorgemade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whatgeorgemade/32/103246_2.png) [@whatgeorgemade](https://discuss.elastic.co/u/whatgeorgemade)\
**Post date:** [May 15, 2019, 9:55pm UTC](https://discuss.elastic.co/t/how-to-combine-similar-items-in-aggregations/181234/2 "2019-05-15T21:55:36Z")

</div>

I don't think you'll be able to do this with a regular aggregation.

You could deal with the 'special' colours by using a [filters aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html), matching and gathering all variations of `white` into the same bucket but the resulting `bool` query could get hairy. You'd also have to deal with the regular colours in a normal `terms` aggregation.

Another option is to have an ingest pipeline. The pipeline could set a new 'canonical' colour field that's used in the aggregation. A [script processor](https://www.elastic.co/guide/en/elasticsearch/reference/current/script-processor.html) in the pipeline would copy the value of the `Color` field into the `CanonicalColor` field, changing it to `white` if the value of `Color` contains `white`. You'd still have the original value in the `Color` field if you need it.

I'm sorry this isn't an easy answer. If someone else has a more straight-forward way of solving this, I'd be interested in hearing it.

---

<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:** [June 12, 2019, 9:55pm UTC](https://discuss.elastic.co/t/how-to-combine-similar-items-in-aggregations/181234/3 "2019-06-12T21:55:38Z")

</div>

Closed on June 12, 2019, 9:55pm UTC

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