对可选缺席字段

时间:2017-06-02 19:15:42

标签: elasticsearch elasticsearch-5

我有一个简单对象的弹性索引,它有两个字段:

{
   "content": "something",
   "acl": ["andrey", "gilles"]
}

acl字段是用户名数组或值“public”,或者根本不存在于对象中。我想写一个查询,它给我acl=testuser或不存在的所有对象。这是我的问题:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool" : {
            "must": [
              {"exists" : { "field" : "acl" }},
              {"match" : {"acl":"testuser"}}
            ]
          },
          "bool": {
            "must_not": [
                "exists": {
                    "field": "acl"
                }
            ]
          }
        }
      ]
    }
  }
}

但是当我执行它时,我收到一个错误:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 12,
        "col": 11
      }
    ],
    "type": "parsing_exception",
    "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
    "line": 12,
    "col": 11
  },
  "status": 400
}

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的JSON无效:

  1. 您在const bcrypt = require('bcrytpjs'); 查询
  2. 中设置了两次字段bool
  3. should是一个数组,您没有为新对象添加花括号
  4. 此外,您不需要must_not查询与exist查询匹配。因此,您只需将match查询和match must_not查询留在exist内,如下所示:

    should
相关问题