Skip to content

Instantly share code, notes, and snippets.

@revis0r
Last active August 29, 2015 14:16
Show Gist options
  • Save revis0r/8702f5e690d2441b8e05 to your computer and use it in GitHub Desktop.
Save revis0r/8702f5e690d2441b8e05 to your computer and use it in GitHub Desktop.
elasticsearch migration
#!/usr/bin/env ruby
require 'yaml'
require 'cql'
require 'base32'
require 'simple_uuid'
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
# http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/
keep_old_index = false # if you want to save old index
old_version = 4 # need if keep_old_index = true
new_version = 2
Tire.index "#{Settings::ElasticSearch.index}_v#{new_version}" do
delete
create :mappings => {
:Log => {
:properties => {
:id => { type: 'string', index: "not_analyzed", norms: { enabled: false }, include_in_all: false },
:object_id => { type: 'string', index: "not_analyzed", norms: { enabled: false }, include_in_all: false },
:company => { type: 'string', index: "not_analyzed", norms: { enabled: false }, include_in_all: false },
:domain => { type: 'string', index: "not_analyzed", norms: { enabled: false }, include_in_all: false },
:message => { type: 'string', analyzer: 'snowball' },
:controller => { type: 'string', index: "not_analyzed", norms: { enabled: false }, include_in_all: false },
:new_attributes_values => { type: 'object', index: "not_analyzed", norms: { enabled: false }, include_in_all: false },
:old_attributes_values => { type: 'object', index: "not_analyzed", norms: {enabled: false}, include_in_all: false },
:timestamp => { type: 'date', format: "date_time", index: "not_analyzed", norms:{ enabled: false }, include_in_all: false }
}
}
}
end
Tire.index Settings::ElasticSearch.index do
reindex "#{Settings::ElasticSearch.index}_v#{new_version}"
delete unless keep_old_index
end
if keep_old_index
a = Tire::Alias.find(Settings::ElasticSearch.index)
a.indices.delete "#{Settings::ElasticSearch.index}_v#{old_version}"
a.indices.add "#{Settings::ElasticSearch.index}_v#{new_version}"
a.save
else
Tire::Alias.create name: Settings::ElasticSearch.index, indices: ["#{Settings::ElasticSearch.index}_v#{new_version}"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment