# Terms aggregation for emails list

**URL:** <https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222>\
**Category:** Elasticsearch\
**Created:** [October 18, 2019, 12:14pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222 "2019-10-18T12:14:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![isamzwuairi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isamzwuairi/32/56163_2.png) [@isamzwuairi](https://discuss.elastic.co/u/isamzwuairi)\
**Post date:** [October 18, 2019, 12:14pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/1 "2019-10-18T12:14:51Z")

</div>

Hello all,

I have problem with terms aggregation; the problem is when am trying to make an aggregation out of multiple lists of emails, elasticsearch is splitting the response in buckets.key by the "@" sign in the email address. the request is:

```
GET test/_search
{
	"_source": "emails",
    "query": {
        "term": {
            "companyId": {
                "value": 31953
            }
        }
    },
	"aggs": {
    "distinct_emails": {
      "terms": {
        "field": "emails"
      }
    }
  }
}

```

the full response is:

```
  {
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 18,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "test",
                "_type": "items",
                "_id": "8290c5f279dabb08a21ed11f3515a94e2408650d",
                "_score": 1,
                "_source": {
                    "emails": [
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "items",
                "_id": "64e5974d5db9e1379fd52393521e256b71b364f9",
                "_score": 1,
                "_source": {
                    "emails": [
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyotherdomain.com",
                        "info@companyname.com",
                        "info@companyname.com"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "items",
                "_id": "67754094f58ceca963ab0933c7ecb7f7cded6077",
                "_score": 1,
                "_source": {
                    "emails": [
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "items",
                "_id": "f71617eb607b3e77f03794e41ca239322d53b709",
                "_score": 1,
                "_source": {
                    "emails": [
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com",
                        "info@companyname.com"
                    ]
                }
            }
        ]
    },
    "aggregations": {
        "distinct_emails": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "companyname.com",
                    "doc_count": 13
                },
                {
                    "key": "info",
                    "doc_count": 13
                },
                {
                    "key": "companyotherdomain.com",
                    "doc_count": 1
                }
            ]
        }
    }
}

```

the buckets part in the response is:

```
"buckets": [
                {
                    "key": "companyname.com",
                    "doc_count": 13
                },
                {
                    "key": "info",
                    "doc_count": 13
                },
                {
                    "key": "companyotherdomain.com",
                    "doc_count": 1
                }
            ]

```

I'm expecting the below:

```
"buckets": [
                {
                    "key": "info@companyname.com",
                    "doc_count": 13
                },
                {
                    "key": "info@companyotherdomain.com",
                    "doc_count": 1
                }
            ]
```

---

<div class="post-metadata">

**Author:** ![pkast](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkast/32/55818_2.png) [@pkast](https://discuss.elastic.co/u/pkast)\
**Post date:** [October 18, 2019, 12:40pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/2 "2019-10-18T12:40:20Z")

</div>

Hi,

maybe this helps here as well:

> [@Aggregations and special characters](https://discuss.elastic.co/t/aggregations-and-special-characters/18559):
>
> I am performing an terms aggregation on a query to return the unique values of a field, in this case the field being emails in the format [example@gmail.com](mailto:example@gmail.com) or [example@aaa.example.com](mailto:example@aaa.example.com). "aggregations": { "users\_overall": { "terms": { "field": "email" } } } Instead of receiving back the full unique emails that I need I get results such as these: { key: [gmail.com](http://gmail.com) doc\_count: 121864 } { key: [yahoo.com](http://yahoo.com) doc\_count: 68648 } { key: [roadrunner.com](http://roadrunner.com) doc\_count: 58194 } { key: [opti…](http://optimum.net)

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [October 18, 2019, 1:04pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/3 "2019-10-18T13:04:01Z")

</div>

Hi @isamzwuairi,

Can you show your mapping, you may need to use multi-field to have a keyword type and your actual analyzer, so you can make your aggregations on the keyword mapped field. You can check the doc here:  
[https://www.elastic.co/guide/en/elasticsearch/reference/7.4/multi-fields.html#multi-fields](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/multi-fields.html#multi-fields)

---

<div class="post-metadata">

**Author:** ![isamzwuairi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isamzwuairi/32/56163_2.png) [@isamzwuairi](https://discuss.elastic.co/u/isamzwuairi)\
**Post date:** [October 18, 2019, 1:46pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/4 "2019-10-18T13:46:13Z")

</div>

Thanks for your reply. Any suggestions on how to fix this behavior?

---

<div class="post-metadata">

**Author:** ![isamzwuairi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/isamzwuairi/32/56163_2.png) [@isamzwuairi](https://discuss.elastic.co/u/isamzwuairi)\
**Post date:** [October 18, 2019, 1:47pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/5 "2019-10-18T13:47:15Z")

</div>

Hi @gabriel_tessier,

Thanks for your replay, the field mapping is:

```
{
    "test": {
        "mappings": {
            "emails": {
                "full_name": "emails",
                "mapping": {
                    "emails": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        },
                        "fielddata": true
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [October 18, 2019, 1:51pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/6 "2019-10-18T13:51:03Z")

</div>

You can make the aggregations on "emails.keyword".

Something like:

`"aggs": { "distinct_emails": { "terms": { "field": "emails.keyword" } }`

---

<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:** [November 15, 2019, 1:51pm UTC](https://discuss.elastic.co/t/terms-aggregation-for-emails-list/204222/7 "2019-11-15T13:51:05Z")

</div>

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