class Tire::Alias

Represents an alias in Elasticsearch. An alias may point to one or multiple indices, for instance to separate physical indices into logical entities, where each user has a "virtual index" or for setting up "sliding window" scenarios.

See: www.elasticsearch.org/guide/reference/api/admin-indices-aliases.html

Public Class Methods

all(index=nil) click to toggle source

Returns a collection of Tire::Alias objects for all aliases defined in the cluster, or for a specific index.

# File lib/tire/alias.rb, line 92
def self.all(index=nil)
  @response = Configuration.client.get [Configuration.url, index, '_aliases'].compact.join('/')

  aliases = MultiJson.decode(@response.body).inject({}) do |result, (index, value)|
    # 1] Skip indices without aliases
    next result if value['aliases'].empty?

    # 2] Build a reverse map of hashes (alias => indices, config)
    value['aliases'].each do |key, value| (result[key] ||= { 'indices' => [] }).update(value)['indices'].push(index) end
    result
  end

  # 3] Build a collection of Alias objects from hashes
  aliases.map do |key, value|
    self.new(value.update('name' => key))
  end

ensure
  # FIXME: Extract the `logged` method
  Alias.new.logged '_aliases', %Qcurl "#{Configuration.url}/_aliases"|
end
create(attributes={}, &block) click to toggle source

Create new alias

# File lib/tire/alias.rb, line 124
def self.create(attributes={}, &block)
  new(attributes, &block).save
end
find(name, &block) click to toggle source

Returns an alias by name

# File lib/tire/alias.rb, line 116
def self.find(name, &block)
  a = all.select { |a| a.name == name }.first
  block.call(a) if block_given?
  return a
end
new(attributes={}, &block) click to toggle source

Create an alias pointing to multiple indices:

Tire::Alias.create name: 'my_alias', indices: ['index_1', 'index_2']

Pass the routing and/or filtering configuration in the options Hash:

a = Tire::Alias.new name:    'index_anne',
                indices: ['index_2012_04', 'index_2012_03', 'index_2012_02'],
                routing: 1,
                filter:  { :terms => { :user => 'anne' } }
a.save

You may configure the alias in an imperative manner:

a = Tire::Alias.new
a.name('index_anne')
a.index('index_2012_04')
a.index('index_2012_03')
# ...
a.save

or more declaratively, with a block:

Tire::Alias.new name: 'my_alias' do
  index 'index_A'
  index 'index_B'
  filter :terms, username: 'mary'
end

To update an existing alias, find it by name, configure it and save it:

a = Tire::Alias.find('my_alias')
a.indices.delete 'index_A'
a.indices.add    'index_B'
a.indices.add    'index_C'
a.save

Or do it with a block:

Tire::Alias.find('articles_aliased') do |a|
  a.indices.remove 'articles_2'
  puts '---', "#{a.name} says: /me as JSON >", a.as_json, '---'
  a.save
end

To remove indices from alias, you may want to use the `delete_all` method:

require 'active_support/core_ext/numeric'
require 'active_support/core_ext/date/calculations'

a = Tire::Alias.find('articles_aliased')
a.indices.delete_if do |i|
  Time.parse( i.gsub(%rarticles_/, '') ) < 4.weeks.ago rescue false
end
a.save

To get all aliases, use the `::all` method:

 Tire::Alias.all.each do |a|
  puts "#{a.name.rjust(30)} points to: #{a.indices}"
end
# File lib/tire/alias.rb, line 74
def initialize(attributes={}, &block)
  raise ArgumentError, "Please pass a Hash-like object" unless attributes.respond_to?(:each_pair)

  @attributes = { :indices => IndexCollection.new([]) }

  attributes.each_pair do |key, value|
    if ['index','indices'].include? key.to_s
      @attributes[:indices] = IndexCollection.new(value)
    else
      @attributes[key.to_sym] = value
    end
  end

  block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end

Public Instance Methods

as_json(options=nil) click to toggle source

Return a Hash suitable for JSON serialization

# File lib/tire/alias.rb, line 170
def as_json(options=nil)
  actions = []
  indices.add_indices.each do |index|
    operation = { :index => index, :alias => name }
    operation.update( { :routing => routing } ) if respond_to?(:routing) and routing
    operation.update( { :filter  => filter } )  if respond_to?(:filter)  and filter
    actions.push( { :add => operation } )
  end

  indices.remove_indices.each do |index|
    operation = { :index => index, :alias => name }
    actions.push( { :remove => operation } )
  end

  { :actions => actions }
end
filter(type=nil, *options) click to toggle source

Get or set the alias routing

# File lib/tire/alias.rb, line 155
def filter(type=nil, *options)
  type ? (@attributes[:filter] = Search::Filter.new(type, *options).to_hash and return self ) : @attributes[:filter]
end
index(*names) click to toggle source
Alias for: indices
indices(*names) click to toggle source

Get or set the alias indices

# File lib/tire/alias.rb, line 142
def indices(*names)
  names = Array(names).flatten
  names.compact.empty? ? @attributes[:indices] : (names.each { |n| @attributes[:indices].push(n) } and return self)
end
Also aliased as: index
inspect() click to toggle source
# File lib/tire/alias.rb, line 193
def inspect
  %Q<#{self.class} #{@attributes.inspect}>|
end
logged(endpoint='/', curl='') click to toggle source
# File lib/tire/alias.rb, line 201
def logged(endpoint='/', curl='')
  # FIXME: Extract the `logged` method into module and mix it into classes
  if Configuration.logger
    error = $!

    Configuration.logger.log_request endpoint, @name, curl

    code = @response ? @response.code : error.class rescue 200

    if Configuration.logger.level.to_s == 'debug'
      body = if @response
        MultiJson.encode(@response.body, :pretty => Configuration.pretty)
      else
        error.message rescue ''
      end
    else
      body = ''
    end

    Configuration.logger.log_response code, nil, body
  end
end
method_missing(method_name, *arguments) click to toggle source

Delegate to the `@attributes` Hash

# File lib/tire/alias.rb, line 130
def method_missing(method_name, *arguments)
  @attributes.has_key?(method_name.to_sym) ? @attributes[method_name.to_sym] : super
end
name(value=nil) click to toggle source

Get or set the alias name

# File lib/tire/alias.rb, line 136
def name(value=nil)
  value ? (@attributes[:name] = value and return self) : @attributes[:name]
end
routing(value=nil) click to toggle source

Get or set the alias routing

# File lib/tire/alias.rb, line 150
def routing(value=nil)
  value ? (@attributes[:routing] = value and return self) : @attributes[:routing]
end
save() click to toggle source

Save the alias in Elasticsearch

# File lib/tire/alias.rb, line 161
def save
  @response = Configuration.client.post "#{Configuration.url}/_aliases", to_json

ensure
  logged '_aliases', %Qcurl -X POST "#{Configuration.url}/_aliases" -d '#{to_json}'|
end
to_json(options=nil) click to toggle source

Return alias serialized in JSON for Elasticsearch

# File lib/tire/alias.rb, line 189
def to_json(options=nil)
  as_json.to_json
end
to_s() click to toggle source
# File lib/tire/alias.rb, line 197
def to_s
  name
end