Different filter on multiple indexes

Hello everyone, i have a question about multi indexes search.
I want search multi indexes and sort by score, now i use _msearch query, like this

get  _msearch
{"index" : "user"}
{"query" : {"match" : {"global" : "keyword"}}}
{"index" : "index2"}
{"query" : {"bool" : {"must" : [{"term" : {"field" : {"value" : "value"}}}, {"match" : {"global" : 
"keyword"}}]}}}

but this will back two array, how can i search on multi indexes and get back one array, Beacuse i want sort by score and limit top five result.

Please help me , thx.

Scores from data in separate indexes may not necessarily be comparable, so it may not make sense to sort that data in a single list. One index may very well dominate the top hits, if the scores from that index are higher.

Having said that, you can query the _index field. You can use that to query each of the indexes with a single bool query. That would look something like this:

GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": {
              "term": {
                "_index": "user"
              }
            },
            "must": [
              {
                "match": {
                  "global": "keyword"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": {
              "term": {
                "_index": "index2"
              }
            },
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "field": {
                          "value": "value"
                        }
                      }
                    },
                    {
                      "match": {
                        "global": "keyword"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

thanks, It solved my problem.

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