ElasticSearch 2.x exists filter for nested field doesn't work

匆匆过客 提交于 2019-12-24 08:50:02

问题


I have the following mapping

{
  "properties": {
    "restaurant_name": {"type": "string"},
    "menu": {
      "type": "nested",
      "properties": {
        "name": {"type": "string"}
      }
    }
  }
}

I am trying to filter all those documents which have optional "menu" field exists

GET /restaurnats/_search
{
  "filter": {
    "query": {
      "bool": {
        "must": [
          {"exists" : { "field" : "menu" }}
        ]
      }
    }
  }
}

But, when I try the same query to filter those documents which have "restaurant_name", then it works fine. So why nested field check doesn't work? How to get it work?


回答1:


You need to use a nested query instead:

{
  "filter": {
    "query": {
      "nested": {
        "path": "menu",
        "query": {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "menu"
                }
              }
            ]
          }
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/40475868/elasticsearch-2-x-exists-filter-for-nested-field-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!