4

Cross post from GitHub:

My app search for links in various 3rd-party services like Delicious, Twitter … I have following base class:

class Link
  include Mongoid::Document
  include Tire::Model::Search
  include Tire::Model::Callbacks

  field :href, type: String
  field :name, type: String

  mapping do
    indexes :href, type: 'string', analyzer: 'url'
    indexes :name, type: 'string', analyzer: 'keyword', boost: 10
  end
end

and following class inherits from Link and adds two more fields:

class Link::Delicious < Link
  field :tags, type: Array
  field :time, type: Time

  mapping do
    indexes :tags, type: 'string', analyzer: 'keyword'
    indexes :time, type: 'date'
  end
end

Searches will be done via the Base class: Link.search('google.com'). Is there any chance to get this working? At the moment the (additional) fields in Link::Delicious are completely ignored by Tire/ElasticSearch.

Mario Uher
  • 12,249
  • 4
  • 42
  • 68

1 Answers1

4

Fixed with overwriting the mapping method like so:

class Link
  # …

  class << self
    def mapping_with_super(*args, &block)
      # Creating only one index
      index_name('links')
      document_type('link')

      superclass.mapping_without_super.each do |name, options|
        indexes(name, options)
      end if superclass.respond_to?(:mapping)

      mapping_without_super(args, &block)
    end
    alias_method_chain :mapping, :super
  end
end
Mario Uher
  • 12,249
  • 4
  • 42
  • 68