Multiple Index Step by step query

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.

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

Are the documents similar in structure?

1 Like

Yes they are built from the same base structure

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

1 Like

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.

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