rsolr 1.0.0.beta4 → 1.0.0.beta5
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/README.rdoc +41 -7
- data/VERSION +1 -1
- data/lib/rsolr/pagination.rb +2 -2
- data/lib/rsolr/xml.rb +1 -1
- data/spec/api/client_spec.rb +51 -22
- data/spec/api/connection_spec.rb +11 -0
- data/spec/api/pagination_spec.rb +4 -0
- data/spec/api/rsolr_spec.rb +19 -0
- metadata +6 -4
- data/spec/api/http_spec.rb +0 -4
data/README.rdoc
CHANGED
@@ -26,6 +26,14 @@ The code docs can be viewed here : http://rdoc.info/projects/mwmitchell/rsolr
|
|
26
26
|
# send a request to /catalog
|
27
27
|
response = solr.get 'catalog', :params => {:q => '*:*'}
|
28
28
|
|
29
|
+
When the Solr :wt is :ruby, then the response will be a Hash. This Hash is the same object returned by Solr, but evaluated as Ruby. If the :wt is not :ruby, then the response will be a String.
|
30
|
+
|
31
|
+
The response also exposes 2 attribute readers (for any :wt value), :request and :response. Both are Hash objects with symbolized keys.
|
32
|
+
|
33
|
+
The :request attribute contains the original request context. You can use this for debugging or logging. Some of the keys this object contains are :uri, :query, :method etc..
|
34
|
+
|
35
|
+
The :response attribute contains the original response. This object contains the :status, :body and :headers keys.
|
36
|
+
|
29
37
|
== Querying
|
30
38
|
Use the #get / #post method to send search requests to the /select handler:
|
31
39
|
response = solr.get 'select', :params => {
|
@@ -33,8 +41,10 @@ Use the #get / #post method to send search requests to the /select handler:
|
|
33
41
|
:start=>0,
|
34
42
|
:rows=>10
|
35
43
|
}
|
44
|
+
response["response"]["docs"].each{|doc| puts doc["id"] }
|
36
45
|
|
37
|
-
The :params sent into the method are sent to Solr as-is
|
46
|
+
The :params sent into the method are sent to Solr as-is, which is to say they are converted to Solr url style, but no special mapping is used.
|
47
|
+
When an array is used, multiple parameters *with the same name* are generated for the Solr query. Example:
|
38
48
|
|
39
49
|
solr.get 'select', :params => {:q=>'roses', :fq=>['red', 'violet']}
|
40
50
|
|
@@ -42,6 +52,15 @@ The above statement generates this Solr query:
|
|
42
52
|
|
43
53
|
select?q=roses&fq=red&fq=violet
|
44
54
|
|
55
|
+
===Pagination
|
56
|
+
To paginate through a set of Solr documents, use the paginate method:
|
57
|
+
solr.paginate 1, 10, "select", :params => {:q => "test"}
|
58
|
+
|
59
|
+
The first argument is the current page, the second is how many documents to return for each page. In other words, "page" is the "start" Solr param and "per-page" is the "rows" Solr param.
|
60
|
+
|
61
|
+
The paginate method returns WillPaginate ready "docs" objects, so for example in a Rails application, paginating is as simple as:
|
62
|
+
<%= will_paginate @solr_response["response"]["docs"] %>
|
63
|
+
|
45
64
|
===Method Missing
|
46
65
|
The RSolr::Client class also uses method_missing for setting the request handler/path:
|
47
66
|
|
@@ -50,13 +69,32 @@ The RSolr::Client class also uses method_missing for setting the request handler
|
|
50
69
|
This is sent to Solr as:
|
51
70
|
paintings?q=roses&fq=red&fq=violet
|
52
71
|
|
72
|
+
This works with pagination as well:
|
73
|
+
|
74
|
+
solr.paginate_paintings 1, 10, {:q=>'roses', :fq=>['red', 'violet']}
|
53
75
|
|
54
76
|
===Using POST for Search Queries
|
55
77
|
There may be cases where the query string is too long for a GET request. RSolr solves this issue by converting hash objects into form-encoded strings:
|
56
|
-
response = solr.
|
78
|
+
response = solr.music :data => {:q => "*:*"}
|
57
79
|
|
58
80
|
The :data hash is serialized as a form-encoded query string, and the correct content-type headers are sent along to Solr.
|
59
81
|
|
82
|
+
===Sending HEAD Requests
|
83
|
+
There may be cases where you'd like to send a HEAD request to Solr:
|
84
|
+
solr.head("admin/ping").response[:status] == 200
|
85
|
+
|
86
|
+
==Sending HTTP Headers
|
87
|
+
Solr responds to the request headers listed here: http://wiki.apache.org/solr/SolrAndHTTPCaches
|
88
|
+
To send header information to Solr using RSolr, just use the :headers option:
|
89
|
+
response = solr.head "admin/ping", :headers => {"Cache-Control" => "If-None-Match"}
|
90
|
+
|
91
|
+
===Building a Request
|
92
|
+
RSolr::Client provides a method for building a request context, which can be useful for debugging or logging etc.:
|
93
|
+
request_context = solr.build_request "select", :data => {:q => "*:*"}, :method => :post, :headers => {}
|
94
|
+
|
95
|
+
To build a paginated request use build_paginated_request:
|
96
|
+
request_context = solr.build_paginated_request 1, 10, "select", ...
|
97
|
+
|
60
98
|
== Updating Solr
|
61
99
|
Updating is done using native Ruby objects. Hashes are used for single documents and arrays are used for a collection of documents (hashes). These objects get turned into simple XML "messages". Raw XML strings can also be used.
|
62
100
|
|
@@ -87,7 +125,7 @@ When adding, you can also supply "add" xml element attributes and/or a block for
|
|
87
125
|
|
88
126
|
Now the "add_xml" object can be sent to Solr like:
|
89
127
|
solr.update :data => add_xml
|
90
|
-
|
128
|
+
|
91
129
|
===Deleting
|
92
130
|
Delete by id
|
93
131
|
solr.delete_by_id 1
|
@@ -116,10 +154,6 @@ The default response format is Ruby. When the :wt param is set to :ruby, the res
|
|
116
154
|
===JSON:
|
117
155
|
solr.get 'select', :params => {:wt => :json}
|
118
156
|
|
119
|
-
==Http Request Methods: +get+, +post+, and +head+
|
120
|
-
RSolr can send GET, POST and HEAD requests to Solr:
|
121
|
-
response = solr.head "admin"
|
122
|
-
|
123
157
|
==Related Resources & Projects
|
124
158
|
* {RSolr Google Group}[http://groups.google.com/group/rsolr] -- The RSolr discussion group
|
125
159
|
* {rsolr-ext}[http://github.com/mwmitchell/rsolr-ext] -- An extension kit for RSolr
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.0.
|
1
|
+
1.0.0.beta5
|
data/lib/rsolr/pagination.rb
CHANGED
@@ -38,8 +38,8 @@ module RSolr::Pagination
|
|
38
38
|
protected
|
39
39
|
|
40
40
|
# Checks if the called method starts
|
41
|
-
# with "paginate_" and
|
42
|
-
# converts the
|
41
|
+
# with "paginate_*" and
|
42
|
+
# converts the * to the solr
|
43
43
|
# request path. It then calls paginate
|
44
44
|
# with the appropriate arguments.
|
45
45
|
# If the called method doesn't
|
data/lib/rsolr/xml.rb
CHANGED
@@ -74,7 +74,7 @@ module RSolr::Xml
|
|
74
74
|
|
75
75
|
def build &block
|
76
76
|
require 'builder'
|
77
|
-
b = ::Builder::XmlMarkup.new(:indent=>0, :margin=>0, :encoding => 'UTF-8')
|
77
|
+
b = ::Builder::XmlMarkup.new(:indent => 0, :margin => 0, :encoding => 'UTF-8')
|
78
78
|
b.instruct!
|
79
79
|
block_given? ? yield(b) : b
|
80
80
|
end
|
data/spec/api/client_spec.rb
CHANGED
@@ -29,17 +29,16 @@ describe "RSolr::Client" do
|
|
29
29
|
|
30
30
|
context "post" do
|
31
31
|
include ClientHelper
|
32
|
-
it "should pass the expected params to the connection's #
|
32
|
+
it "should pass the expected params to the connection's #execute method" do
|
33
|
+
request_opts = {:data => "the data", :method=>:post, :headers => {"Content-Type" => "text/plain"}}
|
33
34
|
client.connection.should_receive(:execute).
|
34
|
-
with(
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
)
|
42
|
-
client.post "update", :data => "the data", :method=>:post, :headers => {"Content-Type" => "text/plain"}
|
35
|
+
with(client, hash_including(request_opts)).
|
36
|
+
and_return(
|
37
|
+
:body => "",
|
38
|
+
:status => 200,
|
39
|
+
:headers => {"Content-Type"=>"text/plain"}
|
40
|
+
)
|
41
|
+
client.post "update", request_opts
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
@@ -55,15 +54,18 @@ describe "RSolr::Client" do
|
|
55
54
|
it "should send xml to the connection's #post method" do
|
56
55
|
client.connection.should_receive(:execute).
|
57
56
|
with(
|
58
|
-
client, hash_including({
|
57
|
+
client, hash_including({
|
58
|
+
:path => "update",
|
59
|
+
:headers => {"Content-Type"=>"text/xml"},
|
60
|
+
:method => :post,
|
61
|
+
:data => "<xml/>"
|
62
|
+
})
|
59
63
|
).
|
60
64
|
and_return(
|
61
65
|
:body => "",
|
62
66
|
:status => 200,
|
63
67
|
:headers => {"Content-Type"=>"text/xml"}
|
64
68
|
)
|
65
|
-
# the :xml attr is lazy loaded... so load it up first
|
66
|
-
client.xml
|
67
69
|
client.xml.should_receive(:add).
|
68
70
|
with({:id=>1}, {:commitWith=>10}).
|
69
71
|
and_return("<xml/>")
|
@@ -76,7 +78,12 @@ describe "RSolr::Client" do
|
|
76
78
|
it "should send data to the connection's #post method" do
|
77
79
|
client.connection.should_receive(:execute).
|
78
80
|
with(
|
79
|
-
client, hash_including({
|
81
|
+
client, hash_including({
|
82
|
+
:path => "update",
|
83
|
+
:headers => {"Content-Type"=>"text/xml"},
|
84
|
+
:method => :post,
|
85
|
+
:data => "<optimize/>"
|
86
|
+
})
|
80
87
|
).
|
81
88
|
and_return(
|
82
89
|
:body => "",
|
@@ -93,7 +100,12 @@ describe "RSolr::Client" do
|
|
93
100
|
it "should send a #{meth} message to the connection's #post method" do
|
94
101
|
client.connection.should_receive(:execute).
|
95
102
|
with(
|
96
|
-
client, hash_including({
|
103
|
+
client, hash_including({
|
104
|
+
:path => "update",
|
105
|
+
:headers => {"Content-Type"=>"text/xml"},
|
106
|
+
:method => :post,
|
107
|
+
:data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>"
|
108
|
+
})
|
97
109
|
).
|
98
110
|
and_return(
|
99
111
|
:body => "",
|
@@ -110,7 +122,12 @@ describe "RSolr::Client" do
|
|
110
122
|
it "should send data to the connection's #post method" do
|
111
123
|
client.connection.should_receive(:execute).
|
112
124
|
with(
|
113
|
-
client, hash_including({
|
125
|
+
client, hash_including({
|
126
|
+
:path => "update",
|
127
|
+
:headers => {"Content-Type"=>"text/xml"},
|
128
|
+
:method => :post,
|
129
|
+
:data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id></delete>"
|
130
|
+
})
|
114
131
|
).
|
115
132
|
and_return(
|
116
133
|
:body => "",
|
@@ -126,7 +143,12 @@ describe "RSolr::Client" do
|
|
126
143
|
it "should send data to the connection's #post method" do
|
127
144
|
client.connection.should_receive(:execute).
|
128
145
|
with(
|
129
|
-
client, hash_including({
|
146
|
+
client, hash_including({
|
147
|
+
:path => "update",
|
148
|
+
:headers => {"Content-Type"=>"text/xml"},
|
149
|
+
:method => :post,
|
150
|
+
:data => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query fq=\"category:"trash"\"/></delete>"
|
151
|
+
})
|
130
152
|
).
|
131
153
|
and_return(
|
132
154
|
:body => "",
|
@@ -142,18 +164,16 @@ describe "RSolr::Client" do
|
|
142
164
|
it 'should not try to evaluate ruby when the :qt is not :ruby' do
|
143
165
|
body = '{:time=>"NOW"}'
|
144
166
|
result = client.adapt_response({:params=>{}}, {:status => 200, :body => body, :headers => {}})
|
145
|
-
result.should be_a(String)
|
146
167
|
result.should == body
|
147
168
|
end
|
148
169
|
|
149
170
|
it 'should evaluate ruby responses when the :wt is :ruby' do
|
150
171
|
body = '{:time=>"NOW"}'
|
151
172
|
result = client.adapt_response({:params=>{:wt=>:ruby}}, {:status => 200, :body => body, :headers => {}})
|
152
|
-
result.should be_a(Hash)
|
153
173
|
result.should == {:time=>"NOW"}
|
154
174
|
end
|
155
175
|
|
156
|
-
it "ought raise a RSolr::Error::InvalidRubyResponse when the ruby is indeed frugged" do
|
176
|
+
it "ought raise a RSolr::Error::InvalidRubyResponse when the ruby is indeed frugged, or even fruggified" do
|
157
177
|
lambda {
|
158
178
|
client.adapt_response({:params=>{:wt => :ruby}}, {:status => 200, :body => "<woops/>", :headers => {}})
|
159
179
|
}.should raise_error RSolr::Error::InvalidRubyResponse
|
@@ -164,7 +184,12 @@ describe "RSolr::Client" do
|
|
164
184
|
context "build_request" do
|
165
185
|
include ClientHelper
|
166
186
|
it 'should return a request context array' do
|
167
|
-
result = client.build_request
|
187
|
+
result = client.build_request('select',
|
188
|
+
:method => :post,
|
189
|
+
:params => {:q=>'test', :fq=>[0,1]},
|
190
|
+
:data => "data",
|
191
|
+
:headers => {}
|
192
|
+
)
|
168
193
|
[/fq=0/, /fq=1/, /q=test/, /wt=ruby/].each do |pattern|
|
169
194
|
result[:query].should match pattern
|
170
195
|
end
|
@@ -173,7 +198,11 @@ describe "RSolr::Client" do
|
|
173
198
|
end
|
174
199
|
|
175
200
|
it "should set the Content-Type header to application/x-www-form-urlencoded if a hash is passed in to the data arg" do
|
176
|
-
result = client.build_request
|
201
|
+
result = client.build_request('select',
|
202
|
+
:method => :post,
|
203
|
+
:data => {:q=>'test', :fq=>[0,1]},
|
204
|
+
:headers => {}
|
205
|
+
)
|
177
206
|
result[:query].should == "wt=ruby"
|
178
207
|
[/fq=0/, /fq=1/, /q=test/].each do |pattern|
|
179
208
|
result[:data].should match pattern
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe "RSolr::Connection" do
|
3
|
+
|
4
|
+
# context "execute" do
|
5
|
+
# c = RSolr::Connection.new
|
6
|
+
# base_url = "http://localhost:8983/solr"
|
7
|
+
# client = RSolr::Client.new c, :url => base_url
|
8
|
+
# c.execute client, {:method => :get, :uri => URI.parse(base_url + "/select")}
|
9
|
+
# end
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe "RSolr" do
|
3
|
+
|
4
|
+
it "has a version that can be read via #version or VERSION" do
|
5
|
+
RSolr.version.should == RSolr::VERSION
|
6
|
+
end
|
7
|
+
|
8
|
+
it "can escape" do
|
9
|
+
RSolr.should be_a(RSolr::Char)
|
10
|
+
RSolr.escape("this string").should == "this\\ string"
|
11
|
+
end
|
12
|
+
|
13
|
+
context "connect" do
|
14
|
+
it "should return a RSolr::Client instance" do
|
15
|
+
RSolr.connect.should be_a(RSolr::Client)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
|
|
6
6
|
- 1
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.0.
|
9
|
+
- beta5
|
10
|
+
version: 1.0.0.beta5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matt Mitchell
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-28 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -88,8 +88,10 @@ summary: A Ruby client for Apache Solr
|
|
88
88
|
test_files:
|
89
89
|
- spec/api/char_spec.rb
|
90
90
|
- spec/api/client_spec.rb
|
91
|
+
- spec/api/connection_spec.rb
|
91
92
|
- spec/api/error_spec.rb
|
92
|
-
- spec/api/
|
93
|
+
- spec/api/pagination_spec.rb
|
94
|
+
- spec/api/rsolr_spec.rb
|
93
95
|
- spec/api/uri_spec.rb
|
94
96
|
- spec/api/xml_spec.rb
|
95
97
|
- spec/spec_helper.rb
|
data/spec/api/http_spec.rb
DELETED