Skip to content

Instantly share code, notes, and snippets.

@13k
Last active August 29, 2015 14:02
Show Gist options
  • Save 13k/4b44f2da282f0e2d9f98 to your computer and use it in GitHub Desktop.
Save 13k/4b44f2da282f0e2d9f98 to your computer and use it in GitHub Desktop.
class Target
class_attribute :index_mappings
class << self
def mappings(*args, &block)
self.index_mappings ||= { options: {}, mappings: {} }
mappings = nil
options = nil
if block_given?
options = args.extract_options!
mappings = Elasticsearch::Model::Indexing::Mappings.new(document_type, options)
mappings.instance_eval(&block)
mappings = mappings.to_hash[document_type.to_sym][:properties]
elsif args.first.respond_to?(:to_hash)
mappings = args.first
end
if mappings.present?
index_mappings[:mappings] = index_mappings[:mappings].deep_merge(mappings.to_hash)
expire_mappings_hash!
end
if options.present?
index_mappings[:options] = index_mappings[:options].deep_merge(options.to_hash)
expire_mappings_hash!
end
mappings_hash
end
alias :mapping :mappings
def mappings_hash
@mappings_hash ||= begin
type_mappings = index_mappings[:options].merge({
properties: index_mappings[:mappings]
})
{ document_type.to_sym => type_mappings }
end
end
def expire_mappings_hash!
@mappings_hash = nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment