Class: Elasticsearch::Model::Indexing::Mappings

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/model/indexing.rb

Overview

Constant Summary collapse

TYPES_WITH_EMBEDDED_PROPERTIES =
%w(object nested)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mappings

Returns a new instance of Mappings.



56
57
58
59
# File 'lib/elasticsearch/model/indexing.rb', line 56

def initialize(options={})
  @options = options
  @mapping = {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



52
53
54
# File 'lib/elasticsearch/model/indexing.rb', line 52

def options
  @options
end

Instance Method Details

#as_json(options = {}) ⇒ Object



89
90
91
# File 'lib/elasticsearch/model/indexing.rb', line 89

def as_json(options={})
  to_hash
end

#indexes(name, options = {}, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/elasticsearch/model/indexing.rb', line 61

def indexes(name, options={}, &block)
  @mapping[name] = options

  if block_given?
    @mapping[name][:type] ||= 'object'
    properties = TYPES_WITH_EMBEDDED_PROPERTIES.include?(@mapping[name][:type].to_s) ? :properties : :fields

    @mapping[name][properties] ||= {}

    previous = @mapping
    begin
      @mapping = @mapping[name][properties]
      self.instance_eval(&block)
    ensure
      @mapping = previous
    end
  end

  # Set the type to `text` by default
  @mapping[name][:type] ||= 'text'

  self
end

#to_hashObject



85
86
87
# File 'lib/elasticsearch/model/indexing.rb', line 85

def to_hash
  @options.merge( properties: @mapping )
end