编写 Elasticsearch 2.1| 脚本没有这样的属性:文档

Scripting Elasticsearch 2.1| No such property: doc

我在尝试在 ES JSON 请求中执行脚本时遇到问题

要求:

POST _search
{
   "query": {
      "bool": {
         "must": [
            {
               "match_all": {}
            }
         ]
      }
   },
   "aggs": {
      "bucket_histogram": {
         "histogram": {
            "field": "dayTime",
            "interval": 10
         },
         "aggs": {
            "get_average": {
               "avg": {
                  "field": "value"
               }
            },
            "check-threshold": {
               "bucket_script": {
                  "buckets_path": {
                     "averageValue": "get_average"
                  },
                  "script": "averageValue - doc[\"thresholdValue\"].value"
               }
            }
         }
      }
   }
}

但是我得到这个错误而不是返回值

    {
   "error": {
      "root_cause": [],
      "type": "reduce_search_phase_exception",
      "reason": "[reduce] ",
      "phase": "fetch",
      "grouped": true,
      "failed_shards": [],
      "caused_by": {
         "type": "groovy_script_execution_exception",
         "reason": "failed to run inline script [averageValue - doc[\"thresholdValue\"].value] using lang [groovy]",
         "caused_by": {
            "type": "missing_property_exception",
            "reason": "No such property: doc for class: 7dcca7d142ac809a7192625d43d95bde9883c434"
         }
      }
   },
   "status": 503
}

然而,如果我删除 doc[\"thresholdValue\"] 并输入一个数字,一切正常。

您正在使用 bucket_script,它是随 Elasticsearch 2.0 发布的 pipeline aggregations 的一部分。管道聚合针对其他聚合而不是文档,这就是 doc 上下文未提供给聚合的原因。

如果您想针对特定文档处理聚合,那么您可能需要 scripted metric aggregation