class RSolr::Client
def adapt_response request, response
if :wt == :ruby and the body
+adapt_response+ will raise an InvalidRubyResponse
request and response from the connection.
These methods give you access to the original
The return object has methods attached, :request and :response.
... otherwise, the body is returned as is.
if the params[:uri].params[:wt] == :ruby
This method will evaluate the :body value
def adapt_response request, response raise "The response does not have the correct keys => :body, :headers, :status" unless %W(body headers status) == response.keys.map{|k|k.to_s}.sort raise RSolr::Error::Http.new request, response unless [200,302].include? response[:status] result = request[:params][:wt] == :ruby ? evaluate_ruby_response(request, response) : response[:body] result.extend Context result.request = request result.response = response result end
def add doc, opts = {}
)
:add_attributes => {:boost=>5.0, :commitWithin=>10}
[{:id=>1, :name=>'one'}, {:id=>2, :name=>'two'}],
solr.update(
update using an array
solr.update(:id=>1, :name=>'one')
single record:
http://wiki.apache.org/solr/UpdateXmlMessages#add.2BAC8-update
+add+ creates xml "add" documents and sends the xml data to the +update+ method
def add doc, opts = {} add_attributes = opts.delete :add_attributes update opts.merge(:data => xml.add(doc, add_attributes)) end
def base_request_uri
def base_request_uri base_uri.request_uri end
def base_uri
returns the uri proxy if present,
def base_uri @proxy || @uri end
def build_request path, opts
:path
:uri
:data
:headers
:params
:method
returns a hash with the following keys:
if the +data+ arg is a hash.
and converts the +data+ arg to form-urlencoded,
+build_request+ sets up the uri/query string
to a solr connection driver.
then prepares a normalized hash to return for sending
+build_request+ accepts a path and options hash,
def build_request path, opts raise "path must be a string or symbol, not #{path.inspect}" unless [String,Symbol].include?(path.class) path = path.to_s opts[:proxy] = proxy unless proxy.nil? opts[:method] ||= :get raise "The :data option can only be used if :method => :post" if opts[:method] != :post and opts[:data] opts[:params] = opts[:params].nil? ? {:wt => :ruby} : {:wt => :ruby}.merge(opts[:params]) query = RSolr::Uri.params_to_solr(opts[:params]) unless opts[:params].empty? opts[:query] = query if opts[:data].is_a? Hash opts[:data] = RSolr::Uri.params_to_solr opts[:data] opts[:headers] ||= {} opts[:headers]['Content-Type'] ||= 'application/x-www-form-urlencoded' end opts[:path] = path opts[:uri] = base_uri.merge(path.to_s + (query ? "?#{query}" : "")) if base_uri opts end
def commit opts = {}
http://wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22
send "commit" xml with opts
def commit opts = {} commit_attrs = opts.delete :commit_attributes update opts.merge(:data => xml.commit( commit_attrs )) end
def delete_by_id id, opts = {}
solr.delete_by_id 10
Delete one or many documents by id
def delete_by_id id, opts = {} update opts.merge(:data => xml.delete_by_id(id)) end
def delete_by_query query, opts = {}
solr.delete_by_query 'available:0'
http://wiki.apache.org/solr/UpdateXmlMessages#A.22delete.22_by_ID_and_by_Query
delete one or many documents by query.
def delete_by_query query, opts = {} update opts.merge(:data => xml.delete_by_query(query)) end
def evaluate_ruby_response request, response
instead, giving full access to the
RSolr::Error::InvalidRubyResponse
this method intercepts and raises a
If a SyntaxError is raised, then
attemps to bring the ruby string to life.
evaluates the response[:body],
def evaluate_ruby_response request, response begin Kernel.eval response[:body].to_s rescue SyntaxError raise RSolr::Error::InvalidRubyResponse.new request, response end end
def execute request_context
def execute request_context raw_response = connection.execute self, request_context adapt_response(request_context, raw_response) unless raw_response.nil? end
def initialize connection, options = {}
def initialize connection, options = {} @connection = connection url = options[:url] || 'http://127.0.0.1:8983/solr/' url << "/" unless url[-1] == ?/ proxy_url = options[:proxy] proxy_url << "/" unless proxy_url.nil? or proxy_url[-1] == ?/ @uri = RSolr::Uri.create url @proxy = RSolr::Uri.create proxy_url if proxy_url @options = options extend RSolr::Pagination::Client end
def method_missing name, *args
def method_missing name, *args send_and_receive name, *args end
def optimize opts = {}
http://wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22
send "optimize" xml with opts.
def optimize opts = {} optimize_attributes = opts.delete :optimize_attributes update opts.merge(:data => xml.optimize(optimize_attributes)) end
def rollback opts = {}
http://wiki.apache.org/solr/UpdateXmlMessages#A.22rollback.22
send
def rollback opts = {} update opts.merge(:data => xml.rollback) end
def send_and_receive path, opts
which returns a simple hash,
sends it to the connection's +execute+ method
creates a request context hash,
When the :params[:wt] => :ruby, the response will be a hash, else a string.
+send_and_receive+ returns either a string or hash on a successful ruby request.
All other options are passed right along to the connection's +send_and_receive+ method (:get, :post, or :head)
:headers : optional - hash of request headers
:data : optional - post data -- if a hash is given, it's sent as "application/x-www-form-urlencoded"
:params : optional - the query string params in hash form
:method : required - the http method (:get, :post or :head)
"opt" : A hash, which can contain the following keys:
"path" : A string value that usually represents a solr request handler
+send_and_receive+ is the main request method responsible for sending requests to the +connection+ object.
def send_and_receive path, opts request_context = build_request path, opts execute request_context end
def update opts = {}
:params - solr query parameter hash
:headers - http headers
:data - posted data
+opts+ can/should contain:
'Content-Type' set to 'text/xml'
If not set, opts[:headers] will be set to a hash with the key
http://wiki.apache.org/solr/UpdateXmlMessages#add.2BAC8-update
POST XML messages to /update with optional params.
def update opts = {} opts[:headers] ||= {} opts[:headers]['Content-Type'] ||= 'text/xml' post 'update', opts end
def xml
def xml @xml ||= RSolr::Xml::Generator.new end