[Groonga-commit] ranguba/groonga-client-rails at 8231769 [master] Support output_columns

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 27 18:52:44 JST 2016


Kouhei Sutou	2016-03-27 18:52:44 +0900 (Sun, 27 Mar 2016)

  New Revision: 82317696751f5b3e6d4f19b0a046257c86c50046
  https://github.com/ranguba/groonga-client-rails/commit/82317696751f5b3e6d4f19b0a046257c86c50046

  Message:
    Support output_columns

  Added files:
    test/unit/searcher/select/output_columns_parameter_test.rb
  Modified files:
    lib/groonga/client/searcher/select/request.rb
    test/apps/rails4-mongoid/test/searchers/posts_searcher_test.rb

  Modified: lib/groonga/client/searcher/select/request.rb (+34 -0)
===================================================================
--- lib/groonga/client/searcher/select/request.rb    2016-03-27 18:50:49 +0900 (4e4edb0)
+++ lib/groonga/client/searcher/select/request.rb    2016-03-27 18:52:44 +0900 (9f9f6ad)
@@ -47,6 +47,11 @@ module Groonga
                           RequestParameter.new(:query, value))
           end
 
+          def output_columns(value)
+            add_parameter(OverwriteMerger,
+                          OutputColumnsParameter.new(value))
+          end
+
           private
           def create_request(parameters)
             self.class.new(parameters)
@@ -94,6 +99,35 @@ module Groonga
             end
           end
         end
+
+        # @private
+        class OutputColumnsParameter
+          def initialize(output_columns)
+            @output_columns = output_columns
+          end
+
+          def to_parameters
+            if @output_columns.blank?
+              {}
+            else
+              case @output_columns
+              when ::Array
+                output_columns = @output_columns.join(", ")
+              when Symbol
+                output_columns = @output_columns.to_s
+              else
+                output_columns = @output_columns
+              end
+              parameters = {
+                output_columns: output_columns,
+              }
+              if output_columns.include?("(")
+                parameters[:command_version] = "2"
+              end
+              parameters
+            end
+          end
+        end
       end
     end
   end

  Modified: test/apps/rails4-mongoid/test/searchers/posts_searcher_test.rb (+42 -0)
===================================================================
--- test/apps/rails4-mongoid/test/searchers/posts_searcher_test.rb    2016-03-27 18:50:49 +0900 (9b6df87)
+++ test/apps/rails4-mongoid/test/searchers/posts_searcher_test.rb    2016-03-27 18:52:44 +0900 (98d3330)
@@ -21,4 +21,46 @@ class PostsSearcherTest < ActionController::TestCase
     assert_equal(["Hello World"],
                  result_set.records.collect {|record| record["body"]})
   end
+
+  test "should support snippet_html in output_columns" do
+    create(:post, body: "Hello World")
+    create(:post, body: "Hi Rails! Hello!")
+    result_set = @searcher.
+      search.
+      query("Hello").
+      output_columns("snippet_html(body)").
+      result_set
+    snippet_htmls = result_set.records.collect do |record|
+      record["snippet_html"]
+    end
+    assert_equal([
+                   ["<span class=\"keyword\">Hello</span> World"],
+                   ["Hi Rails! <span class=\"keyword\">Hello</span>!"],
+                 ],
+                 snippet_htmls)
+  end
+
+  test "should support Array for output_columns" do
+    post = create(:post, body: "Hello World")
+    result_set = @searcher.
+      search.
+      query("World").
+      output_columns(["_key", "body"]).
+      result_set
+    data = result_set.records.collect do |record|
+      [
+        record["_id"],
+        record["_key"],
+        record["body"],
+      ]
+    end
+    assert_equal([
+                   [
+                     nil,
+                     "#{post.class}-#{post.id}",
+                     "Hello World",
+                   ],
+                 ],
+                 data)
+  end
 end

  Added: test/unit/searcher/select/output_columns_parameter_test.rb (+68 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/searcher/select/output_columns_parameter_test.rb    2016-03-27 18:52:44 +0900 (2517bdd)
@@ -0,0 +1,68 @@
+# Copyright (C) 2016  Kouhei Sutou <kou �� clear-code.com>
+#
+# 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
+
+require "test_helper"
+
+class SearcherSelectOutputColumnsParameterTest < Test::Unit::TestCase
+  def output_columns_parameter(output_columns)
+    Groonga::Client::Searcher::Select::OutputColumnsParameter.new(output_columns)
+  end
+
+  def test_nil
+    assert_equal({},
+                 output_columns_parameter(nil).to_parameters)
+  end
+
+  def test_string
+    assert_equal({
+                   :output_columns => "title",
+                 },
+                 output_columns_parameter("title").to_parameters)
+  end
+
+  def test_empty_string
+    assert_equal({},
+                 output_columns_parameter("").to_parameters)
+  end
+
+  def test_symbol
+    assert_equal({
+                   :output_columns => "title",
+                 },
+                 output_columns_parameter(:title).to_parameters)
+  end
+
+  def test_array
+    assert_equal({
+                   :output_columns => "title, body",
+                 },
+                 output_columns_parameter(["title", "body"]).to_parameters)
+  end
+
+  def test_empty_array
+    assert_equal({},
+                 output_columns_parameter([]).to_parameters)
+  end
+
+  def test_function
+    parameter = output_columns_parameter(["title", "snippet_html(body)"])
+    assert_equal({
+                   :output_columns => "title, snippet_html(body)",
+                   :command_version => "2",
+                 },
+                 parameter.to_parameters)
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index