Yasuhiro Horimoto 2019-03-26 05:24:32 +0900 (Tue, 26 Mar 2019) Revision: dbfe19ec2af2075e64aedb01585f36ea8e3d65a9 https://github.com/groonga/groonga-command/commit/dbfe19ec2af2075e64aedb01585f36ea8e3d65a9 Message: Fix a bug that conversion result is wrong (#12) * Use "columns" parameter * Fix a bug that conversion result is wrong * test: add test case for unusing ":columns" * Don't split he command object to name and arguments Modified files: lib/groonga/command/base.rb lib/groonga/command/format/elasticsearch.rb test/command/test-base.rb Modified: lib/groonga/command/base.rb (+1 -1) =================================================================== --- lib/groonga/command/base.rb 2019-03-22 08:30:36 +0900 (eb4185c) +++ lib/groonga/command/base.rb 2019-03-26 05:24:32 +0900 (7b68396) @@ -156,7 +156,7 @@ module Groonga end def to_elasticsearch_format(options={}) - format = Format::Elasticsearch.new(@command_name, normalized_arguments) + format = Format::Elasticsearch.new(self) format.json(options) end Modified: lib/groonga/command/format/elasticsearch.rb (+49 -53) =================================================================== --- lib/groonga/command/format/elasticsearch.rb 2019-03-22 08:30:36 +0900 (8be9e3a) +++ lib/groonga/command/format/elasticsearch.rb 2019-03-26 05:24:32 +0900 (6d5eb0b) @@ -18,71 +18,67 @@ module Groonga module Command module Format class Elasticsearch - def initialize(name, arguments) - @name = name - @arguments = arguments + def initialize(command) + @command = command end def json(options={}) - return nil unless @name == "load" + return nil unles****@comma*****_a?(Load) components = [] - elasticsearch_version = options[:version] || 5 + action = build_action(options) + each_source do |source| + components << JSON.generate(action) + components << JSON.generate(source) + end + components.join("\n") + end - sorted_arguments =****@argum*****_by(&:first) - sorted_arguments.each do |name, value| - case name - when :table - case elasticsearch_version - when 7 - header = { - "index" => { - "_index" => value.downcase, - "_type"=>"_doc", - } - } - when 8 - header = { - "index" => { - "_index" => value.downcase, - } - } - else - # Version 5.x or 6.x - header = { - "index" => { - "_index" => value.downcase, - "_type" => "groonga", - } - } - end - components << JSON.generate(header) - when :values - record = JSON.parse(value) + private + def elasticsearch_version(options) + options[:version] || 5 + end - if record[0].is_a?(::Array) - column_names = nil + def build_action(options) + index_name =****@comma***** + case elasticsearch_version(options) + when 5, 6 + { + "index" => { + "_index" => index_name, + "_type" => "groonga", + } + } + when 7 + { + "index" => { + "_index" => index_name, + "_type"=>"_doc", + } + } + else + { + "index" => { + "_index" => index_name, + } + } + end + end - record.each do |load_value| - if column_names.nil? - column_names = load_value - next - end - body = {} - column_values = load_value - column_names.zip(column_values) do |column_name, column_value| - body[column_name] = column_value - end - components << JSON.generate(body) - end + def each_source + columns =****@comma***** + values =****@comma***** || [] + values.each do |value| + if value.is_a?(::Array) + if columns.nil? + columns = value else - record.each do |load_value| - components << JSON.generate(load_value) - end + yield(Hash[columns.zip(value)]) end + else + yield(value) end end - components.join("\n") end end end Modified: test/command/test-base.rb (+50 -8) =================================================================== --- test/command/test-base.rb 2019-03-22 08:30:36 +0900 (ba50092) +++ test/command/test-base.rb 2019-03-26 05:24:32 +0900 (6bb8a79) @@ -134,13 +134,53 @@ select \\ end end + sub_test_case("no use :columns") do + def single_record + load = Groonga::Command::Load.new("load", + :table => "Site", + :values => <<-VALUES) + [ + ["_key", "title"] + ["http://example.org/","This is test record 1!"] + ] + VALUES + + expected = <<-OUTPUT.chomp +{"index":{"_index":"site","_type":"groonga"}} +{"_key":"http://example.org/","title":"This is test record 1!"} + OUTPUT + + assert_equal(expected, load.to_elasticsearch_format) + end + + def multiple_records + load = Groonga::Command::Load.new("load", + :table => "Site", + :columns => "_key,title", + :values => <<-VALUES) + [ + ["http://example.org/","This is test record 1!"], + ["http://example.net/","This is test record 2!"] + ] + VALUES + + expected = <<-OUTPUT.chomp +{"index":{"_index":"site","_type":"groonga"}} +{"_key":"http://example.org/","title":"This is test record 1!"} +{"_key":"http://example.net/","title":"This is test record 2!"} + OUTPUT + + assert_equal(expected, load.to_elasticsearch_format) + end + end + sub_test_case("single record") do def test_brackets_format - load = Groonga::Command::Base.new("load", + load = Groonga::Command::Load.new("load", :table => "Site", + :columns => "_key,title", :values => <<-VALUES) [ - ["_key","title"], ["http://example.org/","This is test record 1!"] ] VALUES @@ -154,7 +194,7 @@ select \\ end def test_curly_brackets_format - load = Groonga::Command::Base.new("load", + load = Groonga::Command::Load.new("load", :table => "Site", :values => <<-VALUES) [ @@ -173,11 +213,11 @@ select \\ sub_test_case("multiple records") do def test_brackets_format - load = Groonga::Command::Base.new("load", + load = Groonga::Command::Load.new("load", :table => "Site", + :columns => "_key,title", :values => <<-VALUES) [ - ["_key","title"], ["http://example.org/","This is test record 1!"], ["http://example.net/","This is test record 2!"] ] @@ -186,6 +226,7 @@ select \\ expected = <<-OUTPUT.chomp {"index":{"_index":"site","_type":"groonga"}} {"_key":"http://example.org/","title":"This is test record 1!"} +{"index":{"_index":"site","_type":"groonga"}} {"_key":"http://example.net/","title":"This is test record 2!"} OUTPUT @@ -193,7 +234,7 @@ select \\ end def test_curly_brackets_format - load = Groonga::Command::Base.new("load", + load = Groonga::Command::Load.new("load", :table => "Site", :values => <<-VALUES) [ @@ -205,6 +246,7 @@ select \\ expected = <<-OUTPUT.chomp {"index":{"_index":"site","_type":"groonga"}} {"_key":"http://example.org/","title":"This is test record 1!"} +{"index":{"_index":"site","_type":"groonga"}} {"_key":"http://example.net/","title":"This is test record 2!"} OUTPUT @@ -213,11 +255,11 @@ select \\ end def setup - @load = Groonga::Command::Base.new("load", + @load = Groonga::Command::Load.new("load", :table => "Site", + :columns =>"_key,title", :values => <<-VALUES) [ -["_key","title"], ["http://example.org/","This is test record 1!"] ] VALUES -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190326/14a5d6fd/attachment-0001.html>