Class: RSolr::Client
- Inherits:
-
Object
- Object
- RSolr::Client
- Defined in:
- lib/rsolr/client.rb
Defined Under Namespace
Modules: Context
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.
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 actual request uri object.
-
#base_uri ⇒ Object
returns the uri proxy if present, otherwise just the uri 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.
-
#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.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rsolr/client.rb', line 5 def initialize connection, = {} @connection = connection unless false === [:url] url = [:url] || 'http://127.0.0.1:8983/solr/' url << "/" unless url[-1] == ?/ proxy_url = [: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 end @options = extend RSolr::Pagination::Client 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.
221 222 223 |
# File 'lib/rsolr/client.rb', line 221 def method_missing name, *args send_and_receive name, *args end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/rsolr/client.rb', line 3 def connection @connection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/rsolr/client.rb', line 3 def @options end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
3 4 5 |
# File 'lib/rsolr/client.rb', line 3 def proxy @proxy end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
3 4 5 |
# File 'lib/rsolr/client.rb', line 3 def uri @uri 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.
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rsolr/client.rb', line 206 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 |
#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}
)
73 74 75 76 |
# File 'lib/rsolr/client.rb', line 73 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 actual request uri object.
20 21 22 |
# File 'lib/rsolr/client.rb', line 20 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.
26 27 28 |
# File 'lib/rsolr/client.rb', line 26 def base_uri @proxy || @uri 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rsolr/client.rb', line 169 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 |
#commit(opts = {}) ⇒ Object
send “commit” xml with opts
wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22
82 83 84 85 |
# File 'lib/rsolr/client.rb', line 82 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])
108 109 110 |
# File 'lib/rsolr/client.rb', line 108 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"']
118 119 120 |
# File 'lib/rsolr/client.rb', line 118 def delete_by_query query, opts = {} update opts.merge(:data => xml.delete_by_query(query)) end |
#execute(request_context) ⇒ Object
150 151 152 153 |
# File 'lib/rsolr/client.rb', line 150 def execute request_context raw_response = connection.execute self, request_context 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
91 92 93 94 |
# File 'lib/rsolr/client.rb', line 91 def optimize opts = {} optimize_attributes = opts.delete :optimize_attributes update opts.merge(:data => xml.optimize(optimize_attributes)) end |
#rollback(opts = {}) ⇒ Object
101 102 103 |
# File 'lib/rsolr/client.rb', line 101 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"
: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
.
144 145 146 147 |
# File 'lib/rsolr/client.rb', line 144 def send_and_receive path, opts request_context = build_request path, opts 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
52 53 54 55 56 |
# File 'lib/rsolr/client.rb', line 52 def update opts = {} opts[:headers] ||= {} opts[:headers]['Content-Type'] ||= 'text/xml' post 'update', opts end |