2017-10-12 20 views
0

我有以下映射Elasticsearch:如何檢測一個人通過城市的天數?

PUT /traffic-data 
{ 
    "mappings": { 
     "traffic-entry": { 
     "_all": { 
     "enabled": false 
     }, 
     "properties": { 
      "CameraId": { 
      "type":"keyword" 
      }, 
      "VehiclePlateNumber": { 
      "type":"keyword" 
      }, 
      "DateTime": { 
      "type":"date" 
      } 
     } 
     } 
    } 
} 

我要計算多少天,每月有車輛停留。一個獨特的車輛是由VehiclePlateNumber確定。 所以,我想要得到的結果是這樣的:

VehiclePlaneNumber Month StayDays 
111     1  5 
222     1  1 
... 

我如何使用Elasticsearch查詢它做什麼?

這是我的嘗試:

GET traffic-data/_search? 
{ 
"size": 0, 
    "aggs":{ 
    "by_district":{ 
     "terms": { 
     "field": "VehiclePlateNumber", 
     "size": 100000 
     }, 
     "aggs": { 
     "by_month": { 
      "terms": { 
      "field": "DateTime", 
      "size": 12 
      } 
     } 
     } 
    } 
    } 
} 
+0

請粘貼111,222 VehiclePlaneNumber的樣本數據進行分析。 –

回答

0

你可以做的車牌號條款的聚集則條款上月子AGG然後在幾天之子AGG。

喜歡的東西:

GET traffic-data/_search 
{ 
"size": 0, 
    "aggs":{ 
    "by_district":{ 
     "terms": { 
     "field": "VehiclePlateNumber", 
     "size": 100000 
     }, 
     "aggs": { 
     "by_month": { 
      "terms": { 
      "field": "DateTime", 
      "size": 12 
      }, 
      "aggs": { 
      "days": { 
       "sum": { 
       "field": "days" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

月應該是一個腳本場,但將更好地計算它在索引時間。

這應該工作。

或者您可以使用以實體爲中心的設計並定期對計算出的值進行索引。請參閱https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes

+0

嗯,的確,我也有'月份'和'月份日期'這是從'DateTime'中提取的。你能不能在幾天內展示如何總結子集合? – Dinosaurius

相關問題