Module: Escargot::AdminIndexVersions

Defined in:
lib/escargot/elasticsearch_ex.rb

Instance Method Summary collapse

Instance Method Details

#create_index_version(index, create_options) ⇒ Object

creates an index to store a new index version. Returns its name



6
7
8
9
10
# File 'lib/escargot/elasticsearch_ex.rb', line 6

def create_index_version(index, create_options)
  index_with_timestamp = "#{index}_#{Time.now.to_f}"
  $elastic_search_client.create_index(index_with_timestamp, create_options)
  return index_with_timestamp
end

#current_index_version(index) ⇒ Object

returns the full index name of the current version for this index



13
14
15
# File 'lib/escargot/elasticsearch_ex.rb', line 13

def current_index_version(index)
  $elastic_search_client.index_status(index)["indices"].keys.first rescue nil
end

#deploy_index_version(index, new_version) ⇒ Object

“deploys” a new version as the current one



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/escargot/elasticsearch_ex.rb', line 18

def deploy_index_version(index, new_version)
  $elastic_search_client.refresh(new_version)
  if current_version = current_index_version(index)
    $elastic_search_client.alias_index(
      :add => {new_version => index}, 
      :remove => {current_version => index}
    )
  else
    $elastic_search_client.alias_index(:add => {new_version => index})
  end
end

#index_versions(index) ⇒ Object

lists all current, old, an in-progress versions for this index



41
42
43
# File 'lib/escargot/elasticsearch_ex.rb', line 41

def index_versions(index)
  $elastic_search_client.index_status()["indices"].keys.grep(/^#{index}_/)
end

#prune_index_versions(index) ⇒ Object

deletes all index versions older than the current one



31
32
33
34
35
36
37
38
# File 'lib/escargot/elasticsearch_ex.rb', line 31

def prune_index_versions(index)
  current_version = current_index_version(index)
  return unless current_version
  old_versions = index_versions(index).select{|version| version_timestamp(version) < version_timestamp(current_version)}
  old_versions.each do |version|
    $elastic_search_client.delete_index(version)
  end
end