Simple date histogram?

*爱你&永不变心* 提交于 2019-12-02 22:06:22

问题


Viewing documents on per weekday classification? My data is in a format like this:

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

For this I am using a dateConversion.groovy script and kept in the scripts folder in ES 5.1.1.

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

When I executed the following code in 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
}

回答1:


To use a script in an aggregation the script value is not a string but another object. I think you also need to specify lang alongside file.

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



回答2:


Some parts of my code need some modifications

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

And also my test.groovy code too

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


来源:https://stackoverflow.com/questions/41426875/simple-date-histogram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!