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 result = if respond_to? "evaluate_#{request[:params][:wt]}_response", true send "evaluate_#{request[:params][:wt]}_response", request, response else response[:body] end if result.is_a?(Hash) || request[:method] == :head result = RSolr::HashWithResponse.new(request, response, result) end result end
def add doc, opts = {}
)
:add_attributes => {:boost=>5.0, :commitWithin=>10}
[{:id=>1, :name=>'one'}, {:id=>2, :name=>'two'}],
solr.add(
add using an array
solr.add(: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 => builder.add(doc, add_attributes)) end
def base_request_uri
def base_request_uri base_uri.request_uri if base_uri end
def base_uri
def base_uri @uri end
def build_paginated_request page, per_page, path, opts
def build_paginated_request page, per_page, path, opts per_page = per_page.to_s.to_i page = page.to_s.to_i-1 page = page < 1 ? 0 : page opts[:params]["start"] = page * per_page opts[:params]["rows"] = per_page build_request path, opts 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] = params_with_wt(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; charset=UTF-8' end opts[:path] = path opts[:uri] = base_uri.merge(path.to_s + (query ? "?#{query}" : "")) if base_uri opts end
def builder
def builder @builder ||= if update_format.is_a? Class update_format.new elsif update_format == :json RSolr::JSON::Generator.new elsif update_format == :xml RSolr::Xml::Generator.new else update_format end 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 => builder.commit( commit_attrs )) end
def connection
def connection @connection ||= begin conn_opts = { request: {} } conn_opts[:url] = uri.to_s conn_opts[:proxy] = proxy if proxy conn_opts[:request][:open_timeout] = options[:open_timeout] if options[:open_timeout] if options[:read_timeout] || options[:timeout] # read_timeout was being passed to faraday as timeout since Rsolr 2.0, # it's now deprecated, just use `timeout` directly. conn_opts[:request][:timeout] = options[:timeout] || options[:read_timeout] end conn_opts[:request][:params_encoder] = Faraday::FlatParamsEncoder Faraday.new(conn_opts) do |conn| if uri.user && uri.password case Faraday::VERSION when /^0/ conn.basic_auth uri.user, uri.password when /^1/ conn.request :basic_auth, uri.user, uri.password else conn.request :authorization, :basic_auth, uri.user, uri.password end end conn.response :raise_error conn.request :retry, max: options[:retry_after_limit], interval: 0.05, interval_randomness: 0.5, backoff_factor: 2, exceptions: ['Faraday::Error', 'Timeout::Error'] if options[:retry_503] conn.adapter options[:adapter] || Faraday.default_adapter || :net_http end end end
def default_wt
def default_wt @default_wt ||= :json end
def default_wt
def default_wt self.options[:default_wt] || self.class.default_wt end
def default_wt= value
def default_wt= value @default_wt = value 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 => builder.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 => builder.delete_by_query(query)) end
def evaluate_json_response request, response
def evaluate_json_response request, response return if response[:body].nil? || response[:body].empty? JSON.parse response[:body].to_s rescue JSON::ParserError raise RSolr::Error::InvalidJsonResponse.new request, response 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
attempts to bring the ruby string to life.
evaluates the response[:body],
def evaluate_ruby_response request, response Kernel.eval response[:body].to_s rescue SyntaxError raise RSolr::Error::InvalidRubyResponse.new request, response end
def execute request_context
def execute request_context raw_response = begin response = connection.send(request_context[:method], request_context[:uri].to_s) do |req| req.body = request_context[:data] if request_context[:method] == :post and request_context[:data] req.headers.merge!(request_context[:headers]) if request_context[:headers] end { status: response.status.to_i, headers: response.headers, body: response.body.force_encoding('utf-8') } rescue Faraday::TimeoutError => e raise RSolr::Error::Timeout.new(request_context, e.response) rescue Errno::ECONNREFUSED, defined?(Faraday::ConnectionFailed) ? Faraday::ConnectionFailed : Faraday::Error::ConnectionFailed raise RSolr::Error::ConnectionRefused.new(request_context) rescue Faraday::Error => e raise RSolr::Error::Http.new(request_context, e.response) end adapt_response(request_context, raw_response) unless raw_response.nil? end
def extract_url_from_options(options)
def extract_url_from_options(options) url = options[:url] ? options[:url].dup : DEFAULT_URL url << "/" unless url[-1] == ?/ uri = ::URI.parse(url) # URI::HTTPS is a subclass of URI::HTTP, so this check accepts HTTP(S) raise ArgumentError, "You must provide an HTTP(S) url." unless uri.kind_of?(URI::HTTP) uri end
def initialize connection, options = {}
def initialize connection, options = {} @proxy = @uri = nil @connection = connection unless false === options[:url] @uri = extract_url_from_options(options) if options[:proxy] proxy_url = options[:proxy].dup proxy_url << "/" unless proxy_url.nil? or proxy_url[-1] == ?/ @proxy = ::URI.parse proxy_url if proxy_url elsif options[:proxy] == false @proxy = false # used to avoid setting the proxy from the environment. end end @update_format = options.delete(:update_format) || RSolr::JSON::Generator @update_path = options.fetch(:update_path, 'update') @options = options if options[:read_timeout] warn "DEPRECATION: Rsolr.new/connect option `read_timeout` is deprecated and will be removed in Rsolr 3. `timeout` is currently a synonym, use that instead." end end
def method_missing name, *args
def method_missing name, *args if name.to_s =~ /^paginated?_(.+)$/ paginate args[0], args[1], $1, *args[2..-1] else send_and_receive name, *args end 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 => builder.optimize(optimize_attributes)) end
def paginate page, per_page, path, opts = nil
Converts the page and per_page
A paginated request method.
def paginate page, per_page, path, opts = nil opts ||= {} opts[:params] ||= {} raise "'rows' or 'start' params should not be set when using +paginate+" if ["start", "rows"].include?(opts[:params].keys) execute build_paginated_request(page, per_page, path, opts) end
def params_with_wt(params)
def params_with_wt(params) return { wt: default_wt } if params.nil? return params if params.key?(:wt) || params.key?('wt') { wt: default_wt }.merge(params) end
def rollback opts = {}
http://wiki.apache.org/solr/UpdateXmlMessages#A.22rollback.22
send
def rollback opts = {} update opts.merge(:data => builder.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; charset=UTF-8"
:params : optional - the query string params in hash form
:method : required - the http method (:get, :post or :head)
"opts" : 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 soft_commit opts = {}
https://lucene.apache.org/solr/guide/updatehandlers-in-solrconfig.html#commit-and-softcommit
soft commit
def soft_commit opts = {} commit(opts.merge params: { softCommit: true }) 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'] ||= builder.content_type post opts.fetch(:path, update_path), opts end