# Get aggregation labels from another index

**URL:** https://discuss.elastic.co/t/get-aggregation-labels-from-another-index/320939
**Category:** Elasticsearch
**Created:** [December 10, 2022, 9:14am UTC](https://discuss.elastic.co/t/get-aggregation-labels-from-another-index/320939 "2022-12-10T09:14:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![masoud\_darvishi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/masoud_darvishi/32/114483_2.png) [@masoud\_darvishi](https://discuss.elastic.co/u/masoud_darvishi)
#### Post date: [December 10, 2022, 9:14am UTC](https://discuss.elastic.co/t/get-aggregation-labels-from-another-index/320939/1 "2022-12-10T09:14:36Z")

</div>

hi  
i have two indexes with below fields

**roles**

```auto
{
    id: 1,
    name: "admin",
},
{
    id: 2,
    name: "user"
},

```

**users**

```auto
{
    id: 1,
    name: "a",
    roles_id: [1,2],
},
{
    id: 2,
    name: "b",
    roles_id: [1],
}

```

below code aggregates users index base on roles\_id

```auto
"aggs": {
    "user_roles": {
        "terms": {
            "field": "roles_id"
        }
    }
}

```

and this is result of it

```auto
"user_roles": {
    "buckets": [
        {
             "key": 1,
             "doc_count": 2
         },
         {
              "key": 2,
               "doc_count": 1
         } 
      ]
}

```

**how can i add roles name in aggregate result?**  
thank you

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [December 11, 2022, 9:51pm UTC](https://discuss.elastic.co/t/get-aggregation-labels-from-another-index/320939/2 "2022-12-11T21:51:44Z")

</div>

Welcome to our community! 😃

This is a join and you need to reindex those two into a single index, there's nothing in Elasticsearch to do that at query time sorry to say.

---

<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 8, 2023, 9:51pm UTC](https://discuss.elastic.co/t/get-aggregation-labels-from-another-index/320939/3 "2023-01-08T21:51:53Z")

</div>

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