module Elastics::Tasks::Mappings

Attributes

mappings_path[W]

Public Instance Methods

fix_mapping(name, mapping) click to toggle source

Adds missing type name in the top-level and updates properties definition. It allows to write

properties:
  name: string
  project_id: integer

instead of

task:
  properties:
    name:
      type: string
    project_id:
      type: integer
# File lib/elastics/tasks/mappings.rb, line 79
def fix_mapping(name, mapping)
  mapping = {name => mapping} unless mapping.keys == [name]
  properties = mapping[name]['properties']
  properties && properties.each do |field, val|
    properties[field] = {type: val} if val.is_a?(String)
  end
  mapping
end
index_for_type(type) click to toggle source
# File lib/elastics/tasks/mappings.rb, line 96
def index_for_type(type)
  config[:index] || type
end
indices() click to toggle source
Calls superclass method
# File lib/elastics/tasks/mappings.rb, line 92
def indices
  @indices ||= (super + types.map { |type| index_for_type(type) }).uniq
end
mappings() click to toggle source

Merges mappings from single files and dirs.

# File lib/elastics/tasks/mappings.rb, line 25
def mappings
  @mappings ||= mappings_from_files.merge!(mappings_from_dirs)
end
mappings_from_dirs() click to toggle source

Reads mappings from separate files. Type name is taken from file name.

# user.yml
dynamic: false
properties:
  name: string
# File lib/elastics/tasks/mappings.rb, line 55
def mappings_from_dirs
  mappings_paths.map { |path| Dir["#{path}/*.yml"] }.
    flatten.sort.
    each_with_object({}) do |file, hash|
      name = File.basename file, '.yml'
      hash[name] = fix_mapping(name, load_yaml(file))
    end
end
mappings_from_files() click to toggle source

Reads mappings from single yml file.

user:
  dynamic: false
  properties:
    name: string
tweet:
  properties:
    content: string
    user_id: integer
# File lib/elastics/tasks/mappings.rb, line 39
def mappings_from_files
  mappings_paths.each_with_object({}) do |path, hash|
    file = "#{path}.yml"
    next unless File.exists?(file)
    load_yaml(file).each do |name, data|
      hash[name] = fix_mapping(name, data)
    end
  end
end
mappings_paths() click to toggle source
# File lib/elastics/tasks/mappings.rb, line 6
def mappings_paths
  @mappings_paths ||= base_paths.map { |x| File.join x, 'mappings' }
end
put_mappings(options = {}) click to toggle source

Mappings to put can be filtered with `:indices` & `:types` arrays.

# File lib/elastics/tasks/mappings.rb, line 11
def put_mappings(options = {})
  version = options.fetch :version, :current
  filter = options[:indices].try!(:map, &:to_s)
  each_filtered(types, options[:types]) do |type|
    index = index_for_type(type)
    next if filter && !filter.include?(index)
    versioned_index = versioned_index_name(index, version)
    log "Putting mapping #{index}/#{type} (#{versioned_index}/#{type})"
    client.put_mapping index: versioned_index, type: type,
      body: mappings[type]
  end
end
types() click to toggle source
# File lib/elastics/tasks/mappings.rb, line 88
def types
  @types ||= mappings.keys
end