使用elastic search使用filtered过滤数据时报no [query] registered for [filtered]错误,报错信息如下

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "no [query] registered for [filtered]",
        "line": 3,
        "col": 17
      }
    ],
    "type": "parsing_exception",
    "reason": "no [query] registered for [filtered]",
    "line": 3,
    "col": 17
  },
  "status": 400
}

原因在于过滤器filter 已被弃用,并在ES5.0中删除所以应用bool/must/filter查询来替换

错误查询如下:

GET /megacorp/employee/_search
{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "age": {"gt": 30}
        }
      },
      "match": {
        "last_name": "Smith"
      }
    }
  }
}

更正为:

GET /megacorp/employee/_search
{
    "query" : {
        "bool" : {
            "must": {
              "match" : {
                "last_name" : "smith"
              }
            },
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            }
        }
    }
}

happy coding!