mirror of https://github.com/apache/lucene.git
Modeled Solr's StandardRequestHandler (qt=standard) as Solr::Request::Standard (includes common, standard, and facet parameters, but not highlighting parameters).
git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@496202 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
077fae5c81
commit
cde074eec5
|
@ -15,4 +15,5 @@ require 'solr/request/base'
|
||||||
require 'solr/request/commit'
|
require 'solr/request/commit'
|
||||||
require 'solr/request/ping'
|
require 'solr/request/ping'
|
||||||
require 'solr/request/select'
|
require 'solr/request/select'
|
||||||
|
require 'solr/request/standard'
|
||||||
require 'solr/request/update'
|
require 'solr/request/update'
|
||||||
|
|
|
@ -14,11 +14,12 @@ require 'erb'
|
||||||
|
|
||||||
module Solr
|
module Solr
|
||||||
module Request
|
module Request
|
||||||
|
# "Abstract" base class, only useful with subclasses that add parameters
|
||||||
class Select < Solr::Request::Base
|
class Select < Solr::Request::Base
|
||||||
attr_accessor :query
|
attr_reader :query_type
|
||||||
|
|
||||||
def initialize(query)
|
def initialize(qt=nil)
|
||||||
@query = query
|
@query_type = qt
|
||||||
end
|
end
|
||||||
|
|
||||||
def response_format
|
def response_format
|
||||||
|
@ -30,15 +31,19 @@ module Solr
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
return {:q => query, :wt => 'ruby', :fl => '*,score'}
|
return {:qt => query_type, :wt => 'ruby'}
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
raw_params = self.to_hash
|
raw_params = self.to_hash
|
||||||
|
|
||||||
http_params = []
|
http_params = []
|
||||||
raw_params.each do |key,value|
|
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)}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
http_params.join("&")
|
http_params.join("&")
|
||||||
|
|
|
@ -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
|
||||||
|
end
|
|
@ -13,7 +13,7 @@
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'solr'
|
require 'solr'
|
||||||
|
|
||||||
class BadRequest < Solr::Request::Select
|
class BadRequest < Solr::Request::Standard
|
||||||
def response_format
|
def response_format
|
||||||
:invalid
|
:invalid
|
||||||
end
|
end
|
||||||
|
@ -51,7 +51,7 @@ class ServerTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_response_format
|
def test_invalid_response_format
|
||||||
request = BadRequest.new("invalid")
|
request = BadRequest.new(:query => "solr")
|
||||||
assert_raise(RuntimeError) do
|
assert_raise(RuntimeError) do
|
||||||
@connection.send(request)
|
@connection.send(request)
|
||||||
end
|
end
|
||||||
|
@ -62,7 +62,7 @@ class ServerTest < Test::Unit::TestCase
|
||||||
@connection.send(Solr::Request::AddDocument.new(doc))
|
@connection.send(Solr::Request::AddDocument.new(doc))
|
||||||
@connection.commit
|
@connection.commit
|
||||||
|
|
||||||
request = Solr::Request::Select.new 'ruby_t:ouch'
|
request = Solr::Request::Standard.new :query => 'ruby_t:ouch'
|
||||||
result = @connection.send(request)
|
result = @connection.send(request)
|
||||||
|
|
||||||
assert_match /puts/, result.raw_response
|
assert_match /puts/, result.raw_response
|
||||||
|
|
|
@ -20,7 +20,7 @@ class DocumentTest < Test::Unit::TestCase
|
||||||
doc = Solr::Document.new
|
doc = Solr::Document.new
|
||||||
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
||||||
assert_kind_of REXML::Element, doc.to_xml
|
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_s
|
doc.to_xml.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ class DocumentTest < Test::Unit::TestCase
|
||||||
doc = Solr::Document.new
|
doc = Solr::Document.new
|
||||||
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
||||||
doc << Solr::Field.new(:creator => 'Otis Gospodnetic')
|
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_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_bad_doc
|
def test_bad_doc
|
||||||
|
|
|
@ -28,22 +28,14 @@ class RequestTest < Test::Unit::TestCase
|
||||||
def test_add_doc_request
|
def test_add_doc_request
|
||||||
request = Solr::Request::AddDocument.new(:title => "title")
|
request = Solr::Request::AddDocument.new(:title => "title")
|
||||||
assert_equal "<add><doc><field name='title'>title</field></doc></add>", request.to_s
|
assert_equal "<add><doc><field name='title'>title</field></doc></add>", request.to_s
|
||||||
assert :xml, request.response_format
|
assert_equal :xml, request.response_format
|
||||||
assert 'update', request.handler
|
assert_equal 'update', request.handler
|
||||||
|
|
||||||
assert_raise(RuntimeError) do
|
assert_raise(RuntimeError) do
|
||||||
Solr::Request::AddDocument.new("invalid")
|
Solr::Request::AddDocument.new("invalid")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
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_request
|
def test_ping_request
|
||||||
request = Solr::Request::Ping.new
|
request = Solr::Request::Ping.new
|
||||||
assert_equal :xml, request.response_format
|
assert_equal :xml, request.response_format
|
||||||
|
|
|
@ -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
|
||||||
|
end
|
|
@ -5,3 +5,4 @@ require 'document_test'
|
||||||
require 'field_test'
|
require 'field_test'
|
||||||
require 'request_test'
|
require 'request_test'
|
||||||
require 'response_test'
|
require 'response_test'
|
||||||
|
require 'standard_request_test'
|
Loading…
Reference in New Issue