过滤'_index'与搜索多个索引查询弹性搜索中的'_type'相同

vij*_*ani 0 json elasticsearch

我有两个索引index1index2,并且在弹性搜索中都有两个类型type1type2具有相同的名称.(请假设我们背后有有效的商业原因)

我想搜索index1 - type1index2 -type2

这是我的查询

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"
    }
  }
 }
Run Code Online (Sandbox Code Playgroud)

kee*_*ety 5

您可以使用索引,类型布尔过滤器类型和索引的查询看起来对这些东西线筛选:

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"
              }
            }
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在url示例中传递索引名称:index1,index2/_search是一种很好的做法,否则您可能会冒险在群集中的所有索引上执行查询.