2015-12-23 53 views
1

我目前正試圖從彈性搜索中收集的數據生成圖表。產生一個用戶每次我插入在ES的記錄,具有以下的(例如)數據:使用doc_count作爲累計計數

{ 
    "country": "US", 
    "id": "79ca9523dcd62420030de12b75e08bb7", 
    "createdAt": "1450912898" 
} 

ID是用戶ID的散列,使得用戶ID不能從ID確定存儲在ES中,出於隱私的原因。

在ES指數類型映射如下:

{ 
    "user": { 
    "_timestamp": { 
     "enabled": true 
    }, 
    "properties": { 
     "country": { 
     "type": "string" 
     }, 
     "createdAt": { 
     "type": "date", 
     "format": "epoch_second" 
     }, 
     "id": { 
     "type": "string", 
     "index": "not_analyzed" 
     } 
    } 
    } 
} 

現在,每天能獲得用戶的圖形,我有以下查詢:

{ 
    "size": 0, 
    "query": { 
    "type": { 
     "value": "user" 
    } 
    }, 
    "aggs": { 
    "users_per_day": { 
     "date_histogram": { 
     "field": "createdAt", 
     "interval": "day" 
     } 
    } 
    } 
} 

這給了我這樣的好結果(因爲我設置的時間間隔爲分鐘,讓你對問題有什麼瞭解):

[{ 
    "key_as_string": "1450909920", 
    "key": 1450909920000, 
    "doc_count": 8 
}, 
{ 
    "key_as_string": "1450909980", 
    "key": 1450909980000, 
    "doc_count": 2 
}, 
{ 
    "key_as_string": "1450910040", 
    "key": 1450910040000, 
    "doc_count": 5 
}, 
{ 
    "key_as_string": "1450910100", 
    "key": 1450910100000, 
    "doc_count": 8 
}, 
{ 
    "key_as_string": "1450910160", 
    "key": 1450910160000, 
    "doc_count": 4 
}, 
{ 
    "key_as_string": "1450910220", 
    "key": 1450910220000, 
    "doc_count": 3 
}, 
{ 
    "key_as_string": "1450910280", 
    "key": 1450910280000, 
    "doc_count": 6 
}] 

我想用doc_count生成累積圖表,以便我可以看到我的用戶基數的增長,而不是每天創建的帳戶數量。儘管在互聯網上搜索,我找不到一個似乎與我的問題有關的答案。我找到的大多數答案都是引導我進入Cumulative Sum Aggregation頁面,但給出的例子會給你一個桶中所有結果的累計總和。我想要累積所有存儲桶總數。

+0

所以,下面您的示例'doc_count'爲' 「key_as_string」: 「1450909980」'應該是8(以前doc_count)+ 2? –

+0

它不一定必須存儲在doc_count中,但是。 –

+0

這聽起來更像是可以在客戶端完成的事情。我的意思是,信息的重要部分在那裏,所得到的一組數據只需要調整。另外,根據您計劃如何使用這些結果(例如UI圖),「累積」事情可能會在圖本身中處理。 –

回答

3

你與cumulative sum aggregation正確的道路,你絕對可以使用它。你只需要使用特殊的_count bucket path,這將做你期望的工作。

{ 
    "size": 0, 
    "query": { 
    "type": { 
     "value": "user" 
    } 
    }, 
    "aggs": { 
    "users_per_day": { 
     "date_histogram": { 
     "field": "createdAt", 
     "interval": "day" 
     }, 
     "aggs": { 
     "cumulative": { 
      "cumulative_sum": { 
      "buckets_path": "_count" 
      } 
     } 
     } 
    } 
    } 
} 

結果將是這樣的:

[{ 
    "key_as_string": "1450909920", 
    "key": 1450909920000, 
    "doc_count": 8, 
    "cumulative": {"value": 8} 
}, 
{ 
    "key_as_string": "1450909980", 
    "key": 1450909980000, 
    "doc_count": 2, 
    "cumulative": {"value": 10} 
}, 
{ 
    "key_as_string": "1450910040", 
    "key": 1450910040000, 
    "doc_count": 5, 
    "cumulative": {"value": 15} 
}, 
{ 
    "key_as_string": "1450910100", 
    "key": 1450910100000, 
    "doc_count": 8, 
    "cumulative": {"value": 23} 
}, 
{ 
    "key_as_string": "1450910160", 
    "key": 1450910160000, 
    "doc_count": 4, 
    "cumulative": {"value": 27} 
}, 
{ 
    "key_as_string": "1450910220", 
    "key": 1450910220000, 
    "doc_count": 3, 
    "cumulative": {"value": 30} 
}, 
{ 
    "key_as_string": "1450910280", 
    "key": 1450910280000, 
    "doc_count": 6, 
    "cumulative": {"value": 36} 
}] 
+0

謝謝,這正是我所需要的。我試圖使用'doc_count','count',但沒有考慮'_count' –

+0

很高興它幫助! – Val