{"agg_insignia_id" name="description" /> {"agg_insignia_id" />
9103

Elasticsearch result to table

Question:

I am using the gem <a href="https://github.com/elasticsearch/elasticsearch-rails" rel="nofollow">elasticsearch-rails</a> to retrieve data from elasticsearch in a dynamic way, meaning that the result can have none or multiple aggregations depending on users choices.

Imagine a response like this:

(...) "aggregations"=> {"agg_insignia_id"=> {"buckets"=> [{"key"=>1, "key_as_string"=>"1", "doc_count"=>32156, "agg_chain_id"=> {"buckets"=> [{"key"=>9, "key_as_string"=>"9", "doc_count"=>23079, "agg_store_id"=> {"buckets"=> [{"key"=>450, "key_as_string"=>"450", "doc_count"=>145, "agg_value"=>{"value"=>1785.13}}, {"key"=>349, "key_as_string"=>"349", "doc_count"=>143, "agg_value"=>{"value"=>1690.37}},

How can I transform that data in a tabular data? like

| insignia_id | chain_id | store_id | value | | 1 | 9 | 450 | 1785.13 | | 1 | 9 | 349 | 1690.37 | (...)

EDIT :: Being clear on the response I am looking for, two choices here: Array (simple) or Array of hashes.

<strong>Array style:</strong> [[insignia_id, chain_id, store_id, value], [1,9,450,1785.13], [1,9,349,1690.37],...]

<strong>Array of Hashes style:</strong> [{insignia_id => 1, chain_id => 9, store_id => 450, value => 1785.13}, {insignia_id => 1, chain_id => 9, store_id => 450, value => 1690.37 }]

The later is more like an activerecord style...

Answer1:

ok, so I came up with a solution for an array response.

Firstly added a helper for what comes ahead...

class Hash def deep_find(key, object=self, found=nil) if object.respond_to?(:key?) && object.key?(key) return object[key] elsif object.is_a? Enumerable object.find { |*a| found = deep_find(key, a.last) } return found end end end

now for the array algorithm (added in a concern):

def self.to_table_array(data, aggs, final_table = nil, row = []) final_table = [aggs.keys] if final_table.nil? hash_tree = data.deep_find(aggs.keys.first) if aggs.values.uniq.length == 1 && aggs.values.uniq == [:data] aggs.keys.each do |agg| row << data[agg]["value"] end final_table << row else hash_tree["buckets"].each_with_index do |h, index| row.pop if index > 0 aggs.shift if index == 0 row << h["key_as_string"] final_table = to_table_array(h, aggs.clone, final_table, row.clone) end end final_table end

The call for this method could be made like this:

#_fields = { "insignia_id" => :row, "chain_id" => :row, "store_id"=> :row, "value" => : data } #res.response => Elasticsearch response result = to_table_array(res.response, _fields)

There are some things quite specific to this case like you can see on this _fields variable. Also I'm assuming each aggregation has the name of the term itself. The rest is quite the same for every possible case.

A result of an array of hashes is pretty simple from here just by replacing few lines.

I put a lot of efford in this. Hope this helps someone else other than me.

Recommend

  • how to stack Vertically or Horizontally two MultiCheckboxField wtform fields
  • how to get monthname without using in group by in query and more efficient
  • Jquery flowplayer - tabs - content inside div tags not displaying
  • select with window function (dense_rank()) in SparkSQL
  • How to filter data between two times from hh:mm to hh:mm in mongoDB
  • How I can populate data in birt report by passing parameters in the URL
  • laravel first0rNew Integrity Constraint Violation
  • How to add Rating to sort list in Magento 1.7
  • How Strings are stored in a VBA Dictionary structure?
  • vim tabular plugin question
  • How to associate multiple value to a property using cypher queries in Neo4j
  • URL hash format, what's allowed and what's not?
  • Force html table to have eight columns on a single row
  • Sorting Tabulated Data
  • Getting android_key_not_configured facebook error
  • Implicit property animations do not work with CAReplicatorLayer?
  • Change the width of the JQM panels
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Assign variable to the value in HTML
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How to use carriage return with multiple line?
  • How do I alternate colors in Flat List (React Native)
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Could not find rake using whenever rails
  • Array.prototype.includes - not transformed with babel
  • Fetching methods from BroadcastReceiver to update UI
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • Symfony2: How to get request parameter
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • GridView Sorting works once only
  • Unanticipated behavior
  • WPF Applying a trigger on binding failure
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check