1
votes

I have 2 indices USER and URL. I want to run a query on different fields based on the index.

In USER index, query should search in name and id field. But in URL search has to be performed on title and id field.

POST /_search    
    {  
       "query":{  
          "indices":[  
             {  
                "indices":[  
                   "URL"
                ],
                "query":{  
                   "multi_match":{  
                      "query":"SMU ",
                      "fields":[  
                         "title",
                         "id"
                      ]
                   }
                }
             },
             {  
                "indices":[  
                   "USER"
                ],
                "query":{  
                   "multi_match":{  
                      "query":"SMU ",
                      "fields":[  
                         "name",
                         "id"
                      ]
                   }
                }
             }
          ]
       }
    }

The above query is not working. What is the change required to make it work. How can I merge multi_match search with indices search?

1
Which ES version are you using? What doesn't work with the current query? - Val
Currently, I'm using ES 5.1. The example query is not working. I looking for the query to do the job something like that. - Santosh Hegde

1 Answers

3
votes

The indices query is deprecated in ES 5, but it still works, you just have a bad structure in yours, i.e. you need to put each indices query in a bool/filter clause.

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "indices": {
            "indices": [
              "URL"
            ],
            "query": {
              "multi_match": {
                "query": "SMU ",
                "fields": [
                  "title",
                  "id"
                ]
              }
            }
          }
        },
        {
          "indices": {
            "indices": [
              "USER"
            ],
            "query": {
              "multi_match": {
                "query": "SMU ",
                "fields": [
                  "name",
                  "id"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

Since the indices query is deprecated, the new idea is to use a simple term query on the _index field instead. Try this:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": "URL"
                }
              },
              {
                "multi_match": {
                  "query": "SMU ",
                  "fields": [
                    "title",
                    "id"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": "USER"
                }
              },
              {
                "multi_match": {
                  "query": "SMU ",
                  "fields": [
                    "name",
                    "id"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}