小编典典

Elasticsearch 2.4,对嵌套对象不起作用存在过滤器

elasticsearch

我的映射是:

"properties": {
  "user": {
    "type": "nested",
    "properties": {
      "id": {
        "type": "integer"
      },
      "is_active": {
        "type": "boolean",
        "null_value": false
      },
      "username": {
        "type": "string"
      }
    }
  },

我想获取所有没有user字段的文档。

我试过了:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "user"
          }
        }
      ]
    }
  }
}

返回所有文档。基于ElasticSearch2.x,存在用于嵌套字段的过滤器不起作用的问题,我也尝试过:

GET /index/type/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "user"
              }
            }
          ]
        }
      }
    }
  }
}

返回0个文档。

使所有缺少该user字段的文档的正确查询是什么?


阅读 314

收藏
2020-06-22

共1个答案

小编典典

我找到了正确的语法,应该是:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "user",
            "query": {
              "exists": {
                "field": "user"
              }
            }
          }
        }
      ]
    }
  }
}
2020-06-22