一尘不染

在Elasticsearch中,如何将时区应用于脚本化日期操作?

elasticsearch

通过下面的汇总并使用ES5,我想基于给定的时区(从TZ数据库提供作为标识符)获取dayOfWeek和hourOfDay。

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

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

阅读 382

收藏
2020-06-22

共1个答案

一尘不染

这样的事情应该起作用:

{
  "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"
              }
            }
          }
        }
      }
    }
  }
}

您可能需要通过添加script.engine.groovy.inline.aggs: on到elasticsearch.yml文件来启用groovy的内联脚本。请参阅:此讨论

注意。上面的内容无法正常工作,因为它已被锁定,并且不允许您编辑白名单。

2020-06-22