Class: Tire::Results::Collection
- Inherits:
-
Object
- Object
- Tire::Results::Collection
show all
- Includes:
- Enumerable, Pagination
- Defined in:
- lib/tire/results/collection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Pagination
#current_page, default_per_page, #first_page?, #last_page?, #next_page, #offset, #out_of_bounds?, #per_page, #previous_page, #total_entries, #total_pages
Constructor Details
#initialize(response, options = {}) ⇒ Collection
Returns a new instance of Collection.
10
11
12
13
14
15
16
17
18
|
# File 'lib/tire/results/collection.rb', line 10
def initialize(response, options={})
@response = response
@options = options
@time = response['took'].to_i
@total = response['hits']['total'].to_i rescue nil
@facets = response['facets']
@max_score = response['hits']['max_score'].to_f rescue nil
@wrapper = options[:wrapper] || Configuration.wrapper
end
|
Instance Attribute Details
#facets ⇒ Object
Returns the value of attribute facets.
8
9
10
|
# File 'lib/tire/results/collection.rb', line 8
def facets
@facets
end
|
#max_score ⇒ Object
Returns the value of attribute max_score.
8
9
10
|
# File 'lib/tire/results/collection.rb', line 8
def max_score
@max_score
end
|
#options ⇒ Object
Returns the value of attribute options.
8
9
10
|
# File 'lib/tire/results/collection.rb', line 8
def options
@options
end
|
#time ⇒ Object
Returns the value of attribute time.
8
9
10
|
# File 'lib/tire/results/collection.rb', line 8
def time
@time
end
|
#total ⇒ Object
Returns the value of attribute total.
8
9
10
|
# File 'lib/tire/results/collection.rb', line 8
def total
@total
end
|
Instance Method Details
#__find_records_by_ids(klass, ids) ⇒ Object
150
151
152
|
# File 'lib/tire/results/collection.rb', line 150
def __find_records_by_ids(klass, ids)
@options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load])
end
|
#__get_results_with_load(hits) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/tire/results/collection.rb', line 122
def __get_results_with_load(hits)
return [] if hits.empty?
records = {}
@response['hits']['hits'].group_by { |item| item['_type'] }.each do |type, items|
raise NoMethodError, "You have tried to eager load the model instances, " +
"but Tire cannot find the model class because " +
"document has no _type property." unless type
begin
klass = type.camelize.constantize
rescue NameError => e
raise NameError, "You have tried to eager load the model instances, but " +
"Tire cannot find the model class '#{type.camelize}' " +
"based on _type '#{type}'.", e.backtrace
end
records[type] = __find_records_by_ids klass, items.map { |h| h['_id'] }
end
@response['hits']['hits'].map do |item|
records[item['_type']].detect do |record|
record.id.to_s == item['_id'].to_s
end
end
end
|
#__get_results_without_load(hits) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/tire/results/collection.rb', line 102
def __get_results_without_load(hits)
if @wrapper == Hash
hits
else
hits.map do |h|
document = {}
document = h['_source'] ? document.update( h['_source'] || {} ) : document.update( __parse_fields__(h['fields']) )
document.update( {'id' => h['_id']} )
['_score', '_type', '_index', '_version', 'sort', 'highlight', '_explanation'].each { |key| document.update( {key => h[key]} || {} ) }
@wrapper.new(document)
end
end
end
|
#__parse_fields__(fields = {}) ⇒ Object
Handles _source prefixed fields properly: strips the prefix and converts fields to nested Hashes
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/tire/results/collection.rb', line 82
def __parse_fields__(fields={})
( fields ||= {} ).clone.each_pair do |key,value|
next unless key.to_s =~ /_source/
keys = key.to_s.split('.').reject { |n| n == '_source' }
fields.delete(key)
result = {}
path = []
keys.each do |name|
path << name
eval "result[:#{path.join('][:')}] ||= {}"
eval "result[:#{path.join('][:')}] = #{value.inspect}" if keys.last == name
end
fields.update result
end
fields
end
|
#as_json(options = nil) ⇒ Object
64
65
66
|
# File 'lib/tire/results/collection.rb', line 64
def as_json(options=nil)
to_a.map { |item| item.as_json(options) }
end
|
#each(&block) ⇒ Object
Iterates over the results
collection
34
35
36
|
# File 'lib/tire/results/collection.rb', line 34
def each(&block)
results.each(&block)
end
|
#each_with_hit(&block) ⇒ Object
Iterates over the results
collection and yields
the result
object (Item or model instance) and the
hit
-- raw Elasticsearch response parsed as a Hash
42
43
44
|
# File 'lib/tire/results/collection.rb', line 42
def each_with_hit(&block)
results.zip(@response['hits']['hits']).each(&block)
end
|
#empty? ⇒ Boolean
46
47
48
|
# File 'lib/tire/results/collection.rb', line 46
def empty?
results.empty?
end
|
#error ⇒ Object
68
69
70
|
# File 'lib/tire/results/collection.rb', line 68
def error
@response['error']
end
|
#failure? ⇒ Boolean
76
77
78
|
# File 'lib/tire/results/collection.rb', line 76
def failure?
! success?
end
|
#results ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/tire/results/collection.rb', line 20
def results
return [] if failure?
@results ||= begin
hits = @response['hits']['hits'].map { |d| d.update '_type' => Utils.unescape(d['_type']) }
unless @options[:load]
__get_results_without_load(hits)
else
__get_results_with_load(hits)
end
end
end
|
#size ⇒ Object
Also known as:
length
50
51
52
|
# File 'lib/tire/results/collection.rb', line 50
def size
results.size
end
|
#slice(*args) ⇒ Object
Also known as:
[]
55
56
57
|
# File 'lib/tire/results/collection.rb', line 55
def slice(*args)
results.slice(*args)
end
|
#success? ⇒ Boolean
72
73
74
|
# File 'lib/tire/results/collection.rb', line 72
def success?
error.to_s.empty?
end
|
#to_ary ⇒ Object
60
61
62
|
# File 'lib/tire/results/collection.rb', line 60
def to_ary
self
end
|