2011-12-21 4 views
4

Cross post from GitHub:шин/ElasticSearch Single Table Inheritance

Мой поиск приложение для ссылок в различных услуг третьих лиц, как Delicious, Twitter ... Я ниже базового класса:

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 

и следующий класс наследуется от Link и добавляет еще два поля:

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 

Поисковые запросы будет осуществляться с помощью класса Base: Link.search('google.com'). Есть ли шанс получить эту работу? В настоящий момент (дополнительные) поля в Link::Delicious полностью игнорируются Tire/ElasticSearch.

ответ

4

Зафиксировано с перезаписью метод mapping следующим образом:

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