Class: Elastictastic::Field

Inherits:
BasicObject
Defined in:
lib/elastictastic/field.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, default_options, properties, &block) ⇒ Field

Returns a new instance of Field.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/elastictastic/field.rb', line 23

def initialize(field_name, default_options, properties, &block)
  @field_name = field_name
  @properties = properties
  if block
    @properties['type'] = 'multi_field'
    @properties['fields'] =
      { field_name.to_s => Field.with_defaults(default_options) }
    instance_eval(&block)
  else
    @properties.merge!(Field.with_defaults(default_options))
  end
end

Class Method Details

.process(field_name, default_options, &block) ⇒ Object



5
6
7
8
9
# File 'lib/elastictastic/field.rb', line 5

def self.process(field_name, default_options, &block)
  {}.tap do |properties|
    new(field_name, default_options, properties, &block)
  end
end

.with_defaults(options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/elastictastic/field.rb', line 11

def self.with_defaults(options)
  options = Util.deep_stringify(options)
  if preset = options.delete('preset')
    options = ::Elastictastic.config.presets[preset].merge(options)
  end
  { 'type' => 'string' }.merge(options).tap do |field_properties|
    if field_properties['type'].to_s == 'date'
      field_properties['format'] = 'date_time_no_millis'
    end
  end
end

Instance Method Details

#field(field_name, options = {}) ⇒ Object



36
37
38
39
# File 'lib/elastictastic/field.rb', line 36

def field(field_name, options = {})
  @properties['fields'][field_name.to_s] =
    Field.with_defaults(options)
end