2011-12-21 46 views
4

Cross post from GitHub輪胎/ ElasticSearch單表繼承支持

我的應用程序尋找像美味,微博各種第三方服務的鏈接...我有以下的基類:

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 

搜索將通過基類來完成: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