Sha256: 10b872a6600a55e72f8e6bd204f05bc1fefad718835aab312901a217e707c589

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Esse
  class IndexType
    # https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-put-mapping.html
    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

        @mapping.define_singleton_method(:to_h, &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

4 entries across 4 versions & 1 rubygems

Version Path
esse-0.2.0 lib/esse/index_type/mappings.rb
esse-0.1.3 lib/esse/index_type/mappings.rb
esse-0.1.2 lib/esse/index_type/mappings.rb
esse-0.1.1 lib/esse/index_type/mappings.rb