Apache Solr open-source search software
Modeled Solr's StandardRequestHandler (qt=standard) as Solr::Request::Standard (includes common, standard, and facet parameters, but not highlighting parameters).
Erik Hatchercde074eec598
diff --git a/client/ruby/solrb/lib/solr/request.rb b/client/ruby/solrb/lib/solr/request.rbindex 457c6753ae8..01795ea1605 100755--- a/client/ruby/solrb/lib/solr/request.rb+++ b/client/ruby/solrb/lib/solr/request.rb@@ -15,4 +15,5 @@ require 'solr/request/base'require 'solr/request/commit'require 'solr/request/ping'require 'solr/request/select'+require 'solr/request/standard'require 'solr/request/update'diff --git a/client/ruby/solrb/lib/solr/request/select.rb b/client/ruby/solrb/lib/solr/request/select.rbindex 3ce0c4f4c9d..e4ddabd577a 100644--- a/client/ruby/solrb/lib/solr/request/select.rb+++ b/client/ruby/solrb/lib/solr/request/select.rb@@ -14,11 +14,12 @@ require 'erb'module Solrmodule Request+ # "Abstract" base class, only useful with subclasses that add parametersclass Select < Solr::Request::Base- attr_accessor :query-- def initialize(query)- @query = query+ attr_reader :query_type++ def initialize(qt=nil)+ @query_type = qtenddef response_format@@ -30,15 +31,19 @@ module Solrenddef to_hash- return {:q => query, :wt => 'ruby', :fl => '*,score'}+ return {:qt => query_type, :wt => 'ruby'}end-+def to_sraw_params = self.to_hashhttp_params = []raw_params.each do |key,value|- http_params << "#{key}=#{ERB::Util::url_encode(value)}" if value+ if value.respond_to? :each+ value.each { |v| http_params << "#{key}=#{ERB::Util::url_encode(v)}" }+ else+ http_params << "#{key}=#{ERB::Util::url_encode(value)}"+ endendhttp_params.join("&")diff --git a/client/ruby/solrb/lib/solr/request/standard.rb b/client/ruby/solrb/lib/solr/request/standard.rbnew file mode 100755index 00000000000..e1a1f50e932--- /dev/null+++ b/client/ruby/solrb/lib/solr/request/standard.rb@@ -0,0 +1,92 @@+# The ASF licenses this file to You under the Apache License, Version 2.0+# (the "License"); you may not use this file except in compliance with+# the License. You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++module Solr+ module Request+ class Standard < Solr::Request::Select+ def initialize(params)+ super('standard')++ raise ":query parameter required" unless params[:query]++ # devour StandardRequestHandler params+ @query = params.delete(:query)+ @sort = params.delete(:sort) # TODO add validation such that only :ascending and :descending are supported+ @default_field = params.delete(:default_field)+ @operator = params.delete(:operator)+ @operator = @operator == :and ? "AND" : "OR" if @operator # TODO add validation such that only :and or :or are supported++ # devour common parameters+ @start = params.delete(:start) # TODO validate integer+ @rows = params.delete(:rows) # TODO validate integer+ @filter_queries = params.delete(:filter_queries)+ @field_list = params.delete(:field_list) || ["*","score"]+ @debug_query = params.delete(:debug_query)+ @explain_other = params.delete(:explain_other)++ # devour faceting parameters+ @facets = params.delete(:facets)++ #TODO model highlighting parameters: http://wiki.apache.org/solr/HighlightingParameters++ raise "Invalid parameters: #{params.keys.join(',')}" if params.size > 0+ end++ def to_hash+ hash = {}++ # standard request param processing+ sort = @sort.collect do |sort|+ key = sort.keys[0]+ "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"+ end.join(',') if @sort+ q = sort ? "#{@query};#{sort}" : @query+ hash[:q] = q+ hash[:"q.op"] = @operator+ hash[:df] = @default_field++ # common parameter processing+ hash[:start] = @start+ hash[:rows] = @rows+ hash[:fq] = @filter_queries+ hash[:fl] = @field_list.join(',')+ hash[:debugQuery] = @debug_query+ hash[:explainOther] = @explain_other++ # facet parameter processing+ if @facets+ hash[:facet] = true+ hash[:"facet.field"] = []+ hash[:"facet.query"] = @facets[:queries]+ hash[:"facet.missing"] = @facets[:missing]+ hash[:"facet.zeros"] = @facets[:zeros]+ hash[:"facet.limit"] = @facets[:limit]+ @facets[:fields].each do |f|+ if f.kind_of? Hash+ key = f.keys[0]+ value = f[key]+ hash[:"facet.field"] << key+ hash[:"f.#{key}.facet.missing"] = value[:missing]+ hash[:"f.#{key}.facet.zeros"] = value[:zeros]+ hash[:"f.#{key}.facet.limit"] = value[:limit]+ else+ hash[:"facet.field"] << f+ end+ end+ end++ hash.merge(super.to_hash)+ end++ end+ end+enddiff --git a/client/ruby/solrb/test/functional/server_test.rb b/client/ruby/solrb/test/functional/server_test.rbindex 2fd3b5fce9c..8e192b7fadb 100644--- a/client/ruby/solrb/test/functional/server_test.rb+++ b/client/ruby/solrb/test/functional/server_test.rb@@ -13,7 +13,7 @@require 'test/unit'require 'solr'-class BadRequest < Solr::Request::Select+class BadRequest < Solr::Request::Standarddef response_format:invalidend@@ -51,7 +51,7 @@ class ServerTest < Test::Unit::TestCaseenddef test_invalid_response_format- request = BadRequest.new("invalid")+ request = BadRequest.new(:query => "solr")assert_raise(RuntimeError) do@connection.send(request)end@@ -62,7 +62,7 @@ class ServerTest < Test::Unit::TestCase@connection.send(Solr::Request::AddDocument.new(doc))@connection.commit- request = Solr::Request::Select.new 'ruby_t:ouch'+ request = Solr::Request::Standard.new :query => 'ruby_t:ouch'result = @connection.send(request)assert_match /puts/, result.raw_responsediff --git a/client/ruby/solrb/test/unit/document_test.rb b/client/ruby/solrb/test/unit/document_test.rbindex 4bb01fe466b..c0cbf881a7b 100644--- a/client/ruby/solrb/test/unit/document_test.rb+++ b/client/ruby/solrb/test/unit/document_test.rb@@ -20,7 +20,7 @@ class DocumentTest < Test::Unit::TestCasedoc = Solr::Document.newdoc << Solr::Field.new(:creator => 'Erik Hatcher')assert_kind_of REXML::Element, doc.to_xml- assert "<doc><field name='creator'>Erik Hatcher</field></doc>",+ assert_equal "<doc><field name='creator'>Erik Hatcher</field></doc>",doc.to_xml.to_send@@ -28,7 +28,7 @@ class DocumentTest < Test::Unit::TestCasedoc = Solr::Document.newdoc << Solr::Field.new(:creator => 'Erik Hatcher')doc << Solr::Field.new(:creator => 'Otis Gospodnetic')- assert "<doc><field name='creator'>Erik Hatcher</field><field name='creator'>Otis Gospodnetic</field></doc>", doc.to_xml.to_s+ assert_equal "<doc><field name='creator'>Erik Hatcher</field><field name='creator'>Otis Gospodnetic</field></doc>", doc.to_xml.to_senddef test_bad_docdiff --git a/client/ruby/solrb/test/unit/request_test.rb b/client/ruby/solrb/test/unit/request_test.rbindex 4b0452dea15..be7e8f3c506 100755--- a/client/ruby/solrb/test/unit/request_test.rb+++ b/client/ruby/solrb/test/unit/request_test.rb@@ -28,22 +28,14 @@ class RequestTest < Test::Unit::TestCasedef test_add_doc_requestrequest = Solr::Request::AddDocument.new(:title => "title")assert_equal "<add><doc><field name='title'>title</field></doc></add>", request.to_s- assert :xml, request.response_format- assert 'update', request.handler+ assert_equal :xml, request.response_format+ assert_equal 'update', request.handlerassert_raise(RuntimeError) doSolr::Request::AddDocument.new("invalid")endend- def test_select_request- request = Solr::Request::Select.new('belkin')- assert_equal :ruby, request.response_format- assert 'select', request.handler- assert 'belkin', request.to_hash['q']- assert_match /q=belkin/, request.to_s- end-def test_ping_requestrequest = Solr::Request::Ping.newassert_equal :xml, request.response_formatdiff --git a/client/ruby/solrb/test/unit/standard_request_test.rb b/client/ruby/solrb/test/unit/standard_request_test.rbnew file mode 100755index 00000000000..c536ff01190--- /dev/null+++ b/client/ruby/solrb/test/unit/standard_request_test.rb@@ -0,0 +1,69 @@+# The ASF licenses this file to You under the Apache License, Version 2.0+# (the "License"); you may not use this file except in compliance with+# the License. You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++require 'test/unit'+require 'solr'++class StandardRequestTest < Test::Unit::TestCase++ def test_basic_query+ request = Solr::Request::Standard.new(:query => 'query')+ assert_equal :ruby, request.response_format+ assert_equal 'select', request.handler+ assert_equal 'query', request.to_hash[:q]+ assert_match /q=query/, request.to_s+ end++ def test_bad_params+ assert_raise(RuntimeError) do+ Solr::Request::Standard.new(:foo => "invalid")+ end++ assert_raise(RuntimeError) do+ Solr::Request::Standard.new(:query => "valid", :foo => "invalid")+ end+ end++ def test_common_params+ request = Solr::Request::Standard.new(:query => 'query', :start => 10, :rows => 50,+ :filter_queries => ['fq1', 'fq2'], :field_list => ['id','title','score'])+ assert_equal 10, request.to_hash[:start]+ assert_equal 50, request.to_hash[:rows]+ assert_equal ['fq1','fq2'], request.to_hash[:fq]+ assert_equal "id,title,score", request.to_hash[:fl]+ end++ def test_facet_params+ request = Solr::Request::Standard.new(:query => 'query',+ :facets => {+ :fields => [:genre,+ {:year => {:limit => 50, :zeros => false, :missing => false}}], # field that overrides the global facet parameters+ :queries => ["q1", "q2"],+ :limit => 5, :zeros => true, :missing => true # global facet parameters+ }+ )+ assert_equal true, request.to_hash[:facet]+ assert_equal [:genre, :year], request.to_hash[:"facet.field"]+ assert_equal ["q1", "q2"], request.to_hash[:"facet.query"]+ assert_equal true, request.to_hash[:"facet.missing"]+ assert_equal 5, request.to_hash[:"facet.limit"]+ assert_equal true, request.to_hash[:"facet.zeros"]+ assert_equal 50, request.to_hash[:"f.year.facet.limit"]+ assert_equal false, request.to_hash[:"f.year.facet.zeros"]+ assert_equal false, request.to_hash[:"f.year.facet.missing"]+ end++ def test_basic_sort+ request = Solr::Request::Standard.new(:query => 'query', :sort => [{:title => :descending}])+ assert_equal 'query;title desc', request.to_hash[:q]+ end+enddiff --git a/client/ruby/solrb/test/unit/suite.rb b/client/ruby/solrb/test/unit/suite.rbindex 54795a6e3f5..2e93c75f330 100755--- a/client/ruby/solrb/test/unit/suite.rb+++ b/client/ruby/solrb/test/unit/suite.rb@@ -5,3 +5,4 @@ require 'document_test'require 'field_test'require 'request_test'require 'response_test'+require 'standard_request_test'\ No newline at end of file