Class: UR::Search

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

Overview

Search for products and populate from the metadata cache

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solr_params) ⇒ Search

Returns a new instance of Search.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ur/search.rb', line 14

def initialize(solr_params)
  # Prepare the facets
  @products = []
  @facets = {}
  @solr   = nil
  
  begin 
    solr = RSolr.connect :url => SEARCH_SERVICE_URL
    response = solr.find solr_params
    
    # Populate the products from the Metadata Cache
    @products = (response.ok? && response.docs.size > 0) ?
      Product.find(response.docs.map { |d| d.id }) : []
    
    # Expose the Solr response
    @solr = response
    @solr.facets.map { |f| @facets[f.name] = f.items } if @solr.facets.size > 0
  rescue RSolr::RequestError => e
  end
end

Instance Attribute Details

#facetsObject (readonly)

Returns the value of attribute facets.



12
13
14
# File 'lib/ur/search.rb', line 12

def facets
  @facets
end

#productsObject (readonly)

Returns the value of attribute products.



12
13
14
# File 'lib/ur/search.rb', line 12

def products
  @products
end

#solrObject (readonly)

Returns the value of attribute solr.



12
13
14
# File 'lib/ur/search.rb', line 12

def solr
  @solr
end

Instance Method Details

#next_pageObject



47
48
49
# File 'lib/ur/search.rb', line 47

def next_page
  (ok? && !@solr.docs.nil?) ? @solr.docs.next_page : 0
end

#num_foundObject



39
40
41
# File 'lib/ur/search.rb', line 39

def num_found
  (ok?) ? @solr.response['numFound'].to_i : 0
end

#ok?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/ur/search.rb', line 35

def ok?
  !@solr.nil? && @solr.ok?
end

#previous_pageObject



43
44
45
# File 'lib/ur/search.rb', line 43

def previous_page
  (ok? && !@solr.docs.nil?) ? @solr.docs.previous_page : 0
end