Class: RSolr::Client
- Inherits:
-
Object
- Object
- RSolr::Client
- Defined in:
- lib/rsolr/client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #adapt_response(request, response) ⇒ Object
-
#add(doc, opts = {}) ⇒ Object
add
creates xml “add” documents and sends the xml data to theupdate
method. -
#base_request_uri ⇒ Object
returns the request uri object.
-
#base_uri ⇒ Object
returns the uri proxy if present, otherwise just the uri object.
- #build_paginated_request(page, per_page, path, opts) ⇒ Object
-
#build_request(path, opts) ⇒ Object
build_request
accepts a path and options hash, then prepares a normalized hash to return for sending to a solr connection driver. -
#commit(opts = {}) ⇒ Object
send “commit” xml with opts.
-
#delete_by_id(id, opts = {}) ⇒ Object
Delete one or many documents by id solr.delete_by_id 10 solr.delete_by_id([12, 41, 199]).
-
#delete_by_query(query, opts = {}) ⇒ Object
delete one or many documents by query.
- #execute(request_context) ⇒ Object
-
#initialize(connection, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#optimize(opts = {}) ⇒ Object
send “optimize” xml with opts.
-
#paginate(page, per_page, path, opts = nil) ⇒ Object
A paginated request method.
- #params_with_wt(params) ⇒ Object
- #retry_503?(request_context, response) ⇒ Boolean
-
#retry_after(response) ⇒ Object
Retry-After can be a relative number of seconds from now, or an RFC 1123 Date.
-
#rollback(opts = {}) ⇒ Object
send </rollback>.
-
#send_and_receive(path, opts) ⇒ Object
send_and_receive
is the main request method responsible for sending requests to theconnection
object. -
#update(opts = {}) ⇒ Object
POST XML messages to /update with optional params.
-
#xml ⇒ Object
shortcut to RSolr::Xml::Generator.
Constructor Details
#initialize(connection, options = {}) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rsolr/client.rb', line 20 def initialize connection, = {} @proxy = @uri = nil @connection = connection unless false === [:url] url = [:url] ? [:url].dup : 'http://127.0.0.1:8983/solr/' url << "/" unless url[-1] == ?/ @uri = RSolr::Uri.create url if [:proxy] proxy_url = [:proxy].dup proxy_url << "/" unless proxy_url.nil? or proxy_url[-1] == ?/ @proxy = RSolr::Uri.create proxy_url if proxy_url end end @options = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (protected)
converts the method name for the solr request handler path.
302 303 304 305 306 307 308 |
# File 'lib/rsolr/client.rb', line 302 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 |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
18 19 20 |
# File 'lib/rsolr/client.rb', line 18 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'lib/rsolr/client.rb', line 18 def @options end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
18 19 20 |
# File 'lib/rsolr/client.rb', line 18 def proxy @proxy end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
18 19 20 |
# File 'lib/rsolr/client.rb', line 18 def uri @uri end |
Class Method Details
.default_wt ⇒ Object
9 10 11 |
# File 'lib/rsolr/client.rb', line 9 def default_wt @default_wt || :ruby end |
.default_wt=(value) ⇒ Object
13 14 15 |
# File 'lib/rsolr/client.rb', line 13 def default_wt= value @default_wt = value end |
Instance Method Details
#adapt_response(request, response) ⇒ Object
This method will evaluate the :body value if the params.params == :ruby … otherwise, the body is returned as is. The return object has methods attached, :request and :response. These methods give you access to the original request and response from the connection.
adapt_response
will raise an InvalidRubyResponse if :wt == :ruby and the body couldn’t be evaluated.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/rsolr/client.rb', line 281 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 = 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) result = RSolr::HashWithResponse.new(request, response, result) end result end |
#add(doc, opts = {}) ⇒ Object
add
creates xml “add” documents and sends the xml data to the update
method
wiki.apache.org/solr/UpdateXmlMessages#add.2BAC8-update
single record: solr.update(:id=>1, :name=>‘one’)
update using an array
solr.update(
[{:id=>1, :name=>'one'}, {:id=>2, :name=>'two'}],
:add_attributes => {:boost=>5.0, :commitWithin=>10}
)
100 101 102 103 |
# File 'lib/rsolr/client.rb', line 100 def add doc, opts = {} add_attributes = opts.delete :add_attributes update opts.merge(:data => xml.add(doc, add_attributes)) end |
#base_request_uri ⇒ Object
returns the request uri object.
37 38 39 |
# File 'lib/rsolr/client.rb', line 37 def base_request_uri base_uri.request_uri if base_uri end |
#base_uri ⇒ Object
returns the uri proxy if present, otherwise just the uri object.
43 44 45 |
# File 'lib/rsolr/client.rb', line 43 def base_uri @proxy ? @proxy : @uri end |
#build_paginated_request(page, per_page, path, opts) ⇒ Object
262 263 264 265 266 267 268 269 |
# File 'lib/rsolr/client.rb', line 262 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 |
#build_request(path, opts) ⇒ Object
build_request
accepts a path and options hash, then prepares a normalized hash to return for sending to a solr connection driver. build_request
sets up the uri/query string and converts the data
arg to form-urlencoded, if the data
arg is a hash. returns a hash with the following keys:
:method
:params
:headers
:data
:uri
:path
:query
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/rsolr/client.rb', line 237 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 |
#commit(opts = {}) ⇒ Object
send “commit” xml with opts
wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22
109 110 111 112 |
# File 'lib/rsolr/client.rb', line 109 def commit opts = {} commit_attrs = opts.delete :commit_attributes update opts.merge(:data => xml.commit( commit_attrs )) end |
#delete_by_id(id, opts = {}) ⇒ Object
Delete one or many documents by id
solr.delete_by_id 10
solr.delete_by_id([12, 41, 199])
135 136 137 |
# File 'lib/rsolr/client.rb', line 135 def delete_by_id id, opts = {} update opts.merge(:data => xml.delete_by_id(id)) end |
#delete_by_query(query, opts = {}) ⇒ Object
delete one or many documents by query.
wiki.apache.org/solr/UpdateXmlMessages#A.22delete.22_by_ID_and_by_Query
solr.delete_by_query 'available:0'
solr.delete_by_query ['quantity:0', 'manu:"FQ"']
145 146 147 |
# File 'lib/rsolr/client.rb', line 145 def delete_by_query query, opts = {} update opts.merge(:data => xml.delete_by_query(query)) end |
#execute(request_context) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/rsolr/client.rb', line 180 def execute request_context raw_response = connection.execute self, request_context while retry_503?(request_context, raw_response) request_context[:retry_503] -= 1 sleep retry_after(raw_response) raw_response = connection.execute self, request_context end adapt_response(request_context, raw_response) unless raw_response.nil? end |
#optimize(opts = {}) ⇒ Object
send “optimize” xml with opts.
wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22
118 119 120 121 |
# File 'lib/rsolr/client.rb', line 118 def optimize opts = {} optimize_attributes = opts.delete :optimize_attributes update opts.merge(:data => xml.optimize(optimize_attributes)) end |
#paginate(page, per_page, path, opts = nil) ⇒ Object
A paginated request method. Converts the page and per_page arguments into “rows” and “start”.
59 60 61 62 63 64 |
# File 'lib/rsolr/client.rb', line 59 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 |
#params_with_wt(params) ⇒ Object
256 257 258 259 260 |
# File 'lib/rsolr/client.rb', line 256 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 |
#retry_503?(request_context, response) ⇒ Boolean
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rsolr/client.rb', line 193 def retry_503?(request_context, response) return false if response.nil? status = response[:status] && response[:status].to_i return false unless status == 503 retry_503 = request_context[:retry_503] return false unless retry_503 && retry_503 > 0 retry_after_limit = request_context[:retry_after_limit] || 1 retry_after = retry_after(response) return false unless retry_after && retry_after <= retry_after_limit true end |
#retry_after(response) ⇒ Object
Retry-After can be a relative number of seconds from now, or an RFC 1123 Date. If the latter, attempt to convert it to a relative time in seconds.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/rsolr/client.rb', line 207 def retry_after(response) retry_after = Array(response[:headers]['Retry-After'] || response[:headers]['retry-after']).flatten.first.to_s if retry_after =~ /\A[0-9]+\Z/ retry_after = retry_after.to_i else begin retry_after_date = DateTime.parse(retry_after) retry_after = retry_after_date.to_time - Time.now retry_after = nil if retry_after < 0 rescue ArgumentError => e retry_after = retry_after.to_i end end retry_after end |
#rollback(opts = {}) ⇒ Object
128 129 130 |
# File 'lib/rsolr/client.rb', line 128 def rollback opts = {} update opts.merge(:data => xml.rollback) end |
#send_and_receive(path, opts) ⇒ Object
send_and_receive
is the main request method responsible for sending requests to the connection
object.
“path” : A string value that usually represents a solr request handler “opts” : A hash, which can contain the following keys:
:method : required - the http method (:get, :post or :head)
:params : optional - the query string params in hash form
:data : optional - post data -- if a hash is given, it's sent as "application/x-www-form-urlencoded; charset=UTF-8"
:headers : optional - hash of request headers
All other options are passed right along to the connection’s send_and_receive
method (:get, :post, or :head)
send_and_receive
returns either a string or hash on a successful ruby request. When the :params => :ruby, the response will be a hash, else a string.
creates a request context hash, sends it to the connection’s execute
method which returns a simple hash, then passes the request/response into adapt_response
.
171 172 173 174 175 176 177 |
# File 'lib/rsolr/client.rb', line 171 def send_and_receive path, opts request_context = build_request path, opts [:open_timeout, :read_timeout, :retry_503, :retry_after_limit].each do |k| request_context[k] = @options[k] end execute request_context end |
#update(opts = {}) ⇒ Object
POST XML messages to /update with optional params.
wiki.apache.org/solr/UpdateXmlMessages#add.2BAC8-update
If not set, opts will be set to a hash with the key ‘Content-Type’ set to ‘text/xml’
opts
can/should contain:
:data - posted data
:headers - http headers
:params - solr query parameter hash
79 80 81 82 83 |
# File 'lib/rsolr/client.rb', line 79 def update opts = {} opts[:headers] ||= {} opts[:headers]['Content-Type'] ||= 'text/xml' post 'update', opts end |