HorimotoYasuhiro
null+****@clear*****
Fri May 26 19:20:00 JST 2017
HorimotoYasuhiro 2017-05-26 19:20:00 +0900 (Fri, 26 May 2017) New Revision: 1810e492ebf3bf5d41b0364a4c3695df6c91eb38 https://github.com/ranguba/groonga-client/commit/1810e492ebf3bf5d41b0364a4c3695df6c91eb38 Merged 96d42b5: Merge pull request #28 from komainu8/feature/add_scorer Message: Add scorer method in groonga-client. Added files: test/request/select/test-scorer.rb Modified files: lib/groonga/client/request/select.rb Modified: lib/groonga/client/request/select.rb (+41 -0) =================================================================== --- lib/groonga/client/request/select.rb 2017-04-27 17:07:39 +0900 (2603098) +++ lib/groonga/client/request/select.rb 2017-05-26 19:20:00 +0900 (a8dd1a7) @@ -143,6 +143,24 @@ module Groonga end end + def scorer(expression_or_column_name, values_or_value) + if expression_or_column_name.is_a?(::Symbol) + expression = "_score = %{column}" + column_name = expression_or_column_name + values = { column: column_name } + elsif expression_or_column_name.is_a?(::String) + expression = expression_or_column_name + unless expression.empty? + unless expression.start_with?("_score = ") + expression = "_score = " + expression + end + end + values = values_or_value + end + add_parameter(ScorerMerger, + ScorerExpressionParameter.new(expression, values)) + end + def output_columns(value) add_parameter(OverwriteMerger, OutputColumnsParameter.new("", value)) @@ -633,6 +651,23 @@ module Groonga end # @private + class ScorerMerger < ParameterMerger + def to_parameters + params1 =****@param*****_parameters + params2 =****@param*****_parameters + params = params1.merge(params2) + scorer1 = params1[:scorer] + scorer2 = params2[:scorer] + if scorer1 and scorer2 + params[:scorer] = "(#{scorer1}) && (#{scorer2})" + elsif scorer1 or scorer2 + params[:scorer] = (scorer1 || scorer2) + end + params + end + end + + # @private module ScriptSyntaxValueEscapable private def escape_script_syntax_value(value) @@ -729,6 +764,12 @@ module Groonga end end + class ScorerExpressionParameter < ScriptSyntaxExpressionParameter + def initialize(expression, values) + super(:scorer, expression, values) + end + end + # @private class OutputColumnsParameter < ValuesParameter def initialize(prefix, output_columns) Added: test/request/select/test-scorer.rb (+85 -0) 100644 =================================================================== --- /dev/null +++ test/request/select/test-scorer.rb 2017-05-26 19:20:00 +0900 (318e0ba) @@ -0,0 +1,85 @@ +# Copyright (C) 2016-2017 Yasuhiro Horimoto <horimoto �� 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 + +class TestRequestSelectScorer < Test::Unit::TestCase + setup do + @request = Groonga::Client::Request::Select.new("posts") + end + + def scorer(expression, values=nil) + @request.scorer(expression, values).to_parameters + end + + sub_test_case("expression") do + def test_nil + assert_equal({ + :table => "posts", + }, + scorer(nil)) + end + + def test_string + assert_equal({ + :table => "posts", + :scorer => "_score = age", + }, + scorer("_score = age")) + end + + def test_empty_string + assert_equal({ + :table => "posts", + }, + scorer("")) + end + + def test_symbol + assert_equal({ + :table => "posts", + :scorer => "_score = age", + }, + scorer(:age)) + end + end + + sub_test_case("values") do + test("Symbol") do + assert_equal({ + :table => "posts", + :scorer => "_score = age", + }, + scorer("_score = %{column}", :column => :age)) + end + + test("Symbols") do + assert_equal({ + :table => "posts", + :scorer => "_score = -geo_distance(location, \"35.68138194x139.766083888889\")", + }, + scorer("-geo_distance(%{column}, %{point})", + column: :location, point: "35.68138194x139.766083888889")) + end + + test("Numeric") do + assert_equal({ + :table => "posts", + :scorer => "_score = 29", + }, + scorer("_score = %{value}", + :value => 29)) + end + end +end -------------- next part -------------- HTML����������������������������...Download