mirror of https://github.com/apache/lucene.git
move core ruby-solr API to client/ruby/solrb
git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@492143 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4d9c4504e1
commit
d57c97bdd7
|
@ -1,15 +0,0 @@
|
||||||
# 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 'solr/request'
|
|
||||||
require 'solr/connection'
|
|
||||||
require 'solr/response'
|
|
|
@ -1,33 +0,0 @@
|
||||||
# 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 'net/http'
|
|
||||||
|
|
||||||
module Solr
|
|
||||||
class Connection
|
|
||||||
attr_reader :url
|
|
||||||
|
|
||||||
def initialize(url)
|
|
||||||
@url = URI.parse(url)
|
|
||||||
end
|
|
||||||
|
|
||||||
def send(request)
|
|
||||||
post = Net::HTTP::Post.new(request.url_path)
|
|
||||||
post.body = request.to_http_body
|
|
||||||
post.content_type = 'application/x-www-form-urlencoded; charset=utf-8'
|
|
||||||
response = Net::HTTP.start(@url.host, @url.port) do |http|
|
|
||||||
http.request(post)
|
|
||||||
end
|
|
||||||
return request.response_format == :ruby ? RubyResponse.new(response.body) : XmlResponse.new(response.body)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,132 +0,0 @@
|
||||||
# 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 "erb"
|
|
||||||
include ERB::Util
|
|
||||||
|
|
||||||
module Solr
|
|
||||||
class Request
|
|
||||||
attr_reader :url_path
|
|
||||||
attr_reader :response_format
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@url_path = "/solr/select"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class UpdateRequest < Request
|
|
||||||
# sent to /solr/update with XML body
|
|
||||||
def initialize(body)
|
|
||||||
@body = body.to_s
|
|
||||||
@url_path = "/solr/update"
|
|
||||||
@response_format = :xml
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_http_body
|
|
||||||
@body
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class AddDocumentRequest < UpdateRequest
|
|
||||||
def initialize(doc_hash)
|
|
||||||
xml = REXML::Element.new('add')
|
|
||||||
|
|
||||||
doc = REXML::Element.new('doc')
|
|
||||||
|
|
||||||
doc_hash.each do |key,value|
|
|
||||||
#TODO: add handling of array values
|
|
||||||
doc.add_element field(key.to_s, value)
|
|
||||||
end
|
|
||||||
|
|
||||||
xml.add_element doc
|
|
||||||
super(xml.to_s)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def field(name, value)
|
|
||||||
field = REXML::Element.new("field")
|
|
||||||
field.add_attribute("name", name)
|
|
||||||
field.add_text(value)
|
|
||||||
|
|
||||||
field
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class SelectRequest < Request
|
|
||||||
# sent to /solr/select, with url query string parameters in the body
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@response_format = :ruby
|
|
||||||
super
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_http_body
|
|
||||||
raw_params = self.to_hash
|
|
||||||
|
|
||||||
http_params = []
|
|
||||||
raw_params.each do |key,value|
|
|
||||||
#TODO: Add array value handling
|
|
||||||
http_params << "#{key}=#{url_encode(value)}" if value
|
|
||||||
end
|
|
||||||
|
|
||||||
http_params.join("&")
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
{:wt => "ruby"}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class CommonRequestBase < SelectRequest
|
|
||||||
# supported by both standard and dismax request handlers
|
|
||||||
# start
|
|
||||||
# rows
|
|
||||||
# filter query (multiple)
|
|
||||||
# field list
|
|
||||||
attr_accessor :start
|
|
||||||
attr_accessor :rows
|
|
||||||
attr_accessor :filter_queries
|
|
||||||
attr_accessor :field_list
|
|
||||||
|
|
||||||
# debug
|
|
||||||
# explainOther
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
{:start => @start,
|
|
||||||
:rows => @rows,
|
|
||||||
:fq => @filter_queries,
|
|
||||||
:fl => @field_list}.merge(super)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class StandardRequest < CommonRequestBase
|
|
||||||
# sort
|
|
||||||
# default field
|
|
||||||
# query
|
|
||||||
# query operator (AND/OR)
|
|
||||||
attr_accessor :sort
|
|
||||||
attr_accessor :default_field
|
|
||||||
attr_accessor :query
|
|
||||||
attr_accessor :operator
|
|
||||||
|
|
||||||
def to_hash
|
|
||||||
{:df => @default_field,
|
|
||||||
:q => @sort ? "#{@query};#{@sort}" : @query,
|
|
||||||
:op => @operator}.merge(super)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
#s = Solr::Request.new("http://localhost:8983")
|
|
||||||
#s.add({:title => "foo"})
|
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
# 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
|
|
||||||
class Response
|
|
||||||
attr_reader :header, :raw_response, :data
|
|
||||||
def initialize(body)
|
|
||||||
@raw_response = body
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class RubyResponse < Response
|
|
||||||
def initialize(body)
|
|
||||||
super(body)
|
|
||||||
parsed_response = eval(body)
|
|
||||||
@header = parsed_response['responseHeader']
|
|
||||||
@data = parsed_response['response']
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class XmlResponse < Response
|
|
||||||
def initialize(body)
|
|
||||||
super(body)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,22 +0,0 @@
|
||||||
# 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 File.dirname(__FILE__) + '/../test_helper'
|
|
||||||
|
|
||||||
class ConnectionTest < Test::Unit::TestCase
|
|
||||||
def test_connection_initialize
|
|
||||||
request = Solr::UpdateRequest.new("<commit/>")
|
|
||||||
connection = Solr::Connection.new("http://localhost:8983")
|
|
||||||
assert_equal("localhost", connection.url.host)
|
|
||||||
assert_equal(8983, connection.url.port)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,33 +0,0 @@
|
||||||
# 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 File.dirname(__FILE__) + '/../test_helper'
|
|
||||||
|
|
||||||
class RequestTest < Test::Unit::TestCase
|
|
||||||
def test_basic_params
|
|
||||||
request = Solr::StandardRequest.new
|
|
||||||
assert_equal("/solr/select", request.url_path)
|
|
||||||
|
|
||||||
request.query = "term"
|
|
||||||
assert_equal "term", request.to_hash[:q]
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_update_request
|
|
||||||
request = Solr::UpdateRequest.new("<commit/>")
|
|
||||||
assert_equal("/solr/update", request.url_path)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_add_doc_request
|
|
||||||
request = Solr::AddDocumentRequest.new({:title => "title"})
|
|
||||||
assert_equal("<add><doc><field name='title'>title</field></doc></add>", request.to_http_body)
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue