mwmitchell-rsolr-ext 0.7.4 → 0.7.5

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 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 = RSolr::Ext.map_params(
6
6
  :page=>2,
7
7
  :per_page=>10,
8
8
  :phrases=>{:name=>'This is a phrase'},
@@ -10,39 +10,53 @@ 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::Ext.connect
15
+ rsolr = RSolr.connect
16
16
 
17
17
  response = rsolr.select(solr_params)
18
18
 
19
19
  ==Response Example
20
- rsolr = RSolr::Ext.connect
20
+ rsolr = RSolr.connect
21
21
 
22
- response = rsolr.select(:q=>'*:*)
22
+ raw_response = rsolr.select(:q=>'*:*)
23
+ r = RSolr::Ext::wrap_response(raw_response)
23
24
 
24
- response.ok?
25
- response.params
26
- response.docs
27
- response.docs.previous_page
28
- response.docs.next_page
29
- response.facets
25
+ r.ok?
26
+ r.params
27
+ r.docs
28
+ r.docs.previous_page
29
+ r.docs.next_page
30
+ r.facets
30
31
 
31
32
  You can access values in the response hash using symbols or strings.
32
33
 
33
34
  ===Doc Pagination
34
- If you wanna paginate, just throw the collection into the WillPaginate view helper.
35
- <%= will_paginate response.docs %>
35
+ After creating a RSolr::Ext::Response object, pass-in the response.docs to the will_paginate view helper:
36
+ rsolr = RSolr.connect
37
+ raw_response = rsolr.select(:q=>'*:*)
38
+ @response = RSolr::Ext.wrap_response(raw_response)
39
+ # in view:
40
+ <%= will_paginate @response.docs %>
36
41
 
37
- ===The Doc Module
38
- You can create your own "models" using RSolr::Ext::Doc
42
+ ==The Findable Module
39
43
 
40
- class Book
41
- include RSolr::Ext::Doc
42
- def self.find_by_author(author)
43
- find(:fq=>'object_type:"book"', :rows=>10, :phrase_filters=>{:author=>author})
44
- end
45
- end
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'}
46
60
 
47
- all_books = Book.find('*:*')
48
- hawk_books = Book.find_by_author('hawk')
61
+ # q=something -- the entire response
62
+ solr_response = solr.find {:q=>'something'}, :include_response=>true
data/lib/rsolr-ext.rb CHANGED
@@ -23,7 +23,7 @@ module RSolr
23
23
 
24
24
  module Ext
25
25
 
26
- VERSION = '0.7.35'
26
+ VERSION = '0.7.5'
27
27
 
28
28
  autoload :Request, 'rsolr-ext/request.rb'
29
29
  autoload :Response, 'rsolr-ext/response.rb'
data/lib/rsolr-ext/doc.rb CHANGED
@@ -41,19 +41,30 @@ module RSolr::Ext::Doc
41
41
  #
42
42
  module Findable
43
43
 
44
- attr_accessor :connection
44
+ attr_accessor :default_params, :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 ||= {:qt=>:standard, :rows=>10}
52
+ end
53
+
50
54
  def find(*args)
51
- mode, solr_params, opts = connection.send(:extract_find_opts!, *args)
52
- connection.find(*[mode, solr_params, opts]) { |doc| self.new(doc) }
55
+ call_finder_method(:find, *args)
56
+ end
57
+
58
+ def find_by_id(*args)
59
+ call_finder_method(:find_by_id, *args)
53
60
  end
54
61
 
55
- def find_by_id(id, solr_params={}, opts={})
56
- connection.find_by_id(id, solr_params, opts) { |doc| self.new(doc) }
62
+ protected
63
+
64
+ def call_finder_method(method_name, *args)
65
+ mode, params, opts = connection.send(:extract_find_opts!, *args)
66
+ params.merge!(default_params)
67
+ connection.send(method_name, *[mode, params, opts]) { |doc| self.new(doc) }
57
68
  end
58
69
 
59
70
  end
@@ -63,21 +74,16 @@ module RSolr::Ext::Doc
63
74
  def self.included(base)
64
75
  base.extend Callbacks
65
76
  base.extend Findable
66
- base.send :include, RSolr::Ext::Response::Docs::Accessible
67
77
  end
68
78
 
69
79
  # The original object passed in to the #new method
70
80
  attr :_source_hash
71
81
 
72
- # The original object passed, converted to a mash
73
- attr :_source_mash
74
-
75
82
  # Constructor **for the class that is getting this module included**
76
83
  # source_doc should be a hash or something similar
77
84
  # calls each of after_initialize blocks
78
85
  def initialize(source_doc={})
79
86
  @_source_hash = source_doc
80
- @_source_mash = source_doc.to_mash
81
87
  self.class.hooks.each do |h|
82
88
  instance_eval &h
83
89
  end
@@ -85,14 +91,14 @@ module RSolr::Ext::Doc
85
91
 
86
92
  # for easy access to the solr id (route helpers etc..)
87
93
  def id
88
- @_source_mash['id']
94
+ @_source_hash['id']
89
95
  end
90
96
 
91
97
  # the wrapper method to the @_source_hash object.
92
98
  # If a method is missing, it gets sent to @_source_hash
93
99
  # with all of the original params and block
94
100
  def method_missing(m, *args, &b)
95
- @_source_mash.send(m, *args, &b)
101
+ @_source_hash.send(m, *args, &b)
96
102
  end
97
103
 
98
104
  end
@@ -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] ||= true
37
+ opts[:include_response] = false unless opts.key?(:include_response)
38
38
 
39
39
  solr_params[:rows] = 1 if mode == :first
40
40
  valid_solr_params = RSolr::Ext.map_params(solr_params)
@@ -64,14 +64,9 @@ module RSolr::Ext::Findable
64
64
  end
65
65
 
66
66
  # find_by_id(10, :handler=>'catalog')
67
- # find_by_id(:id=>10)
68
67
  def find_by_id(id, solr_params={}, opts={}, &blk)
69
- if id.respond_to?(:each_pair)
70
- solr_params = id
71
- else
72
- solr_params[:phrases] ||= {}
73
- solr_params[:phrases][:id] = id.to_s
74
- end
68
+ solr_params[:phrases] ||= {}
69
+ solr_params[:phrases][:id] = id
75
70
  self.find(:first, solr_params, opts, &blk)
76
71
  end
77
72
 
@@ -79,14 +74,16 @@ module RSolr::Ext::Findable
79
74
 
80
75
  def extract_find_opts!(*args)
81
76
  mode = :all
77
+ # extract the mode (:all or :first)
82
78
  valid_modes = [:all, :first]
83
79
  if args[0].is_a?(Symbol)
84
80
  mode = valid_modes.include?(args[0]) ? args.shift : raise("Invalid find mode; should be :first or :all")
85
81
  end
86
82
  # extract solr params
87
- solr_params = args.shift
83
+ solr_params = args.shift || {}
84
+ # create a hash if a string was passed in...
88
85
  unless solr_params.respond_to?(:each_pair)
89
- solr_params = {:q=>solr_params.to_s}
86
+ solr_params = {:q=>solr_params}
90
87
  end
91
88
  # extract options
92
89
  opts = args.shift || {}
@@ -32,11 +32,6 @@ module RSolr::Ext::Request::Queryable
32
32
  return value.collect do |(k,v)|
33
33
  if v.is_a?(Range)
34
34
  "#{k}:#{build_range(v)}"
35
- # If the value is an array, we want the same param, multiple times (not a query join)
36
- elsif v.is_a?(Array)
37
- v.collect do |vv|
38
- "#{k}:#{build_query(vv, quote_string)}"
39
- end
40
35
  else
41
36
  "#{k}:#{build_query(v, quote_string)}"
42
37
  end
@@ -2,7 +2,6 @@ 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'
6
5
 
7
6
  class Base < Mash
8
7
 
@@ -25,18 +24,12 @@ module RSolr::Ext::Response
25
24
 
26
25
  def initialize(*args)
27
26
  super(*args)
28
- extend Docs
29
- extend Facets
30
- extend Spelling
27
+ extend Docs if self['response']['docs']
28
+ extend Facets if key?('facet_counts')
31
29
  end
32
30
 
33
31
  def response
34
- self[:response]
35
- end
36
-
37
- # short cut to response['numFound']
38
- def total
39
- response[:numFound]
32
+ self['response']
40
33
  end
41
34
 
42
35
  end
@@ -1,6 +1,6 @@
1
1
  module RSolr::Ext::Response::Docs
2
2
 
3
- module Accessible
3
+ module Accessable
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,12 +83,10 @@ 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 do |item|
87
- item.extend Accessible
88
- end
86
+ d.each{|item|item.extend Accessable}
87
+ d.start = base['responseHeader']['params']['start'].to_s.to_i
89
88
  d.per_page = base['responseHeader']['params']['rows'].to_s.to_i
90
- d.start = base['response']['start'].to_s.to_i
91
- d.total = base['response']['numFound'].to_s.to_i
89
+ d.total = base['responseHeader']['numFound'].to_s.to_i
92
90
  end
93
91
 
94
92
  def docs
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"
4
- s.date = "2009-07-31"
3
+ s.version = "0.7.5"
4
+ s.date = "2009-05-04"
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,8 +23,6 @@ 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",
27
-
28
26
  "lib/rsolr-ext/response.rb",
29
27
 
30
28
  "lib/rsolr-ext.rb",
@@ -40,7 +38,5 @@ Gem::Specification.new do |s|
40
38
  'test/test_unit_test_case.rb',
41
39
  'test/helper.rb'
42
40
  ]
43
-
44
41
  s.extra_rdoc_files = %w(LICENSE README.rdoc)
45
-
46
42
  end
data/test/request_test.rb CHANGED
@@ -25,10 +25,10 @@ class RSolrExtRequestTest < Test::Unit::TestCase
25
25
  test 'fq param using the phrase_filters mapping' do
26
26
  std = RSolr::Ext::Request::Standard.new
27
27
  solr_params = std.map(
28
- :phrase_filters=>{:manu=>['Apple', 'ASG'], :color=>['red', 'blue']}
28
+ :phrase_filters=>[{:manu=>'Apple'}, {:color=>'red'}]
29
29
  )
30
- expected = {:fq=>["color:\"red\"", "color:\"blue\"", "manu:\"Apple\"", "manu:\"ASG\""]}
31
- assert expected, solr_params
30
+ assert solr_params[:fq].is_a?(Array)
31
+ assert solr_params[:fq].first.is_a?(String)
32
32
  end
33
33
 
34
34
  end
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
4
+ version: 0.7.5
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-07-31 00:00:00 -07:00
12
+ date: 2009-05-04 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -31,7 +31,6 @@ 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
35
34
  - lib/rsolr-ext/response.rb
36
35
  - lib/rsolr-ext.rb
37
36
  - LICENSE
@@ -39,7 +38,6 @@ files:
39
38
  - rsolr-ext.gemspec
40
39
  has_rdoc: true
41
40
  homepage: http://github.com/mwmitchell/rsolr_ext
42
- licenses:
43
41
  post_install_message:
44
42
  rdoc_options: []
45
43
 
@@ -60,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
58
  requirements: []
61
59
 
62
60
  rubyforge_project:
63
- rubygems_version: 1.3.5
61
+ rubygems_version: 1.2.0
64
62
  signing_key:
65
63
  specification_version: 2
66
64
  summary: An extension lib for RSolr
@@ -1,65 +0,0 @@
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
- word_suggestions = []
28
- spellcheck = self.response[:spellcheck]
29
- if spellcheck && spellcheck[:suggestions]
30
- suggestions = spellcheck[:suggestions]
31
- unless 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
- word_suggestions << term_info['suggestion']['word'] if suggFreq > origFreq
56
- end
57
- end
58
- end
59
- word_suggestions.uniq
60
- )
61
- end
62
-
63
- end
64
-
65
- end