[why]
make es result more easy access.
[example]
input:
result_hash = {}
es_result =
[{"key"=>"build-pkg",
"doc_count"=>354526,
"all_job_state"=>
{"doc_count_error_upper_bound"=>0,
"sum_other_doc_count"=>0,
"buckets"=>
[{"key"=>"failed", "doc_count"=>5708},
{"key"=>"finished", "doc_count"=>1033},
{"key"=>"incomplete", "doc_count"=>204},
{"key"=>"submit", "doc_count"=>136},
...
output:
result_hash =
{"build-pkg"=>
{"failed"=>5708,
"finished"=>1033,
"incomplete"=>204,
"submit"=>136,
"OOM"=>11,
"post_run"=>2},
"cci-depends"=>
{"finished"=>1785,
"failed"=>675,
...
Signed-off-by: Lu Kaiyi <2392863668(a)qq.com>
---
lib/es_query.rb | 47 ++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 46 insertions(+), 1 deletion(-)
diff --git a/lib/es_query.rb b/lib/es_query.rb
index 29d50e9..dcd735b 100644
--- a/lib/es_query.rb
+++ b/lib/es_query.rb
@@ -129,7 +129,9 @@ class ESQuery
result = @client.search(index: @index + '*', body: query)['aggregations']["all_#{field1}"]['buckets']
return nil if result.empty?
- result
+ result_hash = {}
+ parse_fields(result, result_hash)
+ result_hash
end
end
@@ -217,3 +219,46 @@ def build_aggs_from_fields(fields, aggs_hash)
build_aggs_from_fields(fields, aggs_hash['aggs']["all_#{field}"])
end
+
+# input:
+# result_hash = {}
+# es_result =
+# [{"key"=>"build-pkg",
+# "doc_count"=>354526,
+# "all_job_state"=>
+# {"doc_count_error_upper_bound"=>0,
+# "sum_other_doc_count"=>0,
+# "buckets"=>
+# [{"key"=>"failed", "doc_count"=>5708},
+# {"key"=>"finished", "doc_count"=>1033},
+# {"key"=>"incomplete", "doc_count"=>204},
+# {"key"=>"submit", "doc_count"=>136},
+# ...
+#
+# output:
+# result_hash =
+# {"build-pkg"=>
+# {"failed"=>5708,
+# "finished"=>1033,
+# "incomplete"=>204,
+# "submit"=>136,
+# "OOM"=>11,
+# "post_run"=>2},
+# "cci-depends"=>
+# {"finished"=>1785,
+# "failed"=>675,
+# ...
+def parse_fields(es_result, result_hash)
+ es_result.each do |result|
+ key = result['key']
+ sub_field = result.keys.detect { |field| field.start_with?('all_') }
+
+ if sub_field
+ result_hash[key] = {}
+ all_field = result[sub_field]['buckets']
+ parse_fields(all_field, result_hash[key])
+ else
+ result_hash[key] = result['doc_count']
+ end
+ end
+end
--
2.23.0