Search across multiple indices

Hello,

I am trying to search multiple indices, each with its own query. Is it possible for 2.x?
I get the idea from here. Basically my composite query looks like this:

POST /index1, index2/_search
 {
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": index1
                }
              }  
            ], 
            "must": query1
          }
        },
        {
          "bool": {
            "filter": {
              "term": {
                "_index": index2
              }
            }, 
            "must": query2
          }
        }
      ]
    }
  }
}

This works well if both queries are simple. However, my query1 is a complex query that has a has-child piece in it. It fails even if I use query1 for index2, saying that "[has_child] No mapping for for type [subtype in index1]". Is it possible to send query1 to index1 only, and query2 to index2 only?

I would appreciate if someone can give some advice

I think you'll need to use an indices query instead of a term on the index field. The filters don't "share" knowledge so even though only one or the other part of the query apply to an index, all the parts of the query are checked against the mappings.

I think the indices query has a different behavior, but I'm not positive.

Also, 2.x is quite old. You should probably upgrade or try a newer version :slight_smile:

Thank you for replying, Zach! Yeah 2.x is old, but for this project my hands are tied and I need to stick with the old version.

It seems indices query is deprecated from 5.0 in favor of _index (link). Also indices query might not be helpful because I need to support more than 2 indices and each with a query, and indices query only supports query and not_match_query, thus is not extensible for my case.

My guess for query1 failing in index2 is also that query parsing and validation is done for each index. Currently my workaround is to create a subtype in index1 in index2, simply to make query1 run without error in index2. I am not sure about the performance penalty though.

Hi @Junfeng

Can msearch solve your problem?

Hi @gabriel_tessier, thanks for the answer. I was hoping to solve it by one query without the need to merge the multiple sorted lists and to maintain the positions for each list, as I also need to do pagination. But I think your solution is cleaner.

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