-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias.rb
56 lines (39 loc) · 1.08 KB
/
alias.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'tire'
require 'debugger'
Tire.configure { logger STDERR }
def index_elasticsearch(class_name)
Tire.index([class_name, "_", Time.now.strftime('%Y%m%d%H%M%S')].join) do
store first: 'Kimberly', last: "miranda", middle: "middle"
refresh
end
end
class_name = "users"
# Find Alias
index_alias = Tire::Alias.find(class_name)
if index_alias
# Alias exist, find the index
old_index = index_alias.indices
to_be_delete = old_index.first
# Create a new index
new_index = index_elasticsearch(class_name)
# Assign our alias to the new index
a = Tire::Alias.new
a.name(class_name)
a.index(new_index.name)
a.save
# Remove our alias from the old index
old_index.each do |index|
index_alias.indices.delete index
end
index_alias.save
# Delete old index
index_to_be_deleted = Tire::Index.new(to_be_delete)
index_to_be_deleted.delete if index_to_be_deleted.exists?
else
# Alias not found, so let's create an index and an alias
index = index_elasticsearch(class_name)
a = Tire::Alias.new
a.name(class_name)
a.index(index.name)
a.save
end