来自Elasticsearch和Java API中的timestamp字段的每小时聚合

时间:2017-11-30 08:07:22

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

是否可以使用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
    }
  ]

由于

1 个答案:

答案 0 :(得分:0)

使用存储的脚本实现它。

// 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