Module: ElasticSearchFramework::IndexAlias

Defined in:
lib/elastic_search_framework/index_alias.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
57
58
59
60
# File 'lib/elastic_search_framework/index_alias.rb', line 28

def create
  if !valid?
    raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Invalid Index alias.")
  end

  uri = URI("#{host}/_aliases")

  payload = {
    actions: [],
  }

  indexes.each do |index|
    action = nil
    if exists?(index: index[:klass])
      action = "remove" if !index[:active]
    else
      action = "add" if index[:active]
    end
    next if action.nil?

    payload[:actions] << {action => {index: index[:klass].full_name, alias: self.full_name}}
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = JSON.dump(payload)
  request.content_type = "application/json"

  response = repository.with_client do |client|
    client.request(request)
  end

  is_valid_response?(response.code) || Integer(response.code) == 404
end

#deleteObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/elastic_search_framework/index_alias.rb', line 62

def delete
  uri = URI("#{host}/_all/_aliases/#{full_name}")

  request = Net::HTTP::Delete.new(uri.request_uri)

  response = repository.with_client do |client|
    client.request(request)
  end

  is_valid_response?(response.code) || Integer(response.code) == 404
end

#delete_item(id:, type: "default", routing_key: nil) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/elastic_search_framework/index_alias.rb', line 144

def delete_item(id:, type: "default", routing_key: nil)
  indexes.each do |index|
    options = { index: index[:klass], id: id, type: type }
    options[:routing_key] = routing_key if index[:klass].routing_enabled? && routing_key

    repository.drop(**options)
  end
end

#descriptionObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/elastic_search_framework/index_alias.rb', line 107

def description
  index = indexes.last[:klass]
  hash = index.instance_variable_get(:@elastic_search_index_def)
  if index.instance_variable_defined?(:@elastic_search_index_id)
    hash[:id] = index.instance_variable_get(:@elastic_search_index_id)
  else
    hash[:id] = :id
  end
  hash
end

#exists?(index:) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/elastic_search_framework/index_alias.rb', line 74

def exists?(index:)
  uri = URI("#{host}/#{index.full_name}/_alias/#{full_name}")

  request = Net::HTTP::Get.new(uri.request_uri)

  response = repository.with_client do |client|
    client.request(request)
  end

  return false if response.code == "404"

  result = nil
  if is_valid_response?(response.code)
    result = JSON.parse(response.body)
  end

  return true if !result.nil? && result[index.full_name]["aliases"] != nil
  return false
end

#full_nameObject



98
99
100
101
102
103
104
105
# File 'lib/elastic_search_framework/index_alias.rb', line 98

def full_name
  name = instance_variable_get(:@elastic_search_index_alias_name)
  if ElasticSearchFramework.namespace != nil
    "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{name.downcase}"
  else
    name.downcase
  end
end

#get_item(id:, type: "default", routing_key: nil) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/elastic_search_framework/index_alias.rb', line 126

def get_item(id:, type: "default", routing_key: nil)
  active_index = indexes.detect { |i| i[:active] == true }[:klass]

  options = { index: self, id: id, type: type }
  options[:routing_key] = routing_key if active_index.routing_enabled? && routing_key

  repository.get(**options)
end

#hostObject



118
119
120
# File 'lib/elastic_search_framework/index_alias.rb', line 118

def host
  "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}"
end

#index(klass, active:) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/elastic_search_framework/index_alias.rb', line 3

def index(klass, active:)
  unless instance_variable_defined?(:@elastic_search_indexes)
    instance_variable_set(:@elastic_search_indexes, [])
  end
  indexes = self.instance_variable_get(:@elastic_search_indexes)
  indexes << {klass: klass, active: active}
  instance_variable_set(:@elastic_search_indexes, indexes)
end

#indexesObject



12
13
14
# File 'lib/elastic_search_framework/index_alias.rb', line 12

def indexes
  self.instance_variable_get(:@elastic_search_indexes)
end

#is_valid_response?(code) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/elastic_search_framework/index_alias.rb', line 94

def is_valid_response?(code)
  [200, 201, 202].include?(Integer(code))
end

#name(name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/elastic_search_framework/index_alias.rb', line 16

def name(name)
  unless instance_variable_defined?(:@elastic_search_index_alias_name)
    instance_variable_set(:@elastic_search_index_alias_name, "#{name}")
  else
    raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index alias name: #{name}.")
  end
end

#put_item(type: "default", item:, op_type: 'index', routing_key: nil) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/elastic_search_framework/index_alias.rb', line 135

def put_item(type: "default", item:, op_type: 'index', routing_key: nil)
  indexes.each do |index|
    options = { entity: item, index: index[:klass], type: type, op_type: op_type }
    options[:routing_key] = routing_key if index[:klass].routing_enabled? && routing_key

    repository.set(**options)
  end
end

#queryObject



153
154
155
# File 'lib/elastic_search_framework/index_alias.rb', line 153

def query
  ElasticSearchFramework::Query.new(index: self)
end

#repositoryObject



122
123
124
# File 'lib/elastic_search_framework/index_alias.rb', line 122

def repository
  @repository ||= ElasticSearchFramework::Repository.new
end

#routing_enabled?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/elastic_search_framework/index_alias.rb', line 157

def routing_enabled?
  indexes.find(active: true).first[:klass].routing_enabled?
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/elastic_search_framework/index_alias.rb', line 24

def valid?
  indexes.select { |i| i[:active] == true }.length == 1
end