Class: Elasticated::MonthlyPartitionedRepository

Inherits:
Repository
  • Object
show all
Includes:
Configurable
Defined in:
lib/elasticated/repositories/monthly_partitioned_repository.rb

Instance Attribute Summary collapse

Attributes inherited from Repository

#client

Instance Method Summary collapse

Methods included from Configurable

#log

Methods inherited from Repository

#delete_by, #execute_aggregated_search, #execute_aggregations, #execute_count, #execute_search, #exists?, #index_document, #update_document

Constructor Details

#initialize(opts = {}) ⇒ MonthlyPartitionedRepository

Returns a new instance of MonthlyPartitionedRepository.



9
10
11
12
13
14
15
16
17
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 9

def initialize(opts={})
  super opts
  self._index_name = opts[:index_name] || raise('An index prefix should be specified')
  self._index_alias = opts[:index_alias] || raise('An index alias prefix should be specified')
  self._general_alias = opts[:general_alias] || _index_alias
  self.date_field = opts[:date_field] || :date
  self.index_options = opts[:index_options] # hash with mapping, shards info, etc or nil
  self.dynamic_creation = opts[:dynamic_creation] || false
end

Instance Attribute Details

#_general_aliasObject

Returns the value of attribute _general_alias.



5
6
7
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 5

def _general_alias
  @_general_alias
end

#_index_aliasObject

Returns the value of attribute _index_alias.



5
6
7
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 5

def _index_alias
  @_index_alias
end

#_index_nameObject

Returns the value of attribute _index_name.



5
6
7
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 5

def _index_name
  @_index_name
end

#date_fieldObject

Returns the value of attribute date_field.



6
7
8
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 6

def date_field
  @date_field
end

#dynamic_creationObject

Returns the value of attribute dynamic_creation.



7
8
9
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 7

def dynamic_creation
  @dynamic_creation
end

#index_optionsObject

Returns the value of attribute index_options.



7
8
9
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 7

def index_options
  @index_options
end

Instance Method Details

#check_or_create_index!(date) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 38

def check_or_create_index! date
  index_alias = index_alias_for date
  return if client.index_exists? index_alias
  raise("Index '#{index_alias}' not found (dynamic per-month index creation is disabled)") unless dynamic_creation
  index_name = index_name_for date
  create_index index_name
  create_alias index_name, index_alias
end

#create_alias!(date) ⇒ Object



51
52
53
54
55
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 51

def create_alias! date
  index_name = index_name_for date
  index_alias = index_alias_for date
  create_alias index_name, index_alias
end

#create_index!(date) ⇒ Object



47
48
49
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 47

def create_index! date
  create_index index_name_for date
end

#date_from(document) ⇒ Object



33
34
35
36
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 33

def date_from(document)
  str = document.source[date_field.to_s]
  str && Date.parse(str)
end

#execute(action, query, opts = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 19

def execute(action, query, opts={})
  index_alias = index_alias_for query
  super action, query, opts.merge(index: index_alias)
# rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
#   action == :count ? 0 : nil
end

#index_alias_for(object) ⇒ Object



61
62
63
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 61

def index_alias_for(object)
  object.is_a?(Query) ? index_alias_for_query(object) : index_alias_for_date(object)
end

#index_name_for(object) ⇒ Object



57
58
59
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 57

def index_name_for(object)
  object.is_a?(Query) ? index_alias_for_query(object) : index_name_for_date(object)
end

#prepare(action, document, opts = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/elasticated/repositories/monthly_partitioned_repository.rb', line 26

def prepare(action, document, opts={})
  date = date_from document
  raise("The document has not a valid '#{date_field}' field") unless date
  check_or_create_index! date
  super action, document, opts.merge(index: index_alias_for(date))
end