在Elasticsearch中,如何将时区应用于脚本化日期操作? [英] In Elasticsearch, how can I apply a timezone to a scripted date operation?

查看:374
本文介绍了在Elasticsearch中,如何将时区应用于脚本化日期操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过下面的汇总并使用ES5,我希望获得dayOfWeek&根据给定的时区(从TZ数据库提供作为标识符)的hourOfDay.

With the aggregation below and using ES5, I'd like to get the dayOfWeek & hourOfDay based on a given timezone (supplied as an identifier from the TZ database).

如何编辑"doc['created'].date.dayOfWeek'以调整偏移量?

How can I edit "doc['created'].date.dayOfWeek' to adjust for the offset?

    aggs: {
      dayOfWeek: {
        terms: {
          script: {
            inline: "doc['created'].date.dayOfWeek",
            lang: 'painless',
          },
        },
        aggs: {
          hourOfDay: {
            terms: {
              script: {
                inline: "doc['created'].date.hourOfDay",
                lang: 'painless',
              },
            },
          },
        },
      },
    },

推荐答案

类似的方法应该起作用:

Something like this should work:

{
  "size": 0,
  "aggregations": {
    "dayOfWeek": {
      "terms": {
        "script": {
          "inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.dayOfWeek",
          "lang": "groovy",
          "params": {
            "tz": "Europe/London"
          }
        }
      },
      "aggs": {
        "hourOfDay": {
          "terms": {
            "script": {
              "inline": "doc['created'].date.setZone(org.joda.time.DateTimeZone.forID(tz)); doc['created'].date.hourOfDay",
              "lang": "groovy",
              "params": {
                "tz": "Europe/London"
              }
            }
          }
        }
      }
    }
  }
}

您可能需要通过在Elasticsearch.yml文件中添加script.engine.groovy.inline.aggs: on来启用groovy的内联脚本.请参阅:这个讨论.

You will probably need to enable inline scripting for groovy by adding script.engine.groovy.inline.aggs: on to the elasticsearch.yml file. See: This discussion.

注意.上面的内容不会很痛苦,因为它已被锁定,并且不允许您编辑白名单..

Note. The above won't work with painless because it is locked down and does not allow you to edit the whitelist..

这篇关于在Elasticsearch中,如何将时区应用于脚本化日期操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆