Module: ElasticRecord::Index::Manage

Included in:
ElasticRecord::Index
Defined in:
lib/elastic_record/index/manage.rb

Instance Method Summary collapse

Instance Method Details

#aliased_namesObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/elastic_record/index/manage.rb', line 66

def aliased_names
  if (index_names = all_names).any?
    json = connection.json_get "/#{index_names.join(',')}/_alias"
    json.keys.select do |index_name|
      json[index_name]['aliases'].keys.include?(alias_name)
    end
  else
    []
  end
end

#all_namesObject



77
78
79
80
81
82
# File 'lib/elastic_record/index/manage.rb', line 77

def all_names
  connection.json_get("/#{alias_name}/_mapping").keys
rescue
  # TODO: In ES 1.4, this returns empty rather than a 404
  []
end

#create(index_name = new_index_name, setting_overrides: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/elastic_record/index/manage.rb', line 10

def create(index_name = new_index_name, setting_overrides: {})
  mapping_params = {
    "mappings" => (custom_mapping_type_name? ? { mapping_type => mapping } : mapping),
    "settings" => settings.merge(setting_overrides)
  }

  # TODO: Remove include_type_name when ES8 support is added
  connection.json_put "/#{index_name}?include_type_name=#{custom_mapping_type_name?}", mapping_params
  index_name
end

#create_and_deploy(index_name = new_index_name) ⇒ Object



4
5
6
7
8
# File 'lib/elastic_record/index/manage.rb', line 4

def create_and_deploy(index_name = new_index_name)
  create(index_name)
  deploy(index_name)
  index_name
end

#delete(index_name) ⇒ Object



21
22
23
# File 'lib/elastic_record/index/manage.rb', line 21

def delete(index_name)
  connection.json_delete "/#{index_name}"
end

#delete_allObject



25
26
27
28
29
# File 'lib/elastic_record/index/manage.rb', line 25

def delete_all
  all_names.each do |index_name|
    delete index_name
  end
end

#deploy(index_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/elastic_record/index/manage.rb', line 35

def deploy(index_name)
  actions = [
    {
      add: {
        "index" => index_name,
        "alias" => alias_name
      }
    }
  ]

  (aliased_names - [index_name]).each do |index_to_remove|
    actions << {
      remove: {
        "index" => index_to_remove,
        "alias" => alias_name
      }
    }
  end

  connection.json_post '/_aliases', actions: actions
end

#exists?(index_name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/elastic_record/index/manage.rb', line 31

def exists?(index_name)
  connection.head("/#{index_name}") == '200'
end

#refresh(index_name = alias_name) ⇒ Object



57
58
59
# File 'lib/elastic_record/index/manage.rb', line 57

def refresh(index_name = alias_name)
  connection.json_post "/#{index_name}/_refresh"
end

#resetObject



61
62
63
64
# File 'lib/elastic_record/index/manage.rb', line 61

def reset
  delete_all
  create_and_deploy
end