rsolr 0.9.7.1 → 0.9.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.txt +3 -0
- data/lib/rsolr.rb +1 -1
- data/lib/rsolr/client.rb +113 -0
- metadata +3 -2
data/CHANGES.txt
CHANGED
data/lib/rsolr.rb
CHANGED
data/lib/rsolr/client.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
class RSolr::Client
|
2
|
+
|
3
|
+
attr_reader :adapter
|
4
|
+
|
5
|
+
# "adapter" is instance of:
|
6
|
+
# RSolr::Adapter::HTTP
|
7
|
+
# RSolr::Adapter::Direct (jRuby only)
|
8
|
+
# or any other class that uses the connection "interface"
|
9
|
+
def initialize(adapter)
|
10
|
+
@adapter = adapter
|
11
|
+
end
|
12
|
+
|
13
|
+
# Send a request to a request handler using the method name.
|
14
|
+
def method_missing(method_name, *args, &blk)
|
15
|
+
request("/#{method_name}", *args, &blk)
|
16
|
+
end
|
17
|
+
|
18
|
+
# sends data to the update handler
|
19
|
+
# data can be a string of xml, or an object that returns xml from its #to_xml method
|
20
|
+
def update(data, params={})
|
21
|
+
request '/update', params, data
|
22
|
+
end
|
23
|
+
|
24
|
+
# send request solr
|
25
|
+
# params is hash with valid solr request params (:q, :fl, :qf etc..)
|
26
|
+
# if params[:wt] is not set, the default is :ruby
|
27
|
+
# if :wt is something other than :ruby, the raw response body is used
|
28
|
+
# otherwise, a simple Hash is returned
|
29
|
+
# NOTE: to get raw ruby, use :wt=>'ruby' <- a string, not a symbol like :ruby
|
30
|
+
#
|
31
|
+
#
|
32
|
+
def request(path, params={}, *extra)
|
33
|
+
response = @adapter.request(path, map_params(params), *extra)
|
34
|
+
adapt_response(response)
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# single record:
|
39
|
+
# solr.update(:id=>1, :name=>'one')
|
40
|
+
#
|
41
|
+
# update using an array
|
42
|
+
# solr.update([{:id=>1, :name=>'one'}, {:id=>2, :name=>'two'}])
|
43
|
+
#
|
44
|
+
def add(doc, &block)
|
45
|
+
update message.add(doc, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
# send </commit>
|
49
|
+
def commit
|
50
|
+
update message.commit
|
51
|
+
end
|
52
|
+
|
53
|
+
# send </optimize>
|
54
|
+
def optimize
|
55
|
+
update message.optimize
|
56
|
+
end
|
57
|
+
|
58
|
+
# send </rollback>
|
59
|
+
# NOTE: solr 1.4 only
|
60
|
+
def rollback
|
61
|
+
update message.rollback
|
62
|
+
end
|
63
|
+
|
64
|
+
# Delete one or many documents by id
|
65
|
+
# solr.delete_by_id 10
|
66
|
+
# solr.delete_by_id([12, 41, 199])
|
67
|
+
def delete_by_id(id)
|
68
|
+
update message.delete_by_id(id)
|
69
|
+
end
|
70
|
+
|
71
|
+
# delete one or many documents by query
|
72
|
+
# solr.delete_by_query 'available:0'
|
73
|
+
# solr.delete_by_query ['quantity:0', 'manu:"FQ"']
|
74
|
+
def delete_by_query(query)
|
75
|
+
update message.delete_by_query(query)
|
76
|
+
end
|
77
|
+
|
78
|
+
# shortcut to RSolr::Message::Builder
|
79
|
+
def message
|
80
|
+
@message ||= RSolr::Message::Builder.new
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
# sets default params etc.. - could be used as a mapping hook
|
86
|
+
# type of request should be passed in here? -> map_params(:query, {})
|
87
|
+
def map_params(params)
|
88
|
+
params||={}
|
89
|
+
{:wt=>:ruby}.merge(params)
|
90
|
+
end
|
91
|
+
|
92
|
+
# "adapter_response" must be a hash with the following keys:
|
93
|
+
# :params - a sub hash of standard solr params
|
94
|
+
# : body - the raw response body from the solr server
|
95
|
+
# This method will evaluate the :body value if the params[:wt] == :ruby
|
96
|
+
# otherwise, the body is returned
|
97
|
+
# The return object has a special method attached called #adapter_response
|
98
|
+
# This method gives you access to the original response from the adapter,
|
99
|
+
# so you can access things like the actual :url sent to solr,
|
100
|
+
# the raw :body, original :params and original :data
|
101
|
+
def adapt_response(adapter_response)
|
102
|
+
data = adapter_response[:body]
|
103
|
+
# if the wt is :ruby, evaluate the ruby string response
|
104
|
+
if adapter_response[:params][:wt] == :ruby
|
105
|
+
data = Kernel.eval(data)
|
106
|
+
end
|
107
|
+
# attach a method called #adapter_response that returns the original adapter response value
|
108
|
+
def data.raw; @raw end
|
109
|
+
data.send(:instance_variable_set, '@raw', adapter_response)
|
110
|
+
data
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rsolr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.7.
|
4
|
+
version: 0.9.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Mitchell
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-06 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,6 +27,7 @@ extra_rdoc_files:
|
|
27
27
|
files:
|
28
28
|
- examples/direct.rb
|
29
29
|
- examples/http.rb
|
30
|
+
- lib/rsolr/client.rb
|
30
31
|
- lib/rsolr/connection/direct.rb
|
31
32
|
- lib/rsolr/connection/net_http.rb
|
32
33
|
- lib/rsolr/connection.rb
|