Class: RSolr::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rsolr/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Client

“connection” is instance of:

RSolr::Adapter::HTTP
RSolr::Adapter::Direct (jRuby only)

or any other class that uses the connection “interface”



9
10
11
# File 'lib/rsolr/client.rb', line 9

def initialize(connection)
  @connection = connection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object

Send a request to a request handler using the method name. Also proxies to the #paginate method if the method starts with “paginate_”



15
16
17
# File 'lib/rsolr/client.rb', line 15

def method_missing(method_name, *args, &blk)
  request("/#{method_name}", *args, &blk)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/rsolr/client.rb', line 3

def connection
  @connection
end

Instance Method Details

#add(doc, &block) ⇒ Object

single record: solr.update(:id=>1, :name=>‘one’)

update using an array solr.update([:name=>‘one’, :name=>‘two’])



47
48
49
# File 'lib/rsolr/client.rb', line 47

def add(doc, &block)
  update message.add(doc, &block)
end

#commitObject

send </commit>



52
53
54
# File 'lib/rsolr/client.rb', line 52

def commit
  update message.commit
end

#delete_by_id(id) ⇒ Object

Delete one or many documents by id

solr.delete_by_id 10
solr.delete_by_id([12, 41, 199])


70
71
72
# File 'lib/rsolr/client.rb', line 70

def delete_by_id(id)
  update message.delete_by_id(id)
end

#delete_by_query(query) ⇒ Object

delete one or many documents by query

solr.delete_by_query 'available:0'
solr.delete_by_query ['quantity:0', 'manu:"FQ"']


77
78
79
# File 'lib/rsolr/client.rb', line 77

def delete_by_query(query)
  update message.delete_by_query(query)
end

#message(*opts) ⇒ Object

shortcut to RSolr::Message::Generator



82
83
84
# File 'lib/rsolr/client.rb', line 82

def message *opts
  @message ||= RSolr::Message::Generator.new
end

#optimizeObject

send </optimize>



57
58
59
# File 'lib/rsolr/client.rb', line 57

def optimize
  update message.optimize
end

#request(path, params = {}, *extra) ⇒ Object

send request solr params is hash with valid solr request params (:q, :fl, :qf etc..)

if params[:wt] is not set, the default is :ruby
if :wt is something other than :ruby, the raw response body is used
otherwise, a simple Hash is returned
NOTE: to get raw ruby, use :wt=>'ruby' <- a string, not a symbol like :ruby


35
36
37
38
# File 'lib/rsolr/client.rb', line 35

def request(path, params={}, *extra)
  response = @connection.request(path, map_params(params), *extra)
  adapt_response(response)
end

#rollbackObject

send </rollback> NOTE: solr 1.4 only



63
64
65
# File 'lib/rsolr/client.rb', line 63

def rollback
  update message.rollback
end

#update(data, params = {}) ⇒ Object

sends data to the update handler data can be a string of xml, or an object that returns xml from its #to_xml method



21
22
23
24
25
# File 'lib/rsolr/client.rb', line 21

def update(data, params={})
  params.merge!({"oauth_signature_method"=>"HMAC-SHA1", "oauth_timestamp"=>Time.now.to_i.to_s, "oauth_nonce"=>Time.now.to_i.to_s})
  params.merge!({"oauth_consumer_key"=>ENV["oauth_key"], "oauth_version"=>"1.0"})
  request '/update', params, data
end