module ElasticMapper::Mapping::ClassMethods

Public Instance Methods

mapping(*args) click to toggle source

Populates @_mapping with properties describing how the model should be indexed in ElasticSearch. The last parameter is optionally a hash specifying index settings, e.g., analyzed, not_analyzed.

@param args [*args] symbols representing the

fields to index.
# File lib/elastic_mapper/mapping.rb, line 27
def mapping(*args)
  options = {
    :type => :string,
    :index => :analyzed
  }.update(args.extract_options!)

  args.each do |arg|
    _mapping[mapping_key(arg)] = {
      field: arg,
      options: options
    }
  end
end
mapping_hash() click to toggle source

Generates a hash representation of @_mapping, compatible with ElasticSearch.

@return [Hash] mapping.

# File lib/elastic_mapper/mapping.rb, line 85
def mapping_hash
  {
    @_mapping_name => {
      properties: @_mapping.inject({}) { |h, (k, v)| h[k] = v[:options]; h }
    }
  }
end
mapping_name(mapping_name) click to toggle source

Override the default name of the mapping.

@param mapping_name [String] name of mapping.

# File lib/elastic_mapper/mapping.rb, line 77
def mapping_name(mapping_name)
  @_mapping_name = mapping_name.to_sym
end
put_mapping() click to toggle source

Create the described mapping in ElasticSearch.

# File lib/elastic_mapper/mapping.rb, line 94
def put_mapping
  ElasticMapper.index
    .type(@_mapping_name)
    .put_mapping(mapping_hash)
  
  ElasticMapper.index.refresh
end

Private Instance Methods

_mapping() click to toggle source

Return the _mapping instance variable, used to keep track of a model's mapping definition. id is added to the model by default, and is used to map the model back onto an ActiveModel object.

@return [Hash] the mapping description.

# File lib/elastic_mapper/mapping.rb, line 47
def _mapping
  @_mapping ||= { id: {
    :field => :id,
    :options => { :type => :integer, :index => :no }
  }}
end
mapping_key(key) click to toggle source

Create a unique key name for the mapping. there are times where you might want to index the same field multiple time, e.g., analyzed, and not_analyzed.

@param key [String] the original key name. @return [String] the de-duped key name.

# File lib/elastic_mapper/mapping.rb, line 61
def mapping_key(key)
  counter = 1
  mapping_key = key

  while @_mapping.has_key?(mapping_key)
    counter += 1
    mapping_key = "#{key}_#{counter}".to_sym
  end

  return mapping_key
end