是否可以使用elasticsearch Rest HL客户端使用术语聚合和内联脚本从时间戳字段检索每小时存储桶。
我们如何使用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
}
]
由于
答案 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