Class: BlacklightBrowseNearby

Inherits:
Object
  • Object
show all
Includes:
Blacklight::Configurable, Blacklight::SolrHelper
Defined in:
lib/blacklight_browse_nearby.rb,
lib/blacklight_browse_nearby/engine.rb,
lib/blacklight_browse_nearby/version.rb

Defined Under Namespace

Modules: Controller Classes: Engine

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_id, options = {}) ⇒ BlacklightBrowseNearby

Returns a new instance of BlacklightBrowseNearby.



12
13
14
15
# File 'lib/blacklight_browse_nearby.rb', line 12

def initialize(object_id, options={})
  @opts = options
  @documents = get_nearby_documents(object_id)
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



11
12
13
# File 'lib/blacklight_browse_nearby.rb', line 11

def documents
  @documents
end

#original_documentObject (readonly)

Returns the value of attribute original_document.



11
12
13
# File 'lib/blacklight_browse_nearby.rb', line 11

def original_document
  @original_document
end

Instance Method Details

#combined_keyObject

Convenience method to return the combined key from the original document that is preferred_value aware. This is necessary for supporting multi-valued fields.



101
102
103
104
105
106
# File 'lib/blacklight_browse_nearby.rb', line 101

def combined_key
  return @original_document[combined_key_field].first if (@original_document[combined_key_field].length == 1 or @opts[:preferred_value].blank?)
  @original_document[combined_key_field].each do |key|
    return key if get_value_from_combined_key(key, value_field) == @opts[:preferred_value]
  end
end

#current_valueObject

Convenience method to return the value from the original document that is preferred_value aware. This is necessary for supporting the multi-valued field UI.



96
97
98
# File 'lib/blacklight_browse_nearby.rb', line 96

def current_value
  get_value_from_combined_key(combined_key, value_field)
end

#get_nearby_documents(id) ⇒ Object

Returns an array of documents “nearby” the given object ID based on page number. Negative page numbers will return documents “behind” the given object ID. No page number (or 0) will return documents “behind” the given object ID, the given object, and documents “in front of” the given object ID. Positive page numbers will return documents “in front of” the given object ID.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blacklight_browse_nearby.rb', line 21

def get_nearby_documents(id)
  @original_document = get_solr_response_for_doc_id(id).last # returns an array with a response object and the document.
  return [] unless document_has_required_fields?
  shelfkey = get_value_from_combined_key(combined_key, shelfkey_field)
  reverse_shelfkey = get_value_from_combined_key(combined_key, reverse_shelfkey_field)
  if normalized_page == 0
    previous_documents = get_next_documents_from_field_value(reverse_shelfkey, reverse_shelfkey_field)
    next_documents = get_next_documents_from_field_value(shelfkey, shelfkey_field)
    documents = [previous_documents, @original_document, next_documents].flatten
  elsif @opts[:page].to_i < 0
    documents = get_next_documents_from_field_value(reverse_shelfkey, reverse_shelfkey_field)
  elsif @opts[:page].to_i > 0
    documents = get_next_documents_from_field_value(shelfkey, shelfkey_field)
  end
  documents
end

#get_next_documents_from_field_value(value, field) ⇒ Object

Returns an array of documents forward of the given term in the given field. These documents are sorted by the configured solr field. Using solr’s termsComponent we request the next terms from the given field and value. This works for backward sorting by using a reverse-sort keys. We then request the documents for those terms from solr via Blacklight’s get_solr_response_for_field_values



42
43
44
45
# File 'lib/blacklight_browse_nearby.rb', line 42

def get_next_documents_from_field_value(value, field)
  terms = get_ordered_terms(value, field)
  get_solr_response_for_field_values(field, terms, :per_page=>terms.length).last.sort{|a,b| a[shelfkey_field] <=> b[shelfkey_field] }
end

#get_ordered_terms(value, field) ⇒ Object

Returns an array of the next terms using solr’s termsComponent. The number of terms requested/returned from solr may be much larger than what is returned by this method when paging. The pagination happens here and we paginate the returned terms before we request the related documents. This keeps the URLs free of sortkey values.



50
51
52
53
54
55
56
57
58
# File 'lib/blacklight_browse_nearby.rb', line 50

def get_ordered_terms(value, field)
  solr_options = {
    :"terms.fl"         => field,
    :"terms.lower"      => value,
    :"terms.limit"      => total_terms
  }
  response = Blacklight.solr.send_and_receive("#{BlacklightBrowseNearby::Engine.config.request_handler}", {:params=>solr_options})
  response["terms"][field].select{|term| term.is_a?(String) }[start_of_terms..(total_terms-1)]
end

#hits_requestedObject

Returns an integer representing the number of terms that were requested. Falls back to the provided configuration option in BlacklightBrowseNearby::Engine.config.default_hits



73
74
75
76
# File 'lib/blacklight_browse_nearby.rb', line 73

def hits_requested
  return BlacklightBrowseNearby::Engine.config.default_hits.to_i if @opts[:number].blank?
  @opts[:number].to_i
end

#normalized_pageObject

Returns an integer representing a normalized page number. This method will return a 0 in the absense of a page option and will turn any negative integer into a positive integer.



85
86
87
88
# File 'lib/blacklight_browse_nearby.rb', line 85

def normalized_page
  return 0 if @opts[:page].blank?
  @opts[:page].to_s.gsub("-","").to_i
end

#original_query_offsetObject

Returns an integer representing the number of items that would have been intitally requested from page 0. This is necessary to get the appropriate total_terms integer.



79
80
81
# File 'lib/blacklight_browse_nearby.rb', line 79

def original_query_offset
  (hits_requested - 1) / 2
end

#paramsObject

Returns a hash that will be used by BlacklightSolrHelper. The params hash can be passed in as an option on initialization (although the code isn’t currently doing that).



110
111
112
# File 'lib/blacklight_browse_nearby.rb', line 110

def params
  @opts[:params] || {}
end

#potential_valuesObject

Convenience method to return the value field from the original document. This is necessary for supportin the multi-valued field UI.



91
92
93
# File 'lib/blacklight_browse_nearby.rb', line 91

def potential_values
  @original_document[value_field]
end

#start_of_termsObject

Returns an integer representing the beginning of the range of terms we’ll request documents for.



61
62
63
64
# File 'lib/blacklight_browse_nearby.rb', line 61

def start_of_terms
  return 0 if normalized_page == 0
  total_terms - hits_requested
end

#total_termsObject

Returns an integer representing the total number of terms to request from solr.



67
68
69
70
# File 'lib/blacklight_browse_nearby.rb', line 67

def total_terms
  return original_query_offset if normalized_page == 0
  (hits_requested * normalized_page) + original_query_offset
end