rsolr-ext 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -25,6 +25,13 @@ The #luke method returns a Hash/Mash result of a /admin/luke?numTerms=0 request:
25
25
  ===#find
26
26
  The #find method listens for certain keys. All other keys are ignored, allowing the ability to mix-and-match special keys with normal Solr param keys. The recognized keys are describe below.
27
27
 
28
+
29
+ :page - This maps to the Solr "start" param. The current "page" in the results set. RSolr::Ext handles the page-to-rows math for you.
30
+
31
+
32
+ :per_page - This maps to the Solr "rows" param. How many "pages" in the result.
33
+
34
+
28
35
  :queries - This key maps to the Solr "q" param. Accepts a string, array or hash. When an array is used, each value is joined by a space. When a hash is used, the keys are used as Solr fields.
29
36
 
30
37
  * :queries => 'normal' BECOMES ?q=normal
@@ -66,6 +73,8 @@ The #find method listens for certain keys. All other keys are ignored, allowing
66
73
  ==Request Example
67
74
  solr = RSolr::Ext.connect
68
75
  solr_params = {
76
+ :page=>2,
77
+ :per_page=>10,
69
78
  :phrases=>{:name=>'This is a phrase'},
70
79
  :filters=>['test', {:price=>(1..10)}],
71
80
  :phrase_filters=>{:manu=>['Apple']},
@@ -85,6 +94,8 @@ RSolr::Ext decorates the normal output hash from RSolr and adds some helpful met
85
94
  response.ok?
86
95
  response.params
87
96
  response.docs
97
+ response.docs.previous_page
98
+ response.docs.next_page
88
99
  response.facets.each do |facet|
89
100
  puts facet.name
90
101
  facet.items.each do |item|
@@ -94,6 +105,10 @@ RSolr::Ext decorates the normal output hash from RSolr and adds some helpful met
94
105
 
95
106
  You can access values in the response hash using symbols or strings.
96
107
 
108
+ ===Documents/Pagination
109
+ If you wanna paginate, just throw the collection into the WillPaginate view helper.
110
+ <%= will_paginate response.docs %>
111
+
97
112
  ==The "Model" Module
98
113
  You can create your own <read-only> "models" using RSolr::Ext::Model
99
114
 
@@ -4,6 +4,18 @@ module RSolr::Ext::Request
4
4
 
5
5
  def map input
6
6
  output = {}
7
+
8
+ if input[:per_page]
9
+ output[:rows] = input.delete(:per_page).to_i
10
+ end
11
+
12
+ if page = input.delete(:page)
13
+ raise ':per_page must be set when using :page' unless output[:rows]
14
+ page = page.to_s.to_i-1
15
+ page = page < 1 ? 0 : page
16
+ output[:start] = page * output[:rows]
17
+ end
18
+
7
19
  if queries = input.delete(:queries)
8
20
  output[:q] = append_to_param output[:q], build_query(queries, false)
9
21
  end
@@ -1,8 +1,52 @@
1
1
  module RSolr::Ext::Response::Docs
2
2
 
3
+ module Pageable
4
+
5
+ attr_accessor :start, :per_page, :total
6
+
7
+ # Returns the current page calculated from 'rows' and 'start'
8
+ # WillPaginate hook
9
+ def current_page
10
+ return 1 if start < 1
11
+ per_page_normalized = per_page < 1 ? 1 : per_page
12
+ @current_page ||= (start / per_page_normalized).ceil + 1
13
+ end
14
+
15
+ # Calcuates the total pages from 'numFound' and 'rows'
16
+ # WillPaginate hook
17
+ def total_pages
18
+ @total_pages ||= per_page > 0 ? (total / per_page.to_f).ceil : 1
19
+ end
20
+
21
+ # returns the previous page number or 1
22
+ # WillPaginate hook
23
+ def previous_page
24
+ @previous_page ||= (current_page > 1) ? current_page - 1 : 1
25
+ end
26
+
27
+ # returns the next page number or the last
28
+ # WillPaginate hook
29
+ def next_page
30
+ @next_page ||= (current_page == total_pages) ? total_pages : current_page+1
31
+ end
32
+
33
+ def has_next?
34
+ current_page < total_pages
35
+ end
36
+
37
+ def has_previous?
38
+ current_page > 1
39
+ end
40
+
41
+ end
42
+
3
43
  def self.extended(base)
4
44
  d = base['response']['docs']
5
45
  d.each{|doc| doc.extend RSolr::Ext::Doc }
46
+ d.extend Pageable
47
+ d.per_page = base['responseHeader']['params']['rows'].to_s.to_i
48
+ d.start = base['response']['start'].to_s.to_i
49
+ d.total = base['response']['numFound'].to_s.to_i
6
50
  end
7
51
 
8
52
  def docs
@@ -1,11 +1,19 @@
1
1
  module RSolr::Ext::Response::Facets
2
2
 
3
3
  # represents a facet value; which is a field value and its hit count
4
- FacetItem = Struct.new :value,:hits
4
+ class FacetItem
5
+ attr_reader :value, :hits
6
+ def initialize value, hits
7
+ @value, @hits = value, hits
8
+ end
9
+ end
5
10
 
6
11
  # represents a facet; which is a field and its values
7
- FacetField = Struct.new :name, :items do
8
- def items; @items ||= [] end
12
+ class FacetField
13
+ attr_reader :name, :items
14
+ def initialize name, items
15
+ @name, @items = name, items
16
+ end
9
17
  end
10
18
 
11
19
  # @response.facets.each do |facet|
@@ -16,11 +24,10 @@ module RSolr::Ext::Response::Facets
16
24
  def facets
17
25
  @facets ||= (
18
26
  facet_fields.map do |(facet_field_name,values_and_hits)|
19
- facet = FacetField.new(facet_field_name)
20
- (values_and_hits.size/2).times do |index|
21
- facet.items << FacetItem.new(values_and_hits[index*2], values_and_hits[index*2+1])
27
+ items = (values_and_hits.size/2).times.map do |index|
28
+ FacetItem.new(values_and_hits[index*2], values_and_hits[index*2+1])
22
29
  end
23
- facet
30
+ FacetField.new(facet_field_name, items)
24
31
  end
25
32
  )
26
33
  end
data/lib/rsolr-ext.rb CHANGED
@@ -21,7 +21,7 @@ module RSolr
21
21
 
22
22
  module Ext
23
23
 
24
- VERSION = '0.10.1'
24
+ VERSION = '0.11.0'
25
25
 
26
26
  autoload :Connection, 'rsolr-ext/connection.rb'
27
27
  autoload :Doc, 'rsolr-ext/doc.rb'
data/rsolr-ext.gemspec CHANGED
@@ -1,7 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
+
2
3
  s.name = "rsolr-ext"
3
- s.version = "0.10.1"
4
- s.date = "2009-11-13"
4
+ s.version = "0.11.0"
5
+ s.date = "2009-11-17"
5
6
 
6
7
  s.summary = "An extension lib for RSolr"
7
8
  s.email = "goodieboy@gmail.com"
@@ -34,6 +35,6 @@ Gem::Specification.new do |s|
34
35
 
35
36
  s.extra_rdoc_files = %w(LICENSE README.rdoc)
36
37
 
37
- s.add_dependency("rsolr", ["=0.10.1"])
38
+ s.add_dependency("rsolr", ["=0.11.0"])
38
39
 
39
40
  end
@@ -11,8 +11,10 @@ class RSolrExtConnectionTest < Test::Unit::TestCase
11
11
 
12
12
  test 'the #find method' do
13
13
  connection = RSolr::Ext.connect
14
- response = connection.find :q=>'*:*'
14
+ response = connection.find 3, 10, :q=>'*:*'#, :page=>1, :per_page=>10
15
15
  assert response.kind_of?(Mash)
16
+ #r = connection.find 3, 5, :q=>'*:*', :phrase_queries=>'rose'
17
+ #assert ! r.header[:params].include?(:phrase_queries)
16
18
  end
17
19
 
18
20
  test 'the #find method with a custom request handler' do
data/test/request_test.rb CHANGED
@@ -5,8 +5,8 @@ class RSolrExtRequestTest < Test::Unit::TestCase
5
5
 
6
6
  test 'standard request' do
7
7
  solr_params = RSolr::Ext::Request.map(
8
- :start=>10,
9
- :rows=>10,
8
+ :page=>'2',
9
+ :per_page=>'10',
10
10
  :phrases=>{:name=>'This is a phrase'},
11
11
  :filters=>['test', {:price=>(1..10)}],
12
12
  :phrase_filters=>{:manu=>['Apple']},
@@ -18,6 +18,8 @@ class RSolrExtResponseTest < Test::Unit::TestCase
18
18
  assert r.ok?
19
19
  assert_equal 11, r.docs.size
20
20
  assert_equal 'EXPLICIT', r.params[:echoParams]
21
+ assert_equal 1, r.docs.previous_page
22
+ assert_equal 2, r.docs.next_page
21
23
  #
22
24
  assert r.kind_of?(RSolr::Ext::Response::Docs)
23
25
  assert r.kind_of?(RSolr::Ext::Response::Facets)
@@ -47,6 +49,7 @@ class RSolrExtResponseTest < Test::Unit::TestCase
47
49
 
48
50
  first_facet = r.facets.first
49
51
  assert_equal 'cat', first_facet.name
52
+
50
53
  assert_equal 10, first_facet.items.size
51
54
 
52
55
  expected = first_facet.items.collect do |item|
@@ -71,4 +74,33 @@ class RSolrExtResponseTest < Test::Unit::TestCase
71
74
  assert_equal 'cat', facet.name
72
75
  end
73
76
 
77
+ =begin
78
+
79
+ # pagination for facets has been commented out in the response/facets module.
80
+ # ...need to think more about how this can be handled
81
+
82
+ test 'response::standard facets.paginate' do
83
+ raw_response = eval(mock_query_response)
84
+ raw_response['responseHeader']['params']['facet.offset'] = 1
85
+ raw_response['responseHeader']['params']['facet.limit'] = 2
86
+
87
+ r = RSolr::Ext::Response::Standard.new(raw_response)
88
+
89
+ assert_equal 2, r.facets.current_page
90
+
91
+ # always 1 less than facet.limit
92
+ assert_equal 1, r.facets.per_page
93
+
94
+ assert_equal 3, r.facets.next_page
95
+
96
+ assert_equal 1, r.facets.previous_page
97
+
98
+ # can't know how many pages there are with facets.... so we set it to -1
99
+ assert_equal -1, r.facets.total_pages
100
+
101
+ assert r.facets.has_next?
102
+ assert r.facets.has_previous?
103
+ end
104
+ =end
105
+
74
106
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsolr-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
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-13 00:00:00 -05:00
12
+ date: 2009-11-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.10.1
23
+ version: 0.11.0
24
24
  version:
25
25
  description: An extension lib for RSolr
26
26
  email: goodieboy@gmail.com