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

Oli*_*oyd 4 timezone elasticsearch elasticsearch-painless

通过下面的聚合并使用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',
              },
            },
          },
        },
      },
    },
Run Code Online (Sandbox Code Playgroud)

sla*_*wek 7

找到使用无痛的解决方案.因为他们正在将弹性搜索从Joda迁移到本机java.time,所以对Joda的支持并不好无痛.

{
  "size": 0,
  "aggregations": {
    "dayOfWeek": {
      "terms": {
        "script": {
          "inline": "Instant.ofEpochMilli(doc.created.date.millis).atZone(ZoneId.of(params.tz)).dayOfWeek",
          "params": {
            "tz": "Europe/London"
          }
        }
      },
      "aggs": {
        "hourOfDay": {
          "terms": {
            "script": {
               "inline": "Instant.ofEpochMilli(doc.created.date.millis).atZone(ZoneId.of(params.tz)).hour",
               "params": {
                  "tz": "Europe/London"
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 他们也不赞成“内联”。请改用“来源”。 (2认同)

Val*_*Val 5

这样的事情应该工作:

{
  "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"
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

笔记。上述方法不适用于无痛,因为它已被锁定并且不允许您编辑白名单。.