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

问题描述 投票:1回答:1

是否可以使用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个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.