Class: ArtirixDataModels::EsCollection

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

Constants included from WithDAORegistry

WithDAORegistry::DEFAULT_DAO_REGISTRY_LOADER

Instance Attribute Summary collapse

Attributes included from WithDAORegistry

#dao_registry_loader

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithDAORegistry

#dao_registry, #dao_registry=, #default_dao_registry, loader_or_registry_or_default, #set_dao_registry, #set_dao_registry_and_loader, #set_dao_registry_loader, #set_default_dao_registry_loader

Constructor Details

#initialize(klass_or_factory, response:, from: 0, size: DEFAULT_SIZE, dao_registry: nil, dao_registry_loader: nil, aggregations_factory: nil) ⇒ EsCollection

Returns a new instance of EsCollection.

Parameters:

  • klass_or_factory (A Model Class|Callable)

    The model class or the Factory (callable object) to build the model

  • response (Hash)

    The full response returned from the DataLayer

  • from (Int) (defaults to: 0)

    requested offset (0 by default)

  • size (Int) (defaults to: DEFAULT_SIZE)

    requested amount of hits (10 by default)

  • aggregations_factory (Int) (defaults to: nil)

    requested amount of hits (10 by default)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/artirix_data_models/es_collection.rb', line 79

def initialize(klass_or_factory,
               response:,
               from: 0,
               size: DEFAULT_SIZE,
               dao_registry: nil,
               dao_registry_loader: nil,
               aggregations_factory: nil)

  set_dao_registry_and_loader dao_registry_loader, dao_registry

  @klass_or_factory     = klass_or_factory
  @response             = response
  @from                 = from
  @size                 = size
  @aggregations_factory = aggregations_factory
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



71
72
73
# File 'lib/artirix_data_models/es_collection.rb', line 71

def from
  @from
end

#klass_or_factoryObject (readonly)

Returns the value of attribute klass_or_factory.



71
72
73
# File 'lib/artirix_data_models/es_collection.rb', line 71

def klass_or_factory
  @klass_or_factory
end

#responseObject (readonly)

Returns the value of attribute response.



71
72
73
# File 'lib/artirix_data_models/es_collection.rb', line 71

def response
  @response
end

#sizeObject (readonly)

Returns the value of attribute size.



71
72
73
# File 'lib/artirix_data_models/es_collection.rb', line 71

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_kaminariObject



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_paginateObject



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



122
123
124
125
# File 'lib/artirix_data_models/es_collection.rb', line 122

def aggregation(name)
  n = name.to_sym
  aggregations.detect { |x| x.name == n }
end

#aggregationsObject



118
119
120
# File 'lib/artirix_data_models/es_collection.rb', line 118

def aggregations
  @aggregations ||= build_aggregations
end

#aggregations_factoryObject



96
97
98
# File 'lib/artirix_data_models/es_collection.rb', line 96

def aggregations_factory
  @aggregations_factory || dao_registry.get(:aggregations_factory)
end

#cache_keyObject



156
157
158
159
160
161
162
163
# File 'lib/artirix_data_models/es_collection.rb', line 156

def cache_key
  [
    total,
    size,
    from,
    results.map { |x| x.try(:cache_key) || x.to_s }.join(CACHE_KEY_RESULT_SEPARATOR)
  ].join(CACHE_KEY_SECTION_SEPARATOR)
end

#current_pageObject

Return the current page



152
153
154
# File 'lib/artirix_data_models/es_collection.rb', line 152

def current_page
  from / size + 1 if from && size
end

#data_hash(&block) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/artirix_data_models/es_collection.rb', line 133

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

#hitsObject

The raw hits



114
115
116
# File 'lib/artirix_data_models/es_collection.rb', line 114

def hits
  @hits ||= response[:hits]
end

#hits_dataObject



146
147
148
# File 'lib/artirix_data_models/es_collection.rb', line 146

def hits_data
  results.map(&:data_hash)
end

#max_scoreObject

The maximum score for a query



108
109
110
# File 'lib/artirix_data_models/es_collection.rb', line 108

def max_score
  @max_score ||= hits[:max_score]
end

#raw_aggregations_dataObject



165
166
167
# File 'lib/artirix_data_models/es_collection.rb', line 165

def raw_aggregations_data
  response[:aggregations]
end

#resultsObject



127
128
129
# File 'lib/artirix_data_models/es_collection.rb', line 127

def results
  @results ||= load_results
end

#totalObject

The number of total hits for a query



102
103
104
# File 'lib/artirix_data_models/es_collection.rb', line 102

def total
  @total ||= hits[:total]
end