Class: ActsAsSolr::SearchResults

Inherits:
Object
  • Object
show all
Includes:
Enumerable
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


33
34
35
# File 'lib/acts_as_solr/search_results.rb', line 33

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object


121
122
123
124
125
# File 'lib/acts_as_solr/search_results.rb', line 121

def method_missing(symbol, *args, &block)
  self.results.send(symbol, *args, &block)
rescue NoMethodError
  raise NoMethodError, "There is no method called #{symbol} at #{self.class.name} - #{self.inspect}"
end

Instance Method Details

#blank?Boolean


93
94
95
# File 'lib/acts_as_solr/search_results.rb', line 93

def blank?
  total_entries == 0
end

#current_pageObject

Returns the current page


89
90
91
# File 'lib/acts_as_solr/search_results.rb', line 89

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

#each(&block) ⇒ Object


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

def each(&block)
  self.results.each &block
end

#facetsObject

Returns the facets when doing a faceted search


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

def facets
  @solr_data[:facets]
end

#highlightsObject

Returns the highlighted fields which one has asked for..


69
70
71
# File 'lib/acts_as_solr/search_results.rb', line 69

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


60
61
62
# File 'lib/acts_as_solr/search_results.rb', line 60

def max_score
  @solr_data[:max_score]
end

#next_pageObject


113
114
115
116
117
118
119
# File 'lib/acts_as_solr/search_results.rb', line 113

def next_page
  if current_page < total_pages
    current_page + 1
  else
    false
  end
end

#offsetObject


101
102
103
# File 'lib/acts_as_solr/search_results.rb', line 101

def offset
  (current_page - 1) * per_page
end

#per_pageObject

Returns the number of documents per page


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

def per_page
  @solr_data[:rows].to_i
end

#previous_pageObject


105
106
107
108
109
110
111
# File 'lib/acts_as_solr/search_results.rb', line 105

def previous_page
  if current_page > 1
    current_page - 1
  else
    false
  end
end

#query_timeObject


64
65
66
# File 'lib/acts_as_solr/search_results.rb', line 64

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


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

def results
  @solr_data[:docs]
end

#sizeObject


97
98
99
# File 'lib/acts_as_solr/search_results.rb', line 97

def size
  total_entries
end

#suggestObject

Returns a suggested query


74
75
76
# File 'lib/acts_as_solr/search_results.rb', line 74

def suggest
  Hash[@solr_data[:spellcheck]['suggestions']]['collation']
end

#totalObject Also known as: num_found, total_hits, total_entries

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


49
50
51
# File 'lib/acts_as_solr/search_results.rb', line 49

def total
  @solr_data[:total]
end

#total_pagesObject

Returns the number of pages found


84
85
86
# File 'lib/acts_as_solr/search_results.rb', line 84

def total_pages
  per_page.zero? ? 0 : (total / per_page.to_f).ceil
end