# Do filter in the aggregations with composite

**URL:** <https://discuss.elastic.co/t/do-filter-in-the-aggregations-with-composite/227884>\
**Category:** Elasticsearch\
**Created:** [April 14, 2020, 10:08am UTC](https://discuss.elastic.co/t/do-filter-in-the-aggregations-with-composite/227884 "2020-04-14T10:08:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Han](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_han/32/45326_2.png) [@David\_Han](https://discuss.elastic.co/u/David_Han)\
**Post date:** [April 14, 2020, 10:08am UTC](https://discuss.elastic.co/t/do-filter-in-the-aggregations-with-composite/227884/1 "2020-04-14T10:08:54Z")

</div>

Hello everyone

I want to get the aggregation result with filter the bucket key value. And My query is

```auto
> {
> "query": {
> "bool": {
> "filter": [
> {
> "term": {
> "customer_id": "123456"
> }
> },
> {
> "range": {
> "date": {
> "gte": "2019-04-20",
> "lte": "2020-04-14"
> }
> }
> }
> ]
> }
> },
> "size": 0,
> "aggs": {
> "search_terms": {
> "composite": {
> "size": 10,
> "sources": {
> "search_term": {
> "terms": {
> "field": "search_term_standard"
> }
> }
> }
> },
> "aggs": {
> "clicks": {
> "sum": {
> "field": "clicks",
> "missing": 0
> }
> },
> "conversions_rate": {
> "avg": {
> "field": "conversions_rate",
> "missing": 0
> }
> },
> "cost": {
> "sum": {
> "field": "cost",
> "missing": 0
> }
> },
> "conversions": {
> "sum": {
> "field": "conversions",
> "missing": 0
> }
> },
> "avg_cpc": {
> "avg": {
> "field": "avg_cpc",
> "missing": 0
> }
> },
> "conversions_value": {
> "sum": {
> "field": "conversions_value",
> "missing": 0
> }
> }
> }
> }
> }
> }

```

The mapping is

> "mappings": {
> 
> - "\_doc": {
> - "properties": {
> - "ctr": {
> - "type": "float"},
> 
> - "date": {
> - "type": "date"},
> 
> - "conversions": {
> - "type": "integer"},
> 
> - "cost": {
> - "type": "integer"},
> 
> - "conversions\_value\_cost": {
> - "type": "float"},
> 
> - "search\_term\_bigram": {
> - "fielddata": true,
> - "analyzer": "bigram",
> - "type": "text"},
> 
> - "campaign\_name": {
> - "type": "text",
> - "fields": {
> - "keyword": {
> - "ignore\_above": 256,
> - "type": "keyword"}}},
> 
> - "conversions\_value": {
> - "type": "float"},
> 
> - "@timestamp": {
> - "type": "date"},
> 
> - "cost\_conversions": {
> - "type": "float"},
> 
> - "value\_conversions": {
> - "type": "float"},
> 
> - "search\_term\_id": {
> - "type": "long"},
> 
> - "updated\_at": {
> - "type": "date"},
> 
> - "conversions\_rate": {
> - "type": "float"},
> 
> - "search\_term\_trigram": {
> - "fielddata": true,
> - "analyzer": "trigram",
> - "type": "text"},
> 
> - "search\_term": {
> - "type": "text",
> - "fields": {
> - "keyword": {
> - "ignore\_above": 256,
> - "type": "keyword"}}},
> 
> - "avg\_cpc": {
> - "type": "integer"},
> 
> - "clicks": {
> - "type": "integer"},
> 
> - "impression": {
> - "type": "integer"},
> 
> - "id": {
> - "type": "long"},
> 
> - "customer\_id": {
> - "type": "text",
> - "fields": {
> - "keyword": {
> - "ignore\_above": 256,
> - "type": "keyword"}}},
> 
> - "search\_term\_standard": {
> - "fielddata": true,
> - "analyzer": "standard",
> - "type": "text"},
> 
> - "campaign\_id": {
> - "type": "text",
> - "fields": {
> - "keyword": {
> - "ignore\_above": 256,
> - "type": "keyword"}}}}}

and the result is

> {
> 
> - "took": 1161,
> - "timed\_out": false,
> - "\_shards": {
> - "total": 1,
> - "successful": 1,
> - "skipped": 0,
> - "failed": 0},
> 
> - "hits": {
> - "total": {
> - "value": 10000,
> - "relation": "gte"},
> 
> - "max\_score": null,
> - "hits": },
> 
> - "aggregations": {
> - "search\_terms": {
> - "after\_key": {
> - "search\_term": "0.0"},
> 
> - "buckets": [
> - {
> - "key": {
> - "search\_term": "0"},
> 
> - "doc\_count": 392,
> - "conversions\_value": {
> - "value": 114.4799977555},
> 
> - "conversions": {
> - "value": 367},
> 
> - "cost": {
> - "value": 7640},
> 
> - "conversions\_rate": {
> - "value": 0.8928571428571429},
> 
> - "avg\_cpc": {
> - "value": 188.0612244898},
> 
> - "clicks": {
> - "value": 342}}  
> , \* {
> 
> - "key": {
> - "search\_term": "0.0"},
> 
> - "doc\_count": 1,
> - "conversions\_value": {
> - "value": 0},
> 
> - "conversions": {
> - "value": 0},
> 
> - "cost": {
> - "value": 800},
> 
> - "conversions\_rate": {
> - "value": 0},
> 
> - "avg\_cpc": {
> - "value": 800},
> 
> - "clicks": {
> - "value": 1}}]}}
> 
> }

I want to filter the aggregations buckets only show the search\_term contains "for".  
How can I do it ? Thank you.

---

<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:** [May 12, 2020, 10:08am UTC](https://discuss.elastic.co/t/do-filter-in-the-aggregations-with-composite/227884/2 "2020-05-12T10:08:57Z")

</div>

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