search_results.rb
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
60
61
62
63
64
65
66
67
68
module ActsAsSolr #:nodoc:
# 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
#
#
class SearchResults
def initialize(solr_data={})
@solr_data = solr_data
end
# Returns an array with the instances. This method
# is also aliased as docs and records
def results
@solr_data[:docs]
end
# Returns the total records found. This method is
# also aliased as num_found and total_hits
def total
@solr_data[:total]
end
# Returns the facets when doing a faceted search
def facets
@solr_data[:facets]
end
# Returns the highest score found. This method is
# also aliased as highest_score
def max_score
@solr_data[:max_score]
end
def query_time
@solr_data[:query_time]
end
alias docs results
alias records results
alias num_found total
alias total_hits total
alias highest_score max_score
end
end