【问题标题】:ElasticSearch 2.x exists filter for nested field doesn't workElasticSearch 2.x 存在嵌套字段的过滤器不起作用
【发布时间】:2016-11-07 22:31:23
【问题描述】:

我有以下映射

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

我正在尝试过滤所有存在可选“菜单”字段的文档

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

但是,当我尝试使用相同的查询来过滤那些具有“restaurant_name”的文档时,它可以正常工作。那么为什么嵌套字段检查不起作用呢?如何让它发挥作用?

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0 elasticsearch-dsl


    【解决方案1】:

    您需要改用nested 查询:

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

    【讨论】:

    • 感谢@val 的解决方案
    • 当您将逻辑反转为 must_not 时,这似乎不起作用。在我的用例中,它返回 0 个文档,尽管有很多缺少该字段
    • @pmishev 随时针对您的具体问题创建一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2021-11-06
    相关资源
    最近更新 更多