ElasticSearch script查询报错Variable [field] is not defined

ES版本7.6.0
想筛选某两个字段之和大于10的文档,需要用到脚本,简化的请求如下

{
  "query":
  {
    "bool":
    {
      "must":
      [
        {
          "script":
          {
            "script": "doc['field'].value >= 1"
          }
        }
      ]
    }
  }
}

报错提示field未定义,报错详情

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "doc[field]. ...",
          "    ^---- HERE"
        ],
        "script" : "doc[field].value >= 1",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "...",
        "node" : "...",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: ...",
          "index_uuid" : "...",
          "index" : "...",
          "caused_by" : {
            "type" : "script_exception",
            "reason" : "compile error",
            "script_stack" : [
              "doc[field]. ...",
              "    ^---- HERE"
            ],
            "script" : "doc[field].value >= 1",
            "lang" : "painless",
            "caused_by" : {
              "type" : "illegal_argument_exception",
              "reason" : "Variable [field] is not defined."
            }
          }
        }
      }
    ],
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "script_stack" : [
        "doc[field]. ...",
        "    ^---- HERE"
      ],
      "script" : "doc[field].value >= 1",
      "lang" : "painless",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "Variable [field] is not defined."
      }
    }
  },
  "status" : 400
}

解决方法:改用双引号包裹包裹字段名

{
  "query":
  {
    "bool":
    {
      "must":
      [
        {
          "script":
          {
            "script": "doc[\"field\"].value >= 1"
          }
        }
      ]
    }
  }
}

你可能感兴趣的:(elasticsearch,大数据,搜索引擎,java)