6

通过下面的聚合并使用 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',
              },
            },
          },
        },
      },
    },
4

2 回答 2

8

使用无痛找到解决方案。因为他们正在将elasticsearch从Joda迁移到原生java.time,所以对Joda的支持在painless上并不好。

{
  "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"
              }
            }
          }
        }
      }
    }
  }
}
于 2017-10-19T13:20:03.803 回答
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"
              }
            }
          }
        }
      }
    }
  }
}

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

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

于 2016-11-10T12:43:21.763 回答