0

Im working on implementing searchkick in my project and got the search to work fine. Now Im trying to implement a filter system and use the aggs to display the filter criteria. The array that the aggs returns is a little complex for me and trying to figure out how to cycle through the specific parts. Heres the aggs it returns:

{"techniques"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>"Frying", "doc_count"=>1}, {"key"=>"Searing", "doc_count"=>1}]}, "ingredients"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>"Furikake, for serving", "doc_count"=>1}, {"key"=>"Kosher salt and ground black pepper", "doc_count"=>1}, {"key"=>"Salmon fillets", "doc_count"=>1}, {"key"=>"avocado, diced", "doc_count"=>1}, {"key"=>"cooked white rice", "doc_count"=>1}, {"key"=>"japanese cucumber", "doc_count"=>1}, {"key"=>"teriyaki sauce", "doc_count"=>1}, {"key"=>"to 8 scallions, thinly sliced", "doc_count"=>1}, {"key"=>"vegetable oil", "doc_count"=>1}]}, "cuisines"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>"Asian", "doc_count"=>1}, {"key"=>"Japanese", "doc_count"=>1}]}}

How would I write a do loop to cycle through just the cuisines part and pull out the individual names and counts from the buckets:

"cuisines"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[{"key"=>"Asian", "doc_count"=>1}, {"key"=>"Japanese", "doc_count"=>1}]}}

So basically I want a list that would look like this: Asian(1) Japanese(1)

1 Answer 1

1

What you have is a hash with array elements. It depends what you exactly you want, but assuming you assigned it a variable my_hash, you can do this for example:

my_hash['cuisines']['buckets'].reduce('') do |r, h| 
  r += "#{h['key']} (#{h['doc_count']}) "
end
=> "Asian (1) Japanese (1) "
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.