# Multiple Index Step by step query

**URL:** <https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096>\
**Category:** Elasticsearch\
**Created:** [May 25, 2020, 7:42am UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096 "2020-05-25T07:42:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivgenik](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ivgenik](https://discuss.elastic.co/u/ivgenik)\
**Post date:** [May 25, 2020, 7:42am UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096/1 "2020-05-25T07:42:08Z")

</div>

Hello, I probably need to create a multi step multi index query and I am not sure if such a thing is supported or if it is the right approach at all.  
Example Data:  
2 indexes , 1 for orders, 1 for invoices  
I made a simple query that scans the invoices index and counts the amount of referenced orders like so :

```
{
   "aggs":{
      "reference":{
         "terms":{
            "field":"References.DocType.SysName.keyword"
         }
      }
   },
   "query":{
      "bool":{
         "filter":[
            {
               "range":{
                  "MessageDate":{
                     "gte":"2015-05-24T00:00:00+03:00"
                  }
               }
            }
         ]
      }
   }
}

```

I am holding this information inside the invoice index so it is quite simple, by grouping by the docType of the references I can see how many references I got of type "order", also I needed to use a date in the query.

My problem is that later on I will also going to need to query for the actual orders data and not just the orders amount which located in a different index(orders).  
But in order to determine which orders to return I first need to run something like the query above to determine which orders qualifies.

From the first query I got access to the order Ids with which I can find the actual orders in the orders index.  
But I dont think it is very practical to make a second separate query by _XXXXX_ amount of ids.

So I was wondering if it is possible to somehow make a merged query that finds all the order ids by the needed terms against the invoices index and then finds the actual orders from the orders index and retrieves those.

---

<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:** [May 25, 2020, 7:45am UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096/2 "2020-05-25T07:45:32Z")

</div>

You're after a join, which Elasticsearch doesn't do sorry.

Are the documents similar in structure?

---

<div class="post-metadata">

**Author:** ![ivgenik](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ivgenik](https://discuss.elastic.co/u/ivgenik)\
**Post date:** [May 25, 2020, 7:47am UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096/3 "2020-05-25T07:47:12Z")

</div>

Yes they are built from the same base structure

---

<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:** [May 25, 2020, 7:57am UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096/4 "2020-05-25T07:57:36Z")

</div>

It might be easier to put them into the same index then?

---

<div class="post-metadata">

**Author:** ![ivgenik](https://avatars.discourse-cdn.com/v4/letter/i/ce73a5/32.png) [@ivgenik](https://discuss.elastic.co/u/ivgenik)\
**Post date:** [June 15, 2020, 3:09pm UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096/7 "2020-06-15T15:09:46Z")

</div>

Thank you for the replay,  
In the end I ended up adding 2 new fields for my MongoDB collections (which ends up in the elastic DB indexes via an ETL process). This allowed me to create a pretty straight forward flow of 2 queries against the elastic engine which solved my problem.

---

<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:** [July 13, 2020, 3:11pm UTC](https://discuss.elastic.co/t/multiple-index-step-by-step-query/234096/8 "2020-07-13T15:11:59Z")

</div>

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