# How to filter data using two indexes

**URL:** <https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193>\
**Category:** Elasticsearch\
**Created:** [July 18, 2019, 12:10pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193 "2019-07-18T12:10:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lakmina\_chathurya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lakmina_chathurya/32/45418_2.png) [@lakmina\_chathurya](https://discuss.elastic.co/u/lakmina_chathurya)\
**Post date:** [July 18, 2019, 12:10pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193/1 "2019-07-18T12:10:18Z")

</div>

hi team,

I want to select all the users from user index and filter software engineers from user\_job index and result should display in bucket aggregation

---

<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:** [July 18, 2019, 12:25pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193/2 "2019-07-18T12:25:44Z")

</div>

Welcome.

What did you do so far?

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:** ![lakmina\_chathurya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lakmina_chathurya/32/45418_2.png) [@lakmina\_chathurya](https://discuss.elastic.co/u/lakmina_chathurya)\
**Post date:** [July 18, 2019, 12:48pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193/3 "2019-07-18T12:48:22Z")

</div>

hi @dadoonet  
this is my query

```
GET metric/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
               
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "group": {
      "terms": {
        "field": "user.keyword",
        "size": 10000
      },
      "aggs": {
        "Score": {
          "terms": {
            "script": {
              "lang": "painless",
              "source": "\r\n long fixed= 0 ;\r\n"
            }
          }
        }
      }
    }
  }
}

```

this is my result

```
"aggregations" : {
"group" : {
  "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [
    {
      "key" : "david warner",
      "doc_count" : 203,
      "Score" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
          {
            "key" : "1.0",
            "doc_count" : 84
          },
          {
            "key" : "2.0",
            "doc_count" : 49
          }]}}]}}

```

i want to check these user roles are engineer. And user\_roles field in another index called user\_role. i need a buckets aggregation results after checking users are engineer.(it is something like select users from user and select user from user\_role where role=engineer) . need to deal with two indexes

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:** [July 18, 2019, 1:07pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193/4 "2019-07-18T13:07:42Z")

</div>

> [@lakmina\_chathurya](#):
>
> (it is something like select users from user and select user from user\_role where role=engineer) . need to deal with two indexes

You can't do that in elasticsearch (doing joins at search time).  
Unless you use the parent/child feature - but I'd use it only and only if I don't have any other choice.

I prefer doing joins at index time instead. Which means only index one type of document.

---

<div class="post-metadata">

**Author:** ![lakmina\_chathurya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lakmina_chathurya/32/45418_2.png) [@lakmina\_chathurya](https://discuss.elastic.co/u/lakmina_chathurya)\
**Post date:** [July 18, 2019, 1:51pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193/5 "2019-07-18T13:51:37Z")

</div>

thank you @dadoonet

---

<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:** [August 15, 2019, 1:51pm UTC](https://discuss.elastic.co/t/how-to-filter-data-using-two-indexes/191193/6 "2019-08-15T13:51:40Z")

</div>

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