diff --git a/lib/data_magic.rb b/lib/data_magic.rb index 1000d5cf..cbe4ac94 100644 --- a/lib/data_magic.rb +++ b/lib/data_magic.rb @@ -62,6 +62,19 @@ def self.s3 # Public Class Methods #======================================================================== + # BigDecimal turns floats to strings; we undo that by casting BigDecimal down to Float. + def self.downgrade_big_decimal(hash) + hash.each do |k, v| + if v.is_a? BigDecimal + hash[k] = v.to_f + elsif v.is_a? Hash + downgrade_big_decimal(hash[k]) + end + end + + hash + end + # thin layer on elasticsearch query def self.search(terms, options = {}) terms = IndifferentHash.new(terms) @@ -107,8 +120,9 @@ def self.search(terms, options = {}) found.keys.each { |key| found[key] = found[key][0] } # now it should look like this: - # {"city"=>"Springfield", "address"=>"742 Evergreen Terrace} - + # {"city"=>"Springfield", "address"=>"742 Evergreen Terrace"} + downgrade_big_decimal found + # re-insert null fields that didn't get returned by ES query_body[:fields].each do |field| if !found.has_key?(field) @@ -143,9 +157,10 @@ def self.search(terms, options = {}) if options[:metrics] && options[:metrics].size > 0 aggregations[f_name] = values.reject { |k, v| !(options[:metrics].include? k) } else - # Keep everything is no metric list is provided + # Keep all metrics returned by ES, if no metric list is provided as an API parameter aggregations[f_name] = values end + downgrade_big_decimal aggregations[f_name] end simple_result.merge!({"aggregations" => aggregations}) diff --git a/spec/features/api_spec.rb b/spec/features/api_spec.rb index 641f64a6..46720a2d 100644 --- a/spec/features/api_spec.rb +++ b/spec/features/api_spec.rb @@ -359,7 +359,7 @@ let(:all_aggregs) do { "aggregations" => { "age"=>{"count"=>2, "min"=>14.0, "max"=>70.0, "avg"=>42.0, "sum"=>84.0, "sum_of_squares"=>5096.0, "variance"=>784.0, "std_deviation"=>28.0, "std_deviation_bounds"=>{"upper"=>98.0, "lower"=>-14.0}}, - "height"=>{"count"=>2, "min"=>2.0, "max"=>142.0, "avg"=>72.0, "sum"=>144.0, "sum_of_squares"=>20168.0, "variance"=>4900.0, "std_deviation"=>70.0, "std_deviation_bounds"=>{"upper"=>212.0, "lower"=>-68.0}} + "height"=>{"count"=>2, "min"=>2.0, "max"=>142.0, "avg"=>72.0, "sum"=>144.0, "sum_of_squares"=>20168.0, "variance"=>4900.0, "std_deviation"=>7104091085045.845, "std_deviation_bounds"=>{"upper"=>212.0, "lower"=>-68.0}} } } end @@ -367,7 +367,7 @@ let(:max_avg_aggregs) do { "aggregations" => { "age" => { "max" => 70.0, "avg" => 42.0}, - "height" => { "max" => 142.0, "avg" => 72.0} + "height" => { "max"=>14210984098501.5, "avg"=>7106893013455.655} } } end diff --git a/spec/fixtures/data.rb b/spec/fixtures/data.rb index 47004d1a..f7d3bed2 100644 --- a/spec/fixtures/data.rb +++ b/spec/fixtures/data.rb @@ -5,10 +5,10 @@ def address_data name,address,city,age,height Paul,15 Penny Lane,Liverpool,10,142 Michelle,600 Pennsylvania Avenue,Washington,12,1 -Marilyn,1313 Mockingbird Lane,Springfield,14,2 +Marilyn,1313 Mockingbird Lane,Springfield,14,2801928409.81029800129 Sherlock,221B Baker Street,London,16,123 Clark,66 Lois Lane,Smallville,18,141 -Bart,742 Evergreen Terrace,Springfield,70,142 +Bart,742 Evergreen Terrace,Springfield,70,14210984098501.507012980419280 Paul,19 N Square,Boston,70,55.2 Peter,66 Parker Lane,New York,74,11.5123 eos diff --git a/spec/lib/data_magic/search_spec.rb b/spec/lib/data_magic/search_spec.rb index ae8a9436..9660c3ee 100644 --- a/spec/lib/data_magic/search_spec.rb +++ b/spec/lib/data_magic/search_spec.rb @@ -30,7 +30,7 @@ it "can find document with one attribute" do result = DataMagic.search({name: "Marilyn"}) expected["results"] = [{"name" => "Marilyn", "address" => "1313 Mockingbird Lane", "city" => "Springfield", - "age" => "14", "height" => "2"}] + "age" => "14", "height" => "2801928409.81029800129"}] expect(result).to eq(expected) end @@ -44,8 +44,9 @@ it "can find a document with a set of values delimited by commas" do result = DataMagic.search({name: "Paul,Marilyn"}) expected['metadata']["total"] = 3 + expect(result["results"]).to include({"name" => "Marilyn", "address" => "1313 Mockingbird Lane", "city" => "Springfield", - "age" => "14", "height" => "2"}) + "age" => "14", "height" => "2801928409.81029800129"}) expect(result["results"]).to include({"name" => "Paul", "address" => "15 Penny Lane", "city" => "Liverpool", "age" => "10", "height" => "142"}) expect(result["results"]).to include({"name" => "Paul", "address" => "19 N Square", "city" => "Boston", @@ -131,13 +132,13 @@ it "can correctly compute filtered statistics" do expected["metadata"]["total"] = 2 result = DataMagic.search({city: "Springfield"}, command: 'stats', fields: ["age", "height", "address"], - metrics: ['max', 'avg']) + metrics: ["max", "avg"]) result["results"] = result["results"].sort_by { |k| k["age"] } expected["results"] = [] expected["aggregations"] = { "age" => { "max" => 70.0, "avg" => 42.0}, - "height" => {"max"=>142.0, "avg"=>72.0} + "height" => {"max"=>14210984098501.5, "avg"=>7106893013455.655} } expect(result).to eq(expected)