Get aggregation labels from another index

hi
i have two indexes with below fields

roles

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

users

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

below code aggregates users index base on roles_id

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

and this is result of it

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

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

Welcome to our community! :smiley:

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.

1 Like

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