mwmitchell-solr 0.5.2 → 0.5.3
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 +63 -6
- data/lib/solr/connection/base.rb +1 -1
- data/lib/solr/response.rb +4 -139
- data/lib/solr.rb +1 -1
- data/test/connection_test_methods.rb +1 -1
- data/test/ext_pagination_test.rb +4 -4
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -9,7 +9,7 @@ To run tests:
|
|
9
9
|
MRI Ruby: rake
|
10
10
|
JRuby: jruby -S rake
|
11
11
|
|
12
|
-
To get a connection in Ruby:
|
12
|
+
To get a connection in MRI/standard Ruby:
|
13
13
|
|
14
14
|
solr = Solr.connect(:http)
|
15
15
|
|
@@ -17,10 +17,9 @@ To get a direct connection in jRuby using DirectSolrConnection:
|
|
17
17
|
|
18
18
|
solr = Solr.connect(:direct, :home_dir=>'/path/to/solr/home', :dist_dir=>'/path/to/solr/distribution')
|
19
19
|
|
20
|
-
You can set
|
20
|
+
You can set Solr params that will be sent for every request:
|
21
21
|
|
22
|
-
|
23
|
-
solr = Solr.connect(:http, {}, connection_opts)
|
22
|
+
solr = Solr.connect(:http, :global_params=>{:wt=>:ruby, :echoParams=>'EXPLICIT'})
|
24
23
|
|
25
24
|
Solr.connect also yields the adapter instance if a block is supplied:
|
26
25
|
|
@@ -47,8 +46,10 @@ Pagination is simplified by using the :page and :per_page params:
|
|
47
46
|
|
48
47
|
response = solr.query(:page=>1, :per_page=>10, :q=>'*:*')
|
49
48
|
response.per_page
|
50
|
-
response.
|
49
|
+
response.total_pages
|
51
50
|
response.current_page
|
51
|
+
response.previous_page
|
52
|
+
response.next_page
|
52
53
|
|
53
54
|
If you use WillPaginate, just pass-in the response to the #will_paginate view helper:
|
54
55
|
|
@@ -88,4 +89,60 @@ Commit & Optimize
|
|
88
89
|
|
89
90
|
|
90
91
|
==Response Formats
|
91
|
-
The default response format is Ruby. When the :wt param is set to :ruby, the response is eval'd and wrapped up in a nice Solr::Response class. You can get raw ruby by setting the :wt to "ruby" - notice, the string -- not a symbol. All other response formats are available as expected, :wt=>'xml' etc.. Currently, the only response format that gets eval'd and wrapped is :ruby.
|
92
|
+
The default response format is Ruby. When the :wt param is set to :ruby, the response is eval'd and wrapped up in a nice Solr::Response class. You can get raw ruby by setting the :wt to "ruby" - notice, the string -- not a symbol. All other response formats are available as expected, :wt=>'xml' etc.. Currently, the only response format that gets eval'd and wrapped is :ruby.
|
93
|
+
|
94
|
+
==Data Mapping
|
95
|
+
The Solr::Mapper::Base class provides some nice ways of mapping data. You provide a hash mapping and a "data source". The keys of the hash mapping become the Solr field names. The values of the hash mapping get processed differently based on the type of value. The data source must be an Enumerable type object. The hash mapping is called for each item in the data source.
|
96
|
+
|
97
|
+
===Hash Map Processing
|
98
|
+
If the value is a string, the String is used as the final Solr field value. If the value is a Symbol, the Symbol is used as a key on the data source. An Enumerable type does the same as the Symbol, but for each item. The most interesting processing occurs when the value is a Proc. When a Proc is used as a hash mapping value, the Solr::Mapper::Base executes the Proc's #call method, passing in the current data source item.
|
99
|
+
|
100
|
+
===Examples
|
101
|
+
|
102
|
+
mapping = {
|
103
|
+
:id=>:id,
|
104
|
+
:title=>:title,
|
105
|
+
:source=>'Example',
|
106
|
+
:meta=>[:author, :sub_title],
|
107
|
+
:web_id=>proc {|item|
|
108
|
+
WebService.fetch_item_id_by_name(item[:name])
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
data_source = [
|
113
|
+
{
|
114
|
+
:id=>100,
|
115
|
+
:title=>'Doc One',
|
116
|
+
:author=>'Mr. X',
|
117
|
+
:sub_title=>'A first class document.',
|
118
|
+
:name=>'doc_1'
|
119
|
+
},
|
120
|
+
{
|
121
|
+
:id=>200,
|
122
|
+
:title=>'Doc Two',
|
123
|
+
:author=>'Mr. XYZ',
|
124
|
+
:sub_title=>'A second class document.',
|
125
|
+
:name=>'doc_2'
|
126
|
+
}
|
127
|
+
]
|
128
|
+
|
129
|
+
mapper = Solr::Mapper::Base(mapping)
|
130
|
+
mapped_data = mapper.map(data_source)
|
131
|
+
|
132
|
+
# the following would be true...
|
133
|
+
mapped_data == [
|
134
|
+
{
|
135
|
+
:id=>100,
|
136
|
+
:title=>'Doc One',
|
137
|
+
:source=>'Example',
|
138
|
+
:meta=>['Mr. X', 'A first class document'],
|
139
|
+
:web_id=>'web_id_for_doc_1_for_example'
|
140
|
+
},
|
141
|
+
{
|
142
|
+
:id=>200,
|
143
|
+
:title=>'Doc Two',
|
144
|
+
:source=>'Example',
|
145
|
+
:meta=>['Mr. XYZ', 'A second class document'],
|
146
|
+
:web_id=>'web_id_for_doc_2_for_example'
|
147
|
+
}
|
148
|
+
]
|
data/lib/solr/connection/base.rb
CHANGED
@@ -37,7 +37,7 @@ class Solr::Connection::Base
|
|
37
37
|
def query(params)
|
38
38
|
params = map_params(modify_params_for_pagination(params))
|
39
39
|
response = @adapter.query(params)
|
40
|
-
params[:wt]==:ruby ? Solr::Response::Query.new(response) : response
|
40
|
+
params[:wt]==:ruby ? Solr::Response::Query::Base.new(response) : response
|
41
41
|
end
|
42
42
|
|
43
43
|
# Finds a document by its id
|
data/lib/solr/response.rb
CHANGED
@@ -1,143 +1,8 @@
|
|
1
1
|
module Solr::Response
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def initialize(data)
|
9
|
-
if data.is_a?(String)
|
10
|
-
@raw_response = data
|
11
|
-
@data = Kernel.eval(@raw_response)
|
12
|
-
else
|
13
|
-
@data = data
|
14
|
-
end
|
15
|
-
@header = @data['responseHeader']
|
16
|
-
@params = @header['params']
|
17
|
-
@status = @header['status']
|
18
|
-
@query_time = @header['QTime']
|
19
|
-
end
|
20
|
-
|
21
|
-
def ok?
|
22
|
-
self.status==0
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
=begin
|
28
|
-
class Document
|
29
|
-
|
30
|
-
attr_reader :data
|
31
|
-
|
32
|
-
def initialize(source_hash)
|
33
|
-
source_hash.each do |k,v|
|
34
|
-
@data[k.to_sym]=v
|
35
|
-
instance_eval <<-EOF
|
36
|
-
def #{k}
|
37
|
-
@data[:#{k}]
|
38
|
-
end
|
39
|
-
EOF
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
#
|
44
|
-
# doc.has?(:location_facet, 'Clemons')
|
45
|
-
# doc.has?(:id, 'h009', /^u/i)
|
46
|
-
#
|
47
|
-
def has?(k, *values)
|
48
|
-
return if @data[k].nil?
|
49
|
-
target = @data[k]
|
50
|
-
if target.is_a?(Array)
|
51
|
-
values.each do |val|
|
52
|
-
return target.any?{|tv| val.is_a?(Regexp) ? (tv =~ val) : (tv==val)}
|
53
|
-
end
|
54
|
-
else
|
55
|
-
return values.any? {|val| val.is_a?(Regexp) ? (target =~ val) : (target == val)}
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
#
|
60
|
-
def get(key, default=nil)
|
61
|
-
@data[key] || default
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
65
|
-
=end
|
66
|
-
|
67
|
-
# response for queries
|
68
|
-
class Query < Base
|
69
|
-
|
70
|
-
attr_reader :response, :docs, :num_found, :start
|
71
|
-
|
72
|
-
alias :total :num_found
|
73
|
-
alias :offset :start
|
74
|
-
|
75
|
-
def initialize(data)
|
76
|
-
super(data)
|
77
|
-
@response = @data['response']
|
78
|
-
@docs = @response['docs']
|
79
|
-
@num_found = @response['numFound']
|
80
|
-
@start = @response['start']
|
81
|
-
end
|
82
|
-
|
83
|
-
def per_page
|
84
|
-
@per_page = params['rows'].to_s.to_i
|
85
|
-
end
|
86
|
-
|
87
|
-
def current_page
|
88
|
-
@current_page = self.per_page > 0 ? ((self.start / self.per_page).ceil) : 1
|
89
|
-
@current_page == 0 ? 1 : @current_page
|
90
|
-
end
|
91
|
-
|
92
|
-
alias :page :current_page
|
93
|
-
|
94
|
-
def page_count
|
95
|
-
@page_count = self.per_page > 0 ? (self.total / self.per_page.to_f).ceil : 1
|
96
|
-
end
|
97
|
-
|
98
|
-
# supports WillPaginate
|
99
|
-
alias :total_pages :page_count
|
100
|
-
|
101
|
-
alias :pages :page_count
|
102
|
-
|
103
|
-
# supports WillPaginate
|
104
|
-
def previous_page
|
105
|
-
(current_page > 1) ? current_page - 1 : 1
|
106
|
-
end
|
107
|
-
|
108
|
-
# supports WillPaginate
|
109
|
-
def next_page
|
110
|
-
(current_page < page_count) ? current_page + 1 : page_count
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
# response class for update requests
|
116
|
-
class Update < Base
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
# response for /admin/luke
|
121
|
-
class IndexInfo < Base
|
122
|
-
|
123
|
-
attr_reader :index, :directory, :has_deletions, :optimized, :current, :max_doc, :num_docs, :version
|
124
|
-
|
125
|
-
alias :has_deletions? :has_deletions
|
126
|
-
alias :optimized? :optimized
|
127
|
-
alias :current? :current
|
128
|
-
|
129
|
-
def initialize(data)
|
130
|
-
super(data)
|
131
|
-
@index = @data['index']
|
132
|
-
@directory = @data['directory']
|
133
|
-
@has_deletions = @index['hasDeletions']
|
134
|
-
@optimized = @index['optimized']
|
135
|
-
@current = @index['current']
|
136
|
-
@max_doc = @index['maxDoc']
|
137
|
-
@num_docs = @index['numDocs']
|
138
|
-
@version = @index['version']
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
3
|
+
autoload :Base, 'solr/response/base'
|
4
|
+
autoload :Query, 'solr/response/query'
|
5
|
+
autoload :IndexInfo, 'solr/response/index_info'
|
6
|
+
autoload :Update, 'solr/response/update'
|
142
7
|
|
143
8
|
end
|
data/lib/solr.rb
CHANGED
@@ -39,7 +39,7 @@ module ConnectionTestMethods
|
|
39
39
|
|
40
40
|
def test_query_responses
|
41
41
|
r = @solr.query(:q=>'*:*')
|
42
|
-
assert r.is_a?(Solr::Response::Query)
|
42
|
+
assert r.is_a?(Solr::Response::Query::Base)
|
43
43
|
# catch exceptions for bad queries
|
44
44
|
assert_raise Solr::RequestError do
|
45
45
|
@solr.query(:q=>'!')
|
data/test/ext_pagination_test.rb
CHANGED
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'test_helpers')
|
|
3
3
|
class ExtPaginationTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def create_response(params={})
|
6
|
-
response = Solr::Response::Query.new(mock_query_response)
|
6
|
+
response = Solr::Response::Query::Base.new(mock_query_response)
|
7
7
|
response.params.merge! params
|
8
8
|
response
|
9
9
|
end
|
@@ -32,7 +32,7 @@ class ExtPaginationTest < Test::Unit::TestCase
|
|
32
32
|
assert_equal response.params['rows'], response.per_page
|
33
33
|
assert_equal 26, response.total
|
34
34
|
assert_equal 1, response.current_page
|
35
|
-
assert_equal 6, response.
|
35
|
+
assert_equal 6, response.total_pages
|
36
36
|
|
37
37
|
# now switch the rows (per_page)
|
38
38
|
# total and current page should remain the same value
|
@@ -42,7 +42,7 @@ class ExtPaginationTest < Test::Unit::TestCase
|
|
42
42
|
assert_equal response.params['rows'], response.per_page
|
43
43
|
assert_equal 26, response.total
|
44
44
|
assert_equal 1, response.current_page
|
45
|
-
assert_equal 13, response.
|
45
|
+
assert_equal 13, response.total_pages
|
46
46
|
|
47
47
|
# now switch the start
|
48
48
|
|
@@ -52,7 +52,7 @@ class ExtPaginationTest < Test::Unit::TestCase
|
|
52
52
|
assert_equal 26, response.total
|
53
53
|
# 2 per page, currently on the 10th item
|
54
54
|
assert_equal 1, response.current_page
|
55
|
-
assert_equal 9, response.
|
55
|
+
assert_equal 9, response.total_pages
|
56
56
|
end
|
57
57
|
|
58
58
|
end
|