Module: ElasticMapper::Mapping::ClassMethods

Defined in:
lib/elastic_mapper/mapping.rb

Instance Method Summary collapse

Instance Method Details

#mapping(*args) ⇒ Object

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.

Parameters:

  • args (*args)

    symbols representing the fields to index.



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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_hashHash

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

Returns:

  • (Hash)

    mapping.



85
86
87
88
89
90
91
# 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) ⇒ Object

Override the default name of the mapping.

Parameters:

  • mapping_name (String)

    name of mapping.



77
78
79
# File 'lib/elastic_mapper/mapping.rb', line 77

def mapping_name(mapping_name)
  @_mapping_name = mapping_name.to_sym
end

#put_mappingObject

Create the described mapping in ElasticSearch.



94
95
96
97
98
99
100
# File 'lib/elastic_mapper/mapping.rb', line 94

def put_mapping
  ElasticMapper.index
    .type(@_mapping_name)
    .put_mapping(mapping_hash)
  
  ElasticMapper.index.refresh
end