简单的日期直方图?

时间:2017-01-02 12:29:45

标签: groovy elasticsearch-5

按工作日分类查看文档? 我的数据格式如下:

{"text": "hi","created_at": "2016-02-21T18:30:36.000Z"}

为此,我使用dateConversion.groovy脚本并保存在ES 5.1.1的脚本文件夹中。

Date date = new Date(doc[date_field].value);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(format);
format.format(date)

当我在ES PLUGIN中执行以下代码时:

      "aggs": {
        "byDays": {
            "terms": {
                "script": {
                    "lang": "groovy",
                    "file": "dateConversion",
                    "params": {
                        "date_field": "created_at",
                        "format": "EEEEEE"
                    }
                }
            }
        } ``

I am getting an exception like this:

    {
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Unable to find on disk file script [dateConversion] using lang [groovy]"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "testindex-stats",
        "node": "vVhZxH7pQ7CO3qpbYm_uew",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Unable to find on disk file script [dateConversion] using lang [groovy]"
        }
      }
    ],
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Unable to find on disk file script [dateConversion] using lang [groovy]"
    }
  },
  "status": 400
}

2 个答案:

答案 0 :(得分:0)

要在聚合中使用脚本,script值不是字符串而是另一个对象。我认为您还需要在lang旁指定file

"aggs": {
    "byDays": {
        "terms": {
            "script": {
                "lang": "groovy",
                "file": "dateConversion",
                "params": {
                    "date_field": "created_at",
                    "format": "EEEEEE"
                }
            }
        }
    } 
}

答案 1 :(得分:0)

我的代码的某些部分需要进行一些修改

{
   "aggs": {
    "byDays": {
      "terms": {
        "script":{
          "file":"test",
        "params": {
          "date_field": "created_at",
          "format": "EEEEEE"
        }
       }
      }
    }
  }
}

还有我的test.groovy代码

Date date = new Date(doc[date_field].value);
date.format(format);