Class: UR::SabSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/ur/sab_search.rb

Constant Summary collapse

SEARCH_SERVICE_URL =
'http://services.ur.se/search'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sab_code, options = {}) ⇒ SabSearch

Returns a new instance of SabSearch.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ur/sab_search.rb', line 15

def initialize(sab_code, options = {})
  solr = RSolr.connect :url => SEARCH_SERVICE_URL
  @sab = UR::Sab.new(sab_code) unless sab_code.nil?

  response = solr.find({
    :indent => 'on',
    :qt => 'sab',
    'facet.mincount' => 1,
    'facet.limit' => -1,
    'facet.prefix' => sab_code,
    :q => '*:*',
  }.merge(options))

  # Expose the Solr response
  @solr = response

  facet_counts = Hash[*response['facet_counts']['facet_fields']['sab_subjects']]

  sorted_subjects = facet_counts.keys.sort { |l,r| r <=> l }
  reduced_facet_counts = {}

  sorted_subjects.each_index do |j|
    current_code = sorted_subjects[j]
    next_code    = sorted_subjects[j + 1]
    count        = facet_counts[current_code]

    if !next_code.nil? && current_code.match(next_code)
      if facet_counts[next_code] != count
        reduced_facet_counts[current_code] = count
      end
    else
      reduced_facet_counts[current_code] = count
    end
  end

  @subjects = reduced_facet_counts.map { |code, count|
      if (@sab.nil? && code.length == 1) ||
         (!@sab.nil? && code.length < @sab.code.length+2) ||
         (!@sab.nil? && code.match(/#{@sab.code}\.\d$/)) ||
         (!@sab.nil? && code.match(/#{@sab.code}\:\w{1,2}$/))
        sab = UR::Sab.new(code, count)
        sab if sab.text
      end
  }.compact.sort { |l,r| l.code <=> r.code }
end

Instance Attribute Details

#sabObject (readonly)

Returns the value of attribute sab.



13
14
15
# File 'lib/ur/sab_search.rb', line 13

def sab
  @sab
end

#solrObject (readonly)

Returns the value of attribute solr.



13
14
15
# File 'lib/ur/sab_search.rb', line 13

def solr
  @solr
end

#subjectsObject (readonly)

Returns the value of attribute subjects.



13
14
15
# File 'lib/ur/sab_search.rb', line 13

def subjects
  @subjects
end