2017-08-09 122 views
1

我有以下指標mytestindex和類型mytesttype在Elasticsearch 5.5:如何在elasticsearch中執行特定的搜索查詢?

PUT /mytestindex 
{ 
    "mappings": { 
     "mytesttype": { 
     "_source": { 
     "enabled": true 
     }, 
     "_all": { 
     "enabled": true 
     }, 
     "properties": { 
      "Id": { 
      "type":"text" 
      }, 
      "RadarId": { 
      "type":"keyword" 
      }, 
      "VehicleId": { 
      "type":"keyword" 
      }, 
      "Datetime": { 
      "type":"date", 
     "format": "yyyy-MM-dd HH:mm:ss.SSS" 
      }, 
      "Hour": { 
      "type":"integer" 
      } 
     } 
    } 
    } 
} 

我在Elasticsearch非常初學者。我想知道是否有可能在11:00至12:00之間獲得小時平均數量的車輛?我應該使用哪種類型的查詢?例如,在MySQL中我會做這樣的事情(的簡化版本,不考慮11:00-12:00):

SELECT AVG(value), datetime 
FROM  mytable 
GROUP BY HOUR(datetime) 

是否有可能以執行Elasticsearch類似的查詢?

回答

0

如果您對每小時的元素數量或其值的平均值感興趣,我不太瞭解這個問題嗎? 如果你想元素的數量,你可以使用Date Range Aggregation

例如:

{ 
"aggs": { 
    "range": { 
     "date_range": { 
      "field": "Datetime", 
      "format": "yyyy-MM-dd HH:mm:ss.SSS", 
      "ranges": [ 
       { "to": "now-1H/H" }, 
       { "from": "now-1H/H" } 
      ] 
     } 
    } 
} 
}