Class: ArtirixDataModels::EsCollection
- Inherits:
-
Object
- Object
- ArtirixDataModels::EsCollection
show all
- Includes:
- Enumerable
- Defined in:
- lib/artirix_data_models/es_collection.rb
Defined Under Namespace
Modules: KaminariEsCollection, WillPaginateEsCollection
Constant Summary
collapse
- DEFAULT_SIZE =
10
- CACHE_KEY_SECTION_SEPARATOR =
'/'.freeze
- CACHE_KEY_RESULT_SEPARATOR =
'\\'.freeze
- EMPTY_RESPONSE =
Oj.load(<<-JSON, symbol_keys: true)
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
JSON
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass_or_factory, response:, from: 0, size: DEFAULT_SIZE, aggregations_factory: nil) ⇒ EsCollection
Returns a new instance of EsCollection.
78
79
80
81
82
83
84
|
# File 'lib/artirix_data_models/es_collection.rb', line 78
def initialize(klass_or_factory, response:, from: 0, size: DEFAULT_SIZE, aggregations_factory: nil)
@klass_or_factory = klass_or_factory
@response = response
@from = from
@size = size
@aggregations_factory = aggregations_factory
end
|
Instance Attribute Details
#from ⇒ Object
Returns the value of attribute from.
70
71
72
|
# File 'lib/artirix_data_models/es_collection.rb', line 70
def from
@from
end
|
#klass_or_factory ⇒ Object
Returns the value of attribute klass_or_factory.
70
71
72
|
# File 'lib/artirix_data_models/es_collection.rb', line 70
def klass_or_factory
@klass_or_factory
end
|
#response ⇒ Object
Returns the value of attribute response.
70
71
72
|
# File 'lib/artirix_data_models/es_collection.rb', line 70
def response
@response
end
|
#size ⇒ Object
Returns the value of attribute size.
70
71
72
|
# File 'lib/artirix_data_models/es_collection.rb', line 70
def size
@size
end
|
Class Method Details
.empty(model_class, from: 0, size: DEFAULT_SIZE) ⇒ Object
37
38
39
|
# File 'lib/artirix_data_models/es_collection.rb', line 37
def self.empty(model_class, from: 0, size: DEFAULT_SIZE)
new model_class, response: EMPTY_RESPONSE, from: from, size: size
end
|
.from_array(array, total: nil, page_number: nil, per_page: nil) ⇒ Object
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
|
# File 'lib/artirix_data_models/es_collection.rb', line 41
def self.from_array(array, total: nil, page_number: nil, per_page: nil)
self.new(-> (x) { x }, response: {}).tap do |obj|
total ||= array.length
per_page = per_page.to_i
from = 0
size = total
slice = array
if per_page > 0
page_number = page_number.to_i
page_number = 1 if page_number < 1
from = (page_number - 1) * per_page
size = per_page
slice = array.drop(from).take(per_page)
end
obj.instance_variable_set(:@results, slice)
obj.instance_variable_set(:@hits, { hits: slice })
obj.instance_variable_set(:@total, total)
obj.instance_variable_set(:@from, from)
obj.instance_variable_set(:@size, size)
obj.instance_variable_set(:@max_score, 1)
end
end
|
.work_with_kaminari ⇒ Object
10
11
12
13
|
# File 'lib/artirix_data_models/es_collection.rb', line 10
def self.work_with_kaminari
require 'kaminari'
include KaminariEsCollection
end
|
.work_with_will_paginate ⇒ Object
15
16
17
18
|
# File 'lib/artirix_data_models/es_collection.rb', line 15
def self.work_with_will_paginate
require 'will_paginate'
include WillPaginateEsCollection
end
|
Instance Method Details
#aggregation(name) ⇒ Object
112
113
114
115
|
# File 'lib/artirix_data_models/es_collection.rb', line 112
def aggregation(name)
n = name.to_sym
aggregations.detect { |x| x.name == n }
end
|
#aggregations ⇒ Object
108
109
110
|
# File 'lib/artirix_data_models/es_collection.rb', line 108
def aggregations
@aggregations ||= build_aggregations
end
|
#aggregations_factory ⇒ Object
#current_page ⇒ Object
142
143
144
|
# File 'lib/artirix_data_models/es_collection.rb', line 142
def current_page
from / size + 1 if from && size
end
|
#data_hash(&block) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/artirix_data_models/es_collection.rb', line 123
def data_hash(&block)
block ||= :data_hash
{
size: size,
from: from,
total: total,
max_score: max_score,
aggregations: aggregations.map(&:data_hash),
hits: results.map(&block),
}
end
|
#hits ⇒ Object
104
105
106
|
# File 'lib/artirix_data_models/es_collection.rb', line 104
def hits
@hits ||= response[:hits]
end
|
#hits_data ⇒ Object
136
137
138
|
# File 'lib/artirix_data_models/es_collection.rb', line 136
def hits_data
results.map(&:data_hash)
end
|
#max_score ⇒ Object
The maximum score for a query
98
99
100
|
# File 'lib/artirix_data_models/es_collection.rb', line 98
def max_score
@max_score ||= hits[:max_score]
end
|
#raw_aggregations_data ⇒ Object
155
156
157
|
# File 'lib/artirix_data_models/es_collection.rb', line 155
def raw_aggregations_data
response[:aggregations]
end
|
#results ⇒ Object
117
118
119
|
# File 'lib/artirix_data_models/es_collection.rb', line 117
def results
@results ||= load_results
end
|
#total ⇒ Object
The number of total hits for a query
92
93
94
|
# File 'lib/artirix_data_models/es_collection.rb', line 92
def total
@total ||= hits[:total]
end
|