Class: Fields

Inherits:
Object
  • Object
show all
Defined in:
lib/es_fields/fields.rb

Constant Summary collapse

BASE_MAPPING =
{
  "mappings": {
    "doc": {
      "properties": {
      }
    }
  }
}
TYPE_MAPPING =
{
  string: 'text',
  long: 'long',
  date: 'date',
  keyword: 'keyword',
  double: 'double',
  boolean: 'boolean',
  ip: 'ip',
}
OPTIONS_MAPPING =
{
  sortable: 'keyword',
  full_text_search: 'text',
  aggregation: 'keyword',
  filtering: 'keyword',
}

Instance Method Summary collapse

Constructor Details

#initializeFields

Returns a new instance of Fields.



29
30
31
# File 'lib/es_fields/fields.rb', line 29

def initialize
  @base_mapping = BASE_MAPPING.dup
end

Instance Method Details

#field(name, type, options: []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/es_fields/fields.rb', line 33

def field(name, type, options: [])
  @base_mapping[:mappings][:doc][:properties].merge!(
    {
      name => {
        type: options.empty? ? TYPE_MAPPING[type] : OPTIONS_MAPPING[options.first]
      }
    }
  )
  @base_mapping
end