有一個相當複雜的解決方案,並沒有真正的高性能,使用以下scripted_metric
aggregation。
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"docs_per_month": {
"date_histogram": {
"field": "created_date",
"interval": "month",
"min_doc_count": 0
},
"aggs": {
"avg_doc_per_biz_day": {
"scripted_metric": {
"init_script": "_agg.bizdays = []; _agg.allbizdays = [:]; start = new DateTime(1970, 1, 1, 0, 0); now = new DateTime(); while (start < now) { def end = start.plusMonths(1); _agg.allbizdays[start.year + '_' + start.monthOfYear] = (start.toDate()..<end.toDate()).sum {(it.day != 6 && it.day != 0) ? 1 : 0 }; start = end; }",
"map_script": "_agg.bizdays << _agg.allbizdays[doc. created_date.date.year+'_'+doc. created_date.date.monthOfYear]",
"combine_script": "_agg.allbizdays = null; doc_count = 0; for (d in _agg.bizdays){ doc_count++ }; return doc_count/_agg.bizdays[0]",
"reduce_script": "res = 0; for (a in _aggs) { res += a }; return res"
}
}
}
}
}
}
讓我們來詳細介紹下面的每個腳本。
我在做什麼在init_script
是創建地圖工作日每個月的數量自1970年以來和存儲,在_agg.allbizdays
地圖。
_agg.bizdays = [];
_agg.allbizdays = [:];
start = new DateTime(1970, 1, 1, 0, 0);
now = new DateTime();
while (start < now) {
def end = start.plusMonths(1);
_agg.allbizdays[start.year + '_' + start.monthOfYear] = (start.toDate()..<end.toDate()).sum {(it.day != 6 && it.day != 0) ? 1 : 0 };
start = end;
}
在map_script
,我只是平日檢索每個文檔的月份數;
_agg.bizdays << _agg.allbizdays[doc.created_date.date.year + '_' + doc. created_date.date.monthOfYear];
在combine_script
,我總結的平均文檔數爲每個碎片
_agg.allbizdays = null;
doc_count = 0;
for (d in _agg.bizdays){ doc_count++ };
return doc_count/_agg.bizdays[0];
在
reduce_script
最後,我總結的平均文檔數爲每個節點:
res = 0;
for (a in _aggs) { res += a };
return res
再一次,我認爲它非常複雜,而且正如Andrei所說的那樣,最好等待2.0讓它按照它應該的方式工作,但是在此期間,如果你需要的話。
來源
2015-06-15 09:39:56
Val
明確需要更新我的個人資料... –