# Elasticsearch group data

**URL:** <https://discuss.elastic.co/t/elasticsearch-group-data/174040>\
**Category:** Elasticsearch\
**Created:** [March 27, 2019, 4:39am UTC](https://discuss.elastic.co/t/elasticsearch-group-data/174040 "2019-03-27T04:39:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timotius\_Pamungkas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timotius_pamungkas/32/42522_2.png) [@Timotius\_Pamungkas](https://discuss.elastic.co/u/Timotius_Pamungkas)\
**Post date:** [March 27, 2019, 4:39am UTC](https://discuss.elastic.co/t/elasticsearch-group-data/174040/1 "2019-03-27T04:39:05Z")

</div>

Hello, if I have data like this

| ID | Username | Role | Email |
| --- | --- | --- | --- |
| 1001 | johndoe | admin | [johndoe@mail.com](mailto:johndoe@mail.com) |
| 1001 | jonathan.doe | user | [johndoe@mail.com](mailto:johndoe@mail.com) |
| 1001 | johnnydoe | user | [johndoe@mail.com](mailto:johndoe@mail.com) |

Can I do some kind of grouping?  
Notice that :

- ID is same for all rows
- there are 3 distinct usernames
- there are 2 distinct roles
- email is same for all rows
- Other ID might have different combination (e.g 4 distinct emails but all other data are the same)

Can I do query on elasticsearch, so I get data like this :

```auto
{
   "id":"1001",
   "username":[
      "johndoe",
      "jonathan.doe",
      "johnnydoe"
   ],
   "role":[
      "admin",
      "user"
   ],
   "email":[
      "johndoe@mail.com"
   ]
}

```

Thank you

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 27, 2019, 8:22am UTC](https://discuss.elastic.co/t/elasticsearch-group-data/174040/2 "2019-03-27T08:22:22Z")

</div>

I'd run a `terms` agg on `id` with 3 inner `terms` aggs on `username`, `role`, `email`.

---

<div class="post-metadata">

**Author:** ![Timotius\_Pamungkas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timotius_pamungkas/32/42522_2.png) [@Timotius\_Pamungkas](https://discuss.elastic.co/u/Timotius_Pamungkas)\
**Post date:** [March 28, 2019, 4:49am UTC](https://discuss.elastic.co/t/elasticsearch-group-data/174040/3 "2019-03-28T04:49:40Z")

</div>

Hi, can you give example?

This one won't work

```auto
{
    "from": 0,
    "size": 100,
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "id": {
                            "query": "1001",
                            "fuzziness": "0"
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "usernames": {
            "terms": {
                "field": "username"
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 28, 2019, 8:06am UTC](https://discuss.elastic.co/t/elasticsearch-group-data/174040/4 "2019-03-28T08:06:00Z")

</div>

> [@Timotius\_Pamungkas](#):
>
> This one won't work

What does it mean?

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<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:** [April 25, 2019, 8:06am UTC](https://discuss.elastic.co/t/elasticsearch-group-data/174040/5 "2019-04-25T08:06:04Z")

</div>

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