Class: Blacklight::Solr::FacetPaginator

Inherits:
Object
  • Object
show all
Defined in:
vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb

Overview

Pagination for facet values — works by setting the limit to max displayable. You have to ask Solr for limit+1, to get enough results to see if ‘more’ are available’. That is, the all_facet_values arg in constructor should be the result of asking solr for limit+1 values. This is a workaround for the fact that Solr itself can’t compute the total values for a given facet field, so we cannot know how many “pages” there are.

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FacetPaginator) initialize(all_facet_values, arguments)

all_facet_values is a list of facet value objects returned by solr, asking solr for n+1 facet values. options: :limit => number to display per page, or (default) nil. Nil means

           display all with no previous or next. 

:offset => current item offset, default 0 :sort => ‘count’ or ‘index’, solr tokens for facet value sorting, default ‘count’.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 32

def initialize(all_facet_values, arguments)
  # to_s.to_i will conveniently default to 0 if nil
  @offset = arguments[:offset].to_s.to_i 
  @limit =  arguments[:limit].to_s.to_i if arguments[:limit]           
  # count is solr's default
  @sort = arguments[:sort] || "count" 
  
  total = all_facet_values.size
  if (@limit)
    @items = all_facet_values.slice(0, @limit)
    @has_next = total > @limit
    @has_previous = @offset > 0
  else # nil limit
    @items = all_facet_values
    @has_next = false
    @has_previous = false
  end
end

Class Attribute Details

+ (Object) request_keys

Returns the value of attribute request_keys



20
21
22
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 20

def request_keys
  @request_keys
end

Instance Attribute Details

- (Object) items (readonly)

Returns the value of attribute items



23
24
25
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 23

def items
  @items
end

- (Object) limit (readonly)

Returns the value of attribute limit



23
24
25
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 23

def limit
  @limit
end

- (Object) offset (readonly)

Returns the value of attribute offset



23
24
25
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 23

def offset
  @offset
end

- (Object) sort (readonly)

Returns the value of attribute sort



23
24
25
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 23

def sort
  @sort
end

- (Object) total (readonly)

Returns the value of attribute total



23
24
25
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 23

def total
  @total
end

Instance Method Details

- (Boolean) has_next?

Returns:

  • (Boolean)


51
52
53
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 51

def has_next?
  @has_next
end

- (Boolean) has_previous?

Returns:

  • (Boolean)


65
66
67
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 65

def has_previous?
  @has_previous
end

- (Object) params_for_next_url(params)

Pass in your current request params, returns a param hash suitable to passing to an ActionHelper method (resource-based url_for, or link_to or url_for) navigating to the next facet value batch. Returns nil if there is no has_next?



59
60
61
62
63
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 59

def params_for_next_url(params)
  return nil unless has_next?
  
  return params.merge(request_keys[:offset] => offset + limit )
end

- (Object) params_for_previous_url(params)

Pass in your current request params, returns a param hash suitable to passing to an ActionHelper method (resource-based url_for, or link_to or url_for) navigating to the previous facet value batch. Returns nil if there is no has_previous?



73
74
75
76
77
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 73

def params_for_previous_url(params)
  return nil unless has_previous?

  return params.merge(request_keys[:offset] => offset - limit )
end

- (Object) params_for_resort_url(sort_method, params)

Pass in a desired solr facet solr key (‘count’ or ‘index’, see wiki.apache.org/solr/SimpleFacetParameters#facet.limit under facet.sort ), and your current request params. Get back params suitable to passing to an ActionHelper method for creating a url, to resort by that method.



84
85
86
87
88
89
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 84

def params_for_resort_url(sort_method, params)
  # When resorting, we've got to reset the offset to start at beginning,
  # no way to make it make sense otherwise.
  return params.merge(request_keys[:sort] => sort_method,
                      request_keys[:offset] => 0)
end

- (Object) request_keys

shortcut



21
# File 'vendor/plugins/blacklight/lib/blacklight/solr/facet_paginator.rb', line 21

def request_keys ; self.class.request_keys  end