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

时间:2014-10-01 02:52:55

标签: json elasticsearch

我有两个索引 index1 index2 ,两者都有两种类型 type1 type2 同名弹性搜索。(请假设我们背后有有效的商业理由)

我想搜索 index1 - type1 index2 -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"
    }
  }
 }

1 个答案:

答案 0 :(得分:5)

您可以使用indices过滤器中的typebool来过滤类型和索引 查询将在以下行中显示:

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"
              }
            }
          ]
        }
      }
    }
  }
}

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