Last active
August 29, 2015 14:16
-
-
Save revis0r/8702f5e690d2441b8e05 to your computer and use it in GitHub Desktop.
elasticsearch migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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