Module: Mindex::Index::ClassMethods

Defined in:
lib/mindex/index.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#connection_settings(options = {}) ⇒ Object



9
10
11
# File 'lib/mindex/index.rb', line 9

def connection_settings(options = {})
  @connection_settings = options
end

#doc_typeObject



40
41
42
# File 'lib/mindex/index.rb', line 40

def doc_type
  @index_label || name.demodulize.tableize
end

#drop_items(ids) ⇒ Object



60
61
62
63
# File 'lib/mindex/index.rb', line 60

def drop_items(ids)
  bulk_data = [ids].flatten.map { |id| { delete: { _index: index_alias, _type: doc_type, _id: id } } }
  elasticsearch.indices.client.bulk(body: bulk_data) if bulk_data.any?
end

#elasticsearchObject Also known as: es



92
93
94
# File 'lib/mindex/index.rb', line 92

def elasticsearch
  Elasticsearch.connect(@connection_settings)
end

#index_aliasObject



30
31
32
# File 'lib/mindex/index.rb', line 30

def index_alias
  [@index_prefix, (@index_label || name.demodulize.tableize)].compact.join('_').underscore
end

#index_config(settings:, mappings:) ⇒ Object



13
14
15
16
# File 'lib/mindex/index.rb', line 13

def index_config(settings:, mappings:)
  @index_settings = settings
  @index_mappings = mappings
end

#index_create(move_or_create_index_alias: true) ⇒ Object



53
54
55
56
57
58
# File 'lib/mindex/index.rb', line 53

def index_create(move_or_create_index_alias: true)
  index_name = new_index_name
  elasticsearch.indices.create(index: index_name, body: { settings: @index_settings || {}, mappings: @index_mappings || {} })
  add_index_alias(target: index_name) if move_or_create_index_alias
  index_name
end

#index_exist?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/mindex/index.rb', line 48

def index_exist?(name = nil)
  return false if name.nil? && index_name.nil?
  elasticsearch.indices.exists(index: name || index_name)
end

#index_label(label) ⇒ Object



22
23
24
# File 'lib/mindex/index.rb', line 22

def index_label(label)
  @index_label = label
end

#index_nameObject



34
35
36
37
38
# File 'lib/mindex/index.rb', line 34

def index_name
  elasticsearch.indices.get_alias(name: index_alias).keys.first
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
  nil
end

#index_num_threads(count) ⇒ Object



26
27
28
# File 'lib/mindex/index.rb', line 26

def index_num_threads(count)
  @index_num_threads = count
end

#index_prefix(prefix) ⇒ Object



18
19
20
# File 'lib/mindex/index.rb', line 18

def index_prefix(prefix)
  @index_prefix = prefix
end

#index_refreshObject



44
45
46
# File 'lib/mindex/index.rb', line 44

def index_refresh
  elasticsearch.indices.refresh(index: index_name)
end

#recreate_index(options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mindex/index.rb', line 80

def recreate_index(options = {})
  started_at = Time.now
  index_name = index_create(move_or_create_index_alias: false)
  index_queue(index: index_name) do |queue|
    scroll(options) do |items|
      queue << items
    end
  end
  add_index_alias(target: index_name)
  reindex(options.merge(started_at: started_at))
end

#reindex(options = {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/mindex/index.rb', line 71

def reindex(options = {})
  index_create unless index_exist?
  index_queue do |queue|
    scroll(options) do |items|
      queue << items
    end
  end
end

#reindex_items(ids) ⇒ Object



65
66
67
68
69
# File 'lib/mindex/index.rb', line 65

def reindex_items(ids)
  index_queue(threads: 1) do |queue|
    queue << fetch(ids)
  end
end