[Groonga-commit] groonga/groonga-command at da2f603 [master] Add Base#to_elasticsearch_format (#10)

アーカイブの一覧に戻る
Yasuhiro Horimoto null+****@clear*****
Wed Mar 20 16:52:53 JST 2019


Yasuhiro Horimoto	2019-03-20 16:52:53 +0900 (Wed, 20 Mar 2019)

  Revision: da2f603b20124be3f4f9fec8a76e211c02ceec79
  https://github.com/groonga/groonga-command/commit/da2f603b20124be3f4f9fec8a76e211c02ceec79

  Message:
    Add Base#to_elasticsearch_format (#10)
    
    * Add Base#to_elasticsearch_format
    
    This commit convert Groonga's dump format to Elasticsearch's import
    format.
    
    * Fix a indent
    
    * Reduce calling JSON.parse(value)
    
    * Reduce escapes
    
    * Add option for determine elasticsearch version
    
    Because the Elasticsearch handles to _type to differ depending on version.
    
    * Remove needless clause
    
    * Use parameterized test
    
    * Remove needless require
    
    * Use more suitable method name
    
    * Remove needless define of a variable
    
    * Use "{}" literal instead of "Hash.new"
    
    * Set a provide the default version when a user doesn't specify the elasticsearch_version option
    
    * Use more readable code for Hash literal
    
    * Use more suitable a name of variable
    
    * Simplify
    
    * Use more meaningful name
    
    * Simplify
    
    * Simplify
    
    * Reduce scope
    
    * Use more meaningful initial value
    
    * Simplify
    
    * Simplify
    
    * Simplify
    
    * Simplify
    
    * Use more readable code
    
    * Use whitespace as word separator
    
    * Add a whitespace around "=>"
    
    * Modify that doesn't change an expected value in a test
    
    * Use normal test style
    
    * Add an empty line between license header and code
    
    * Remove needless initialize
    
    * Fix a indent

  Added files:
    lib/groonga/command/format/elasticsearch.rb
  Modified files:
    lib/groonga/command/base.rb
    test/command/test-base.rb

  Modified: lib/groonga/command/base.rb (+7 -0)
===================================================================
--- lib/groonga/command/base.rb    2019-03-19 14:07:42 +0900 (96e2ca3)
+++ lib/groonga/command/base.rb    2019-03-20 16:52:53 +0900 (eb4185c)
@@ -1,4 +1,5 @@
 # Copyright (C) 2012-2017  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2019 Yasuhiro Horimoto <horim****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -16,6 +17,7 @@
 
 require "groonga/command/format/uri"
 require "groonga/command/format/command"
+require "groonga/command/format/elasticsearch"
 
 module Groonga
   module Command
@@ -153,6 +155,11 @@ module Groonga
         format.command_line(options)
       end
 
+      def to_elasticsearch_format(options={})
+        format = Format::Elasticsearch.new(@command_name, normalized_arguments)
+        format.json(options)
+      end
+
       def to_s
         if uri_format?
           to_uri_format

  Added: lib/groonga/command/format/elasticsearch.rb (+90 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/command/format/elasticsearch.rb    2019-03-20 16:52:53 +0900 (f72e1e2)
@@ -0,0 +1,90 @@
+# Copyright (C) 2019  Yasuhiro Horimoto <horim****@clear*****>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+module Groonga
+  module Command
+    module Format
+      class Elasticsearch
+        def initialize(name, arguments)
+          @name = name
+          @arguments = arguments
+        end
+
+        def json(options={})
+          return nil unless @name == "load"
+
+          components = []
+          elasticsearch_version = options[:version] || 5
+
+          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
+              body = {}
+              record = JSON.parse(value)
+
+              if record[0].is_a?(::Array)
+                column_names = nil
+
+                record.each do |load_value|
+                  if column_names.nil?
+                    column_names = load_value
+                    next
+                  end
+                  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
+              else
+                record.each do |load_value|
+                  components << JSON.generate(load_value)
+                end
+              end
+            end
+          end
+          components.join("\n")
+        end
+      end
+    end
+  end
+end

  Modified: test/command/test-base.rb (+132 -0)
===================================================================
--- test/command/test-base.rb    2019-03-19 14:07:42 +0900 (c021f82)
+++ test/command/test-base.rb    2019-03-20 16:52:53 +0900 (ba50092)
@@ -122,6 +122,138 @@ select \\
     end
   end
 
+  class CovnertToElasticsearchFormatTest < self
+    sub_test_case("non target command") do
+      def test_select_command
+        select = Groonga::Command::Base.new("select",
+                                            :table => "Users",
+                                            :filter => "age<=30",
+                                            :output_type => "json")
+        assert_equal(nil,
+                     select.to_elasticsearch_format)
+      end
+    end
+
+    sub_test_case("single record") do
+      def test_brackets_format
+        load = Groonga::Command::Base.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 test_curly_brackets_format
+        load = Groonga::Command::Base.new("load",
+                                          :table => "Site",
+                                          :values => <<-VALUES)
+  [
+  {"_key": "http://example.org/", "title": "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
+    end
+
+    sub_test_case("multiple records") do
+      def test_brackets_format
+        load = Groonga::Command::Base.new("load",
+                                          :table => "Site",
+                                          :values => <<-VALUES)
+  [
+  ["_key","title"],
+  ["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
+
+      def test_curly_brackets_format
+        load = Groonga::Command::Base.new("load",
+                                          :table => "Site",
+                                          :values => <<-VALUES)
+  [
+  {"_key": "http://example.org/", "title": "This is test record 1!"},
+  {"_key": "http://example.net/", "title": "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
+
+    def setup
+      @load = Groonga::Command::Base.new("load",
+                                         :table => "Site",
+                                         :values => <<-VALUES)
+[
+["_key","title"],
+["http://example.org/","This is test record 1!"]
+]
+      VALUES
+    end
+
+    sub_test_case(":version") do
+      def test_5
+        assert_equal(<<-REQUESTS.chomp, @load.to_elasticsearch_format(:version => 5))
+{"index":{"_index":"site","_type":"groonga"}}
+{"_key":"http://example.org/","title":"This is test record 1!"}
+        REQUESTS
+      end
+
+      def test_6
+        assert_equal(<<-REQUESTS.chomp, @load.to_elasticsearch_format(:version => 6))
+{"index":{"_index":"site","_type":"groonga"}}
+{"_key":"http://example.org/","title":"This is test record 1!"}
+        REQUESTS
+      end
+
+      def test_7
+        assert_equal(<<-REQUESTS.chomp, @load.to_elasticsearch_format(:version => 7))
+{"index":{"_index":"site","_type":"_doc"}}
+{"_key":"http://example.org/","title":"This is test record 1!"}
+        REQUESTS
+      end
+
+      def test_8
+        assert_equal(<<-REQUESTS.chomp, @load.to_elasticsearch_format(:version => 8))
+{"index":{"_index":"site"}}
+{"_key":"http://example.org/","title":"This is test record 1!"}
+        REQUESTS
+      end
+    end
+  end
+
   sub_test_case("#to_s") do
     def setup
       @table = "Users"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190320/d1d8b6d5/attachment-0001.html>


More information about the Groonga-commit mailing list
アーカイブの一覧に戻る