|
| 1 | +module ElasticSearchFramework |
| 2 | + module IndexAlias |
| 3 | + def index(klass, active:) |
| 4 | + unless instance_variable_defined?(:@elastic_search_indexes) |
| 5 | + instance_variable_set(:@elastic_search_indexes, []) |
| 6 | + end |
| 7 | + indexes = self.instance_variable_get(:@elastic_search_indexes) |
| 8 | + indexes << {klass: klass, active: active} |
| 9 | + instance_variable_set(:@elastic_search_indexes, indexes) |
| 10 | + end |
| 11 | + |
| 12 | + def indexes |
| 13 | + self.instance_variable_get(:@elastic_search_indexes) |
| 14 | + end |
| 15 | + |
| 16 | + def name(name) |
| 17 | + unless instance_variable_defined?(:@elastic_search_index_alias_name) |
| 18 | + instance_variable_set(:@elastic_search_index_alias_name, "#{name}") |
| 19 | + else |
| 20 | + raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Duplicate index alias name: #{name}.") |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + def valid? |
| 25 | + indexes.select { |i| i[:active] == true }.length == 1 |
| 26 | + end |
| 27 | + |
| 28 | + def create |
| 29 | + if !valid? |
| 30 | + raise ElasticSearchFramework::Exceptions::IndexError.new("[#{self.class}] - Invalid Index alias.") |
| 31 | + end |
| 32 | + |
| 33 | + uri = URI("#{host}/_aliases") |
| 34 | + |
| 35 | + payload = { |
| 36 | + actions: [], |
| 37 | + } |
| 38 | + |
| 39 | + indexes.each do |index| |
| 40 | + action = nil |
| 41 | + if exists?(index: index[:klass]) |
| 42 | + action = "remove" if !index[:active] |
| 43 | + else |
| 44 | + action = "add" if index[:active] |
| 45 | + end |
| 46 | + next if action.nil? |
| 47 | + |
| 48 | + payload[:actions] << {action => {index: index[:klass].full_name, alias: self.full_name}} |
| 49 | + end |
| 50 | + |
| 51 | + request = Net::HTTP::Post.new(uri.request_uri) |
| 52 | + request.body = JSON.dump(payload) |
| 53 | + request.content_type = "application/json" |
| 54 | + |
| 55 | + response = repository.with_client do |client| |
| 56 | + client.request(request) |
| 57 | + end |
| 58 | + |
| 59 | + is_valid_response?(response.code) || Integer(response.code) == 404 |
| 60 | + end |
| 61 | + |
| 62 | + def delete |
| 63 | + uri = URI("#{host}/_all/_aliases/#{full_name}") |
| 64 | + |
| 65 | + request = Net::HTTP::Delete.new(uri.request_uri) |
| 66 | + |
| 67 | + response = repository.with_client do |client| |
| 68 | + client.request(request) |
| 69 | + end |
| 70 | + |
| 71 | + is_valid_response?(response.code) || Integer(response.code) == 404 |
| 72 | + end |
| 73 | + |
| 74 | + def exists?(index:) |
| 75 | + uri = URI("#{host}/#{index.full_name}/_alias/#{full_name}") |
| 76 | + |
| 77 | + request = Net::HTTP::Get.new(uri.request_uri) |
| 78 | + |
| 79 | + response = repository.with_client do |client| |
| 80 | + client.request(request) |
| 81 | + end |
| 82 | + |
| 83 | + return false if response.code == "404" |
| 84 | + |
| 85 | + result = nil |
| 86 | + if is_valid_response?(response.code) |
| 87 | + result = JSON.parse(response.body) |
| 88 | + end |
| 89 | + |
| 90 | + return true if !result.nil? && result[index.full_name]["aliases"] != nil |
| 91 | + return false |
| 92 | + end |
| 93 | + |
| 94 | + def is_valid_response?(code) |
| 95 | + [200, 201, 202].include?(Integer(code)) |
| 96 | + end |
| 97 | + |
| 98 | + def full_name |
| 99 | + name = instance_variable_get(:@elastic_search_index_alias_name) |
| 100 | + if ElasticSearchFramework.namespace != nil |
| 101 | + "#{ElasticSearchFramework.namespace}#{ElasticSearchFramework.namespace_delimiter}#{name.downcase}" |
| 102 | + else |
| 103 | + name.downcase |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def description |
| 108 | + index = indexes.last[:klass] |
| 109 | + hash = index.instance_variable_get(:@elastic_search_index_def) |
| 110 | + if index.instance_variable_defined?(:@elastic_search_index_id) |
| 111 | + hash[:id] = index.instance_variable_get(:@elastic_search_index_id) |
| 112 | + else |
| 113 | + hash[:id] = :id |
| 114 | + end |
| 115 | + hash |
| 116 | + end |
| 117 | + |
| 118 | + def host |
| 119 | + "#{ElasticSearchFramework.host}:#{ElasticSearchFramework.port}" |
| 120 | + end |
| 121 | + |
| 122 | + def repository |
| 123 | + @repository ||= ElasticSearchFramework::Repository.new |
| 124 | + end |
| 125 | + |
| 126 | + def get_item(id:, type: "default") |
| 127 | + repository.get(index: self, id: id, type: type) |
| 128 | + end |
| 129 | + |
| 130 | + def put_item(type: "default", item:) |
| 131 | + indexes.each do |index| |
| 132 | + repository.set(entity: item, index: index[:klass], type: type) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def delete_item(id:, type: "default") |
| 137 | + indexes.each do |index| |
| 138 | + repository.drop(index: index[:klass], id: id, type: type) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + def query |
| 143 | + ElasticSearchFramework::Query.new(index: self) |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments