Class: ActsAsSolr::SearchResults

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_solr/search_results.rb

Overview

TODO: Possibly looking into hooking it up with Solr::Response::Standard

Class that returns the search results with four methods.

books = Book.find_by_solr 'ruby'

the above will return a SearchResults class with 4 methods:

docs|results|records: will return an array of records found

books.records.empty?
=> false

total|num_found|total_hits: will return the total number of records found

books.total
=> 2

facets: will return the facets when doing a faceted search

max_score|highest_score: returns the highest score found

books.max_score
=> 1.3213213

Instance Method Summary collapse

Constructor Details

#initialize(solr_data = {}) ⇒ SearchResults

Returns a new instance of SearchResults.



30
31
32
# File 'lib/acts_as_solr/search_results.rb', line 30

def initialize(solr_data={})
  @solr_data = solr_data
end

Instance Method Details

#current_pageObject

Returns the current page



82
83
84
# File 'lib/acts_as_solr/search_results.rb', line 82

def current_page
  (@solr_data[:start].to_i / per_page) + 1
end

#facetsObject

Returns the facets when doing a faceted search



47
48
49
# File 'lib/acts_as_solr/search_results.rb', line 47

def facets
  @solr_data[:facets]
end

#highlightsObject

Returns the highlighted fields which one has asked for..



62
63
64
# File 'lib/acts_as_solr/search_results.rb', line 62

def highlights
    @solr_data[:highlights]
end

#max_scoreObject Also known as: highest_score

Returns the highest score found. This method is also aliased as highest_score



53
54
55
# File 'lib/acts_as_solr/search_results.rb', line 53

def max_score
  @solr_data[:max_score]
end

#per_pageObject

Returns the number of documents per page



72
73
74
# File 'lib/acts_as_solr/search_results.rb', line 72

def per_page
  @solr_data[:rows].to_i
end

#query_timeObject



57
58
59
# File 'lib/acts_as_solr/search_results.rb', line 57

def query_time
  @solr_data[:query_time]
end

#resultsObject Also known as: docs, records

Returns an array with the instances. This method is also aliased as docs and records



36
37
38
# File 'lib/acts_as_solr/search_results.rb', line 36

def results
  @solr_data[:docs]
end

#suggestObject

Returns a suggested query



67
68
69
# File 'lib/acts_as_solr/search_results.rb', line 67

def suggest
  @solr_data[:spellcheck]['suggestions']['collation'].match(/\((.+)\) /)[1]
end

#totalObject Also known as: num_found, total_hits

Returns the total records found. This method is also aliased as num_found and total_hits



42
43
44
# File 'lib/acts_as_solr/search_results.rb', line 42

def total
  @solr_data[:total]
end

#total_pagesObject

Returns the number of pages found



77
78
79
# File 'lib/acts_as_solr/search_results.rb', line 77

def total_pages
  (total / per_page.to_f).ceil
end