【问题标题】:Hourly aggregation from timestamp field in Elasticsearch and Java APIElasticsearch 和 Java API 中时间戳字段的每小时聚合
【发布时间】:2018-05-14 02:19:35
【问题描述】:

是否可以使用术语聚合和使用 elasticsearch Rest HL 客户端的内联脚本从时间戳字段中检索每小时存储桶。

https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575

我们如何使用 Elasticsearch Java HL Rest 客户端实现以下查询?

# script in terms aggs.
GET /pixeluidevent/uidevent/_search
{
"size": 0, 
"query": {
"bool": {
    "must": [ { "match": { "name": "testName"  }}]
    }
  },
  "aggs": {
    "BY_DAYOFWEEK": {
      "terms": {
        "script": {
          "lang": "painless",
          "inline": "doc['eventTime'].date.hourOfDay"
        }
       }
    }
   }
}

部分回复

"buckets": [
    {
      "key": "6",
      "doc_count": 36821
    },
    {
      "key": "0",
      "doc_count": 34000
    },
    {
      "key": "3",
      "doc_count": 30153
    },
    {
      "key": "2",
      "doc_count": 29452
    }
  ]

谢谢

【问题讨论】:

    标签: java elasticsearch elasticsearch-5 spring-data-elasticsearch


    【解决方案1】:

    使用存储的脚本实现它。

    // Code 
    Script _script = new Script(ScriptType.STORED, "painless", "doc['eventTime'].date.dayOfWeek", new HashMap<>());
    
    TermsAggregationBuilder termsAggBuilderForDOW = AggregationBuilders.terms("by_day_of_week").script(_script);
    termsAggBuilderForDOW.size(7); // 7 size
    

    PFA 链接了解有关此内容的详细信息。 https://discuss.elastic.co/t/hourly-aggregation-for-timestamp-and-java-api-irrespective-of-date/109575/8

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 2021-02-05
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多