Skip to content

Custom mappings with elasticsearch_autocomplete

Alex Leschenko edited this page Jul 1, 2013 · 1 revision

If you need some custom mappings in your model, you can just define your mapping and it will be merged with elasticsearch_autocomplete mapping by the tire gem:

class Product < ActiveRecord::Base
  ac_field :name

  mapping _source: {enabled: true, includes: %w(id slug title)} do
    indexes :slug
  end

end

it is important to supply a block to mapping method.

But if you need the full control over the mapping definition, you can use skip_settings option and call ac_index_config method directly to get mapping for particular field:

class Product < ActiveRecord::Base
  ac_field :name, :skip_settings => true

  model = self
  settings ElasticsearchAutocomplete::Analyzers::AC_BASE do
    mapping _source: {enabled: true, includes: %w(id slug title)} do
      indexes :name, model.ac_index_config(:name)
    end
  end

end