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") ⇒ Object



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

def delete_item(id:, type: "default")
  indexes.each do |index|
    repository.drop(index: index[:klass], id: id, type: type)
  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") ⇒ Object



126
127
128
# File 'lib/elastic_search_framework/index_alias.rb', line 126

def get_item(id:, type: "default")
  repository.get(index: self, id: id, type: type)
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:) ⇒ Object



130
131
132
133
134
# File 'lib/elastic_search_framework/index_alias.rb', line 130

def put_item(type: "default", item:)
  indexes.each do |index|
    repository.set(entity: item, index: index[:klass], type: type)
  end
end

#queryObject



142
143
144
# File 'lib/elastic_search_framework/index_alias.rb', line 142

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

#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