Class: RSolr::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, opts = {}) ⇒ Connection

“adapter” is instance of:

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


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

def initialize(adapter, opts={})
  @adapter = adapter
  @opts = opts
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
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’])



49
50
51
# File 'lib/rsolr/connection.rb', line 49

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

#commitObject

send </commit>



54
55
56
# File 'lib/rsolr/connection.rb', line 54

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])


72
73
74
# File 'lib/rsolr/connection.rb', line 72

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"']


79
80
81
# File 'lib/rsolr/connection.rb', line 79

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

#optimizeObject

send </optimize>



59
60
61
# File 'lib/rsolr/connection.rb', line 59

def optimize
  update message.optimize
end

#rollbackObject

send </rollback> NOTE: solr 1.4 only



65
66
67
# File 'lib/rsolr/connection.rb', line 65

def rollback
  update message.rollback
end

#select(params, &blk) ⇒ Object

send a request to the “select” handler



14
15
16
# File 'lib/rsolr/connection.rb', line 14

def select(params, &blk)
  send_request('/select', map_params(params), &blk)
end

#send_request(path, params = {}, data = nil, &blk) ⇒ 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

use a block to get access to the adapter response: solr.send_request(‘/select’, :q=>‘blue’) do |solr_response, adapter_response|

raise 'Woops!' if adapter_response[:status] != 200
solr_response[:response][:docs].each {|doc|}

end



37
38
39
40
# File 'lib/rsolr/connection.rb', line 37

def send_request(path, params={}, data=nil, &blk)
  response = @adapter.send_request(path, map_params(params), data)
  adapt_response(response, &blk)
end

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

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



20
21
22
# File 'lib/rsolr/connection.rb', line 20

def update(data, params={}, &blk)
  send_request('/update', map_params(params), data, &blk)
end