[why] make es result more easy access.
[example] input: 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@qq.com --- lib/es_query.rb | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-)
diff --git a/lib/es_query.rb b/lib/es_query.rb index 91aa6ab..928bd68 100644 --- a/lib/es_query.rb +++ b/lib/es_query.rb @@ -128,7 +128,7 @@ class ESQuery result = @client.search(index: @index + '*', body: query)['aggregations']["all_#{field1}"]['buckets'] return nil if result.empty?
- result + parse_fields(result) end end
@@ -216,3 +216,47 @@ def build_aggs_from_fields(fields) aggs_hash['aggs']["all_#{field}"].merge!(sub_aggs) if sub_aggs aggs_hash end + +# input: +# 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 + all_field = result[sub_field]['buckets'] + result_hash[key] = parse_fields(all_field) + else + result_hash[key] = result['doc_count'] + end + end + + result_hash +end
+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
all_field = result[sub_field]['buckets']
result_hash[key] = parse_fields(all_field)
- else
result_hash[key] = result['doc_count']
- end
above if-else-end => result_hash[key] = result['doc_count'] unless sub_field all_field = result[sub_field]['buckets'] result_hash[key] = parse_fields(all_field)
Thanks, Luan Shengde
- end
- result_hash
+end
2.23.0
On Wed, Jan 13, 2021 at 10:52:03AM +0800, Luan Shengde wrote:
+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
all_field = result[sub_field]['buckets']
result_hash[key] = parse_fields(all_field)
- else
result_hash[key] = result['doc_count']
- end
above if-else-end => result_hash[key] = result['doc_count'] unless sub_field
if this, need "next" unless sub_field result_hash[key] = result['doc_count'] next end
Thanks, Weitao
all_field = result[sub_field]['buckets'] result_hash[key] = parse_fields(all_field)
Thanks, Luan Shengde
- end
- result_hash
+end
2.23.0
On Wed, Jan 13, 2021 at 10:56:07AM +0800, Lu Weitao wrote:
On Wed, Jan 13, 2021 at 10:52:03AM +0800, Luan Shengde wrote:
+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
all_field = result[sub_field]['buckets']
result_hash[key] = parse_fields(all_field)
- else
result_hash[key] = result['doc_count']
- end
above if-else-end => result_hash[key] = result['doc_count'] unless sub_field
if this, need "next" unless sub_field result_hash[key] = result['doc_count'] next end
forget this -_-||
Thanks, Luan Shengde
Thanks, Weitao
all_field = result[sub_field]['buckets'] result_hash[key] = parse_fields(all_field)
Thanks, Luan Shengde
- end
- result_hash
+end
2.23.0