2017-08-16 37 views
0

我试图通过聚合将我的文档按小时分组一段时间,但总是得到“期望的字段名称”异常但得到了[START_OBJECT]“?有什么问题?如何在弹性搜索聚合中将文档按小时分组?

{ 
    "query" : { 
    "bool" : { 
     "must" : { 
     "range" : { 
      "timestamp" : { 
      "from" : "2017-08-14 00:00:00", 
      "to" : "2017-08-15 00:00:00", 
      "include_lower" : true, 
      "include_upper" : true 
      } 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "result_by_hours": { 
     "histogram": { 
     "script": "doc.timestamp.date.getHourOfDay()", 
     "interval": 1 
     } 
    } 
    } 
} 

我期望的是返回昨天每小时的文件数量。我如何使用动态实时代替“2017-08-14 - 2017-08-15”?

感谢提前:)

+0

第二尝试也失败了:( “AGGS”:{ “result_by_hours”:{ “date_histogram”:{ “场”: “时间戳”, “间隔”: “小时”, “格式”:“ k“ } } } – Joey

+0

什么是es版本? – sunkuet02

+0

ES版本是2.3 – Joey

回答

0

根据ES版本,您可以到“现在”使用相对范围过滤器/查询,现在前-1D/d会1天时光倒流。

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

至于AGGS见的例子你也可以组通过的间隔例如使用date_histogram与间隔 在ES 5.5小时: 查询:

"range" : { 
      "timestamp" : { 
      "gte" : "now-1d/d, 
      "lte" : "now/d" 
      } 
     } 

Aggs:

{ 
     "aggs" : { 
      "values_over_time" : { 
       "date_histogram" : { 
        "field" : "timestamp", 
        "interval" : "1h" 
       } 
      } 
     } 
    }