mwmitchell-rsolr-ext 0.7.12 → 0.7.31
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 +15 -45
- data/lib/rsolr-ext/doc.rb +3 -11
- data/lib/rsolr-ext/findable.rb +1 -1
- data/lib/rsolr-ext/response/docs.rb +4 -2
- data/lib/rsolr-ext/response/spelling.rb +68 -0
- data/lib/rsolr-ext/response.rb +8 -1
- data/lib/rsolr-ext.rb +1 -1
- data/rsolr-ext.gemspec +3 -2
- metadata +3 -2
data/README.rdoc
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
A set of helper methods/modules to assist in building Solr queries and handling responses when using the RSolr library.
|
3
3
|
|
4
4
|
==Request Example
|
5
|
-
solr_params =
|
5
|
+
solr_params = {
|
6
6
|
:page=>2,
|
7
7
|
:per_page=>10,
|
8
8
|
:phrases=>{:name=>'This is a phrase'},
|
@@ -10,69 +10,39 @@ A set of helper methods/modules to assist in building Solr queries and handling
|
|
10
10
|
:phrase_filters=>{:manu=>['Apple']},
|
11
11
|
:queries=>'ipod',
|
12
12
|
:facets=>{:fields=>['cat', 'blah']}
|
13
|
-
|
13
|
+
}
|
14
14
|
|
15
|
-
rsolr = RSolr.connect
|
15
|
+
rsolr = RSolr::Ext.connect
|
16
16
|
|
17
17
|
response = rsolr.select(solr_params)
|
18
18
|
|
19
19
|
==Response Example
|
20
|
-
rsolr = RSolr.connect
|
20
|
+
rsolr = RSolr::Ext.connect
|
21
21
|
|
22
|
-
|
23
|
-
r = RSolr::Ext::wrap_response(raw_response)
|
22
|
+
response = rsolr.select(:q=>'*:*)
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
response.ok?
|
25
|
+
response.params
|
26
|
+
response.docs
|
27
|
+
response.docs.previous_page
|
28
|
+
response.docs.next_page
|
29
|
+
response.facets
|
31
30
|
|
32
31
|
You can access values in the response hash using symbols or strings.
|
33
32
|
|
34
33
|
===Doc Pagination
|
35
|
-
|
36
|
-
|
37
|
-
raw_response = rsolr.select(:q=>'*:*)
|
38
|
-
@response = RSolr::Ext.wrap_response(raw_response)
|
39
|
-
# in view:
|
40
|
-
<%= will_paginate @response.docs %>
|
41
|
-
|
42
|
-
==The Findable Module
|
43
|
-
|
44
|
-
You can get a modified RSolr.connect object by calling RSolr::Ext.connect.
|
45
|
-
|
46
|
-
The object returned is an RSolr::Connection::Adapter (Direct or HTTP) with additional methods attached, most notably #find, which comes from the RSolr::Ext::Findable module.
|
47
|
-
|
48
|
-
The #find method provides a convenient way to search solr. Here are some examples:
|
49
|
-
|
50
|
-
solr = RSolr::Ext.connect
|
51
|
-
|
52
|
-
# q=jefferson - returns all docs
|
53
|
-
all_jefferson_docs = solr.find 'jefferson'
|
54
|
-
|
55
|
-
# q=jefferson&rows=1 -- first doc only
|
56
|
-
a_single_jefferson_doc = solr.find :first, 'jefferson'
|
57
|
-
|
58
|
-
# q=jefferson&fq=type:"book" - all docs
|
59
|
-
books_about_jefferson = solr.find 'jefferson', :phrase_filters=>{:type=>'book'}
|
60
|
-
|
61
|
-
# q=something -- the entire response
|
62
|
-
solr_response = solr.find {:q=>'something'}, :include_response=>true
|
34
|
+
If you wanna paginate, just throw the collection into the WillPaginate view helper.
|
35
|
+
<%= will_paginate response.docs %>
|
63
36
|
|
64
37
|
===The Doc Module
|
65
38
|
You can create your own "models" using RSolr::Ext::Doc
|
39
|
+
|
66
40
|
class Book
|
67
41
|
include RSolr::Ext::Doc
|
68
|
-
@default_params = {:fq=>'object_type:"book"', :rows=>10}
|
69
|
-
|
70
42
|
def self.find_by_author(author)
|
71
|
-
find(:phrase_filters=>{:author=>author})
|
43
|
+
find(:fq=>'object_type:"book"', :rows=>10, :phrase_filters=>{:author=>author})
|
72
44
|
end
|
73
45
|
end
|
74
46
|
|
75
47
|
all_books = Book.find('*:*')
|
76
48
|
hawk_books = Book.find_by_author('hawk')
|
77
|
-
|
78
|
-
If you wanna paginate, just throw the collection into the WillPaginate view helper.
|
data/lib/rsolr-ext/doc.rb
CHANGED
@@ -41,27 +41,19 @@ module RSolr::Ext::Doc
|
|
41
41
|
#
|
42
42
|
module Findable
|
43
43
|
|
44
|
-
attr_accessor :
|
44
|
+
attr_accessor :connection
|
45
45
|
|
46
46
|
def connection
|
47
47
|
@connection ||= RSolr::Ext.connect
|
48
48
|
end
|
49
49
|
|
50
|
-
def default_params
|
51
|
-
@default_params ||= {}
|
52
|
-
# don't allow nil values to pass!
|
53
|
-
@default_params.delete_if {|k,v| v.to_s.empty?}
|
54
|
-
@default_params
|
55
|
-
end
|
56
|
-
|
57
50
|
def find(*args)
|
58
51
|
mode, solr_params, opts = connection.send(:extract_find_opts!, *args)
|
59
|
-
|
60
|
-
connection.find(*[mode, final_params, opts]) { |doc| self.new(doc) }
|
52
|
+
connection.find(*[mode, solr_params, opts]) { |doc| self.new(doc) }
|
61
53
|
end
|
62
54
|
|
63
55
|
def find_by_id(id, solr_params={}, opts={})
|
64
|
-
connection.find_by_id(id,
|
56
|
+
connection.find_by_id(id, solr_params, opts) { |doc| self.new(doc) }
|
65
57
|
end
|
66
58
|
|
67
59
|
end
|
data/lib/rsolr-ext/findable.rb
CHANGED
@@ -34,7 +34,7 @@ module RSolr::Ext::Findable
|
|
34
34
|
def find(*args, &blk)
|
35
35
|
mode, solr_params, opts = extract_find_opts!(*args)
|
36
36
|
|
37
|
-
opts[:include_response]
|
37
|
+
opts[:include_response] ||= true
|
38
38
|
|
39
39
|
solr_params[:rows] = 1 if mode == :first
|
40
40
|
valid_solr_params = RSolr::Ext.map_params(solr_params)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RSolr::Ext::Response::Docs
|
2
2
|
|
3
|
-
module
|
3
|
+
module Accessible
|
4
4
|
|
5
5
|
# Helper method to check if value/multi-values exist for a given key.
|
6
6
|
# The value can be a string, or a RegExp
|
@@ -83,7 +83,9 @@ module RSolr::Ext::Response::Docs
|
|
83
83
|
def self.extended(base)
|
84
84
|
d = base['response']['docs']
|
85
85
|
d.extend Pageable
|
86
|
-
d.each
|
86
|
+
d.each do |item|
|
87
|
+
item.extend Accessible
|
88
|
+
end
|
87
89
|
d.per_page = base['responseHeader']['params']['rows'].to_s.to_i
|
88
90
|
d.start = base['response']['start'].to_s.to_i
|
89
91
|
d.total = base['response']['numFound'].to_s.to_i
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# A mixin for making access to the spellcheck component data easy.
|
2
|
+
#
|
3
|
+
# response.spelling.words
|
4
|
+
#
|
5
|
+
module RSolr::Ext::Response::Spelling
|
6
|
+
|
7
|
+
def spelling
|
8
|
+
@spelling ||= Base.new(self)
|
9
|
+
end
|
10
|
+
|
11
|
+
class Base
|
12
|
+
|
13
|
+
attr :response
|
14
|
+
|
15
|
+
def initialize(response)
|
16
|
+
@response = response
|
17
|
+
end
|
18
|
+
|
19
|
+
# returns an array of spelling suggestion for specific query words,
|
20
|
+
# as provided in the solr response. Only includes words with higher
|
21
|
+
# frequency of occurrence than word in original query.
|
22
|
+
# can't do a full query suggestion because we only get info for each word;
|
23
|
+
# combination of words may not have results.
|
24
|
+
# Thanks to Naomi Dushay!
|
25
|
+
def words
|
26
|
+
@words ||= (
|
27
|
+
spellcheck = self[:spellcheck]
|
28
|
+
if spellcheck && spellcheck[:suggestions]
|
29
|
+
suggestions = spellcheck[:suggestions]
|
30
|
+
result = Array.new
|
31
|
+
if (!suggestions.nil?)
|
32
|
+
# suggestions is an array:
|
33
|
+
# (query term)
|
34
|
+
# (hash of term info and term suggestion)
|
35
|
+
# ...
|
36
|
+
# (query term)
|
37
|
+
# (hash of term info and term suggestion)
|
38
|
+
# 'correctlySpelled'
|
39
|
+
# true/false
|
40
|
+
# collation
|
41
|
+
# (suggestion for collation)
|
42
|
+
i_stop = suggestions.index("correctlySpelled")
|
43
|
+
# step through array in 2s to get info for each term
|
44
|
+
0.step(i_stop-1, 2) do |i|
|
45
|
+
term = suggestions[i]
|
46
|
+
term_info = suggestions[i+1]
|
47
|
+
# term_info is a hash:
|
48
|
+
# numFound =>
|
49
|
+
# startOffset =>
|
50
|
+
# endOffset =>
|
51
|
+
# origFreq =>
|
52
|
+
# suggestion => { frequency =>, word => }
|
53
|
+
origFreq = term_info['origFreq']
|
54
|
+
suggFreq = term_info['suggestion']['frequency']
|
55
|
+
if suggFreq > origFreq
|
56
|
+
result.push(term_info['suggestion']['word'])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
result.uniq!
|
60
|
+
result.empty? ? nil : result
|
61
|
+
end
|
62
|
+
end
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
data/lib/rsolr-ext/response.rb
CHANGED
@@ -2,6 +2,7 @@ module RSolr::Ext::Response
|
|
2
2
|
|
3
3
|
autoload :Facets, 'rsolr-ext/response/facets'
|
4
4
|
autoload :Docs, 'rsolr-ext/response/docs'
|
5
|
+
autoload :Spelling, 'rsolr-ext/response/spelling'
|
5
6
|
|
6
7
|
class Base < Mash
|
7
8
|
|
@@ -26,10 +27,16 @@ module RSolr::Ext::Response
|
|
26
27
|
super(*args)
|
27
28
|
extend Docs
|
28
29
|
extend Facets
|
30
|
+
extend Spelling
|
29
31
|
end
|
30
32
|
|
31
33
|
def response
|
32
|
-
self[
|
34
|
+
self[:response]
|
35
|
+
end
|
36
|
+
|
37
|
+
# short cut to response['numFound']
|
38
|
+
def total
|
39
|
+
response[:numFound]
|
33
40
|
end
|
34
41
|
|
35
42
|
end
|
data/lib/rsolr-ext.rb
CHANGED
data/rsolr-ext.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "rsolr-ext"
|
3
|
-
s.version = "0.7.
|
4
|
-
s.date = "2009-05-
|
3
|
+
s.version = "0.7.31"
|
4
|
+
s.date = "2009-05-27"
|
5
5
|
s.summary = "An extension lib for RSolr"
|
6
6
|
s.email = "goodieboy@gmail.com"
|
7
7
|
s.homepage = "http://github.com/mwmitchell/rsolr_ext"
|
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
|
|
23
23
|
|
24
24
|
"lib/rsolr-ext/response/docs.rb",
|
25
25
|
"lib/rsolr-ext/response/facets.rb",
|
26
|
+
"lib/rsolr-ext/response/spelling.rb",
|
26
27
|
"lib/rsolr-ext/response.rb",
|
27
28
|
|
28
29
|
"lib/rsolr-ext.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mwmitchell-rsolr-ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.31
|
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-05-
|
12
|
+
date: 2009-05-27 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- lib/rsolr-ext/request.rb
|
32
32
|
- lib/rsolr-ext/response/docs.rb
|
33
33
|
- lib/rsolr-ext/response/facets.rb
|
34
|
+
- lib/rsolr-ext/response/spelling.rb
|
34
35
|
- lib/rsolr-ext/response.rb
|
35
36
|
- lib/rsolr-ext.rb
|
36
37
|
- LICENSE
|