我能在ES中的一个查询中同时运行内联脚本和存储脚本吗?

时间:2017-01-29 17:22:40

标签: elasticsearch groovy

ES 2.4.0
我尝试使用内联脚本和存储脚本进行聚合。但是,当我执行代码时,它显示错误..

{
      "size": 0,
      "aggregations": {
        "dayOfWeek": {
          "terms": {
            "script": "doc['created_at'].date.dayOfWeek().getAsText()"
          },
          "aggs": {
            "byHours": {
              "terms": {
                "order": {
                  "_term": "asc"
                },
                "script": "test",
                "params": {
                  "date_field": "created_at",
                  "format": "HH"
                }
              }
            }
          }
        }
      }
    }

当我执行上面的代码时,我收到的错误就像这些

{
  "took": 106,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 1,
    "failed": 4,
    "failures": [
      {
        "shard": 1,
        "index": "test",
        "node": "PHgQhqS8T6KORwsJ5ChzHg",
        "reason": {
          "type": "script_exception",
          "reason": "failed to run inline script [test] using lang [groovy]",
          "caused_by": {
            "type": "missing_property_exception",
            "reason": "No such property: test for class: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
          }
        }
      }
    ]

1 个答案:

答案 0 :(得分:1)

您需要像这样引用您的脚本文件:

{
  "size": 0,
  "aggregations": {
    "dayOfWeek": {
      "terms": {
        "script": "doc['created_at'].date.dayOfWeek().getAsText()"
      },
      "aggs": {
        "byHours": {
          "terms": {
            "order": {
              "_term": "asc"
            },
            "script": {                   <--- modify this section
              "file": "test",
              "params": {
                 "date_field": "created_at",
                 "format": "HH"
              }
            }
          }
        }
      }
    }
  }
}