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

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options = {}) ⇒ Mappings

Returns a new instance of Mappings.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/elasticsearch/model/indexing.rb', line 39

def initialize(type, options={})
  raise ArgumentError, "`type` is missing" if type.nil?

  @type    = type
  @options = options
  @mapping = {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



37
38
39
# File 'lib/elasticsearch/model/indexing.rb', line 37

def options
  @options
end

Instance Method Details

#as_json(options = {}) ⇒ Object



76
77
78
# File 'lib/elasticsearch/model/indexing.rb', line 76

def as_json(options={})
  to_hash
end

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/elasticsearch/model/indexing.rb', line 47

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

  if block_given?
    @mapping[name][:type] ||= 'object'
    properties = @mapping[name][:type] == 'multi_field' ? :fields : :properties

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

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

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

  self
end

#to_hashObject



72
73
74
# File 'lib/elasticsearch/model/indexing.rb', line 72

def to_hash
  { @type.to_sym => @options.merge( properties: @mapping ) }
end