简体   繁体   中英

filter '_index' same way as '_type' in search across multiple index query elastic search

I have two indexes index1 and index2 and both has two types type1 and type2 with same name in elastic search.(please assume that we have valid business reason behind it)

I would like to search index1 - type1 and index2 -type2

here is my query

POST _search
{
 "query": {
    "indices": {
      "indices": ["index1","index2"],      
      "query": {
        "filtered":{  
         "query":{
       "multi_match": {
           "query": "test",
           "type": "cross_fields",
           "fields": ["_all"]         
       }

        },
         "filter":{  
            "or":{  
               "filters":[  
                  {  
                     "terms":{ 
                                "_index":["index1"], // how can i make this work?
                               "_type": ["type1"]
                     }                      
                  },
                  {  
                     "terms":{ 
                               "_index":["index2"], // how can i make this work?
                               "_type": ["type2"]
                     }                      
                  }
               ]
            }
         }
      }
      },
      "no_match_query":"none"
    }
  }
 }

You can use the indices , type in a bool filter to filter on type and index The query would look something on these lines :

POST  index1,index2/_search
{
  "query": {
    "filtered": {
      "query": {
        "multi_match": {
          "query": "test",
          "type": "cross_fields",
          "fields": [
            "_all"
          ]
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "indices": {
                "index": "index1",
                "filter": {
                  "type": {
                    "value": "type1"
                  }
                },
                "no_match_filter": "none"
              }
            },
            {
              "indices": {
                "index": "index2",
                "filter": {
                  "type": {
                    "value": "type2"
                  }
                },
                "no_match_filter": "none"
              }
            }
          ]
        }
      }
    }
  }
}

Passing the index names in the url example : index1,index2/_search is a good practice else you risk executing query across all indices in the cluster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM