Module: Elasticsearch::Model::Extensions::IndexOperations::ClassMethods

Defined in:
lib/elasticsearch/model/extensions/index_operations.rb

Instance Method Summary collapse

Instance Method Details

#create_index(name:, force: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/elasticsearch/model/extensions/index_operations.rb', line 10

def create_index(name:, force:true)
  klass = self

  client = __elasticsearch__.client

  indices = client.indices

  if indices.exists(index: name) && force
    indices.delete index: name
  end

  indices.create index: name, body: { settings: klass.settings.to_hash, mappings: klass.mappings.to_hash }
end

#delete_alias(name: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/elasticsearch/model/extensions/index_operations.rb', line 32

def delete_alias(name: nil)
  name ||= index_name

  client = __elasticsearch__.client

  indices = client.indices

  indices_aliased = indices.get_alias(name: name).keys

  indices_aliased.each do |index|
    indices.delete name: name, index: index
  end
end

#delete_index(name:) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/elasticsearch/model/extensions/index_operations.rb', line 24

def delete_index(name:)
  client = __elasticsearch__.client

  indices = client.indices

  indices.delete index: name
end

#prepare_alias(name:, force: true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/elasticsearch/model/extensions/index_operations.rb', line 46

def prepare_alias(name:, force: true)
  client = __elasticsearch__.client

  indices = client.indices

  if indices.exists(index: name) && force
    indices.delete index: name
  end

  unless indices.exists_alias(name: name)
    aliased_index_name = "#{index_name}_#{Time.now.to_i}"

    create_index(name: aliased_index_name, force: force)

    indices.put_alias index: aliased_index_name, name: name
  end
end

#replace_index_for_alias(name:, to:) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/elasticsearch/model/extensions/index_operations.rb', line 64

def replace_index_for_alias(name:, to:)
  client = __elasticsearch__.client

  indices = client.indices

  if indices.exists_alias name: name
    old_index_name = indices.get_alias(name: name).keys.first

    indices.update_aliases body: {
      actions: [
        { remove: { index: old_index_name, alias: name } },
        { add: { index: to, alias: name } }
      ]
    }
  else
    indices.put_alias index: to, name: name
  end
end