Sha256: 0c9c0f33be691d453a021b30749226597f6cfa95441055ee52cc5d46315b3251

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Esse
  class IndexType
    # https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/indices/put_mapping.rb
    module ClassMethods
      # This method is only used to define mapping
      def mappings(hash = {}, &block)
        @mapping = Esse::IndexMapping.new(body: hash, paths: template_dirs, filenames: mapping_filenames)
        return unless block_given?

        @mapping.define_singleton_method(:as_json, &block)
      end

      # This is the actually content that will be passed through the ES api
      def mappings_hash
        hash = mapping.body
        {
          type_name => (hash.key?('properties') ? hash : { 'properties' => hash }),
        }
      end

      private

      def mapping
        @mapping ||= Esse::IndexMapping.new(paths: template_dirs, filenames: mapping_filenames)
      end

      def template_dirs
        return [] unless respond_to?(:index)

        index.template_dirs
      end

      def mapping_filenames
        Esse::IndexMapping::FILENAMES.map { |str| [type_name, str].join('_') }
      end
    end

    extend ClassMethods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esse-0.0.2 lib/esse/index_type/mappings.rb