2011-12-21 4 views
4

Cross post from GitHub :타이어/ElasticSearch 단일 테이블 상속 지원

... 내가 기본 클래스 다음 한 맛있는, 트위터와 같은 다양한 제 3 자 서비스에 링크 내 애플 검색 :

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 

하고 다음과 같은 클래스에서 상속

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와 두 더 많은 필드를 추가합니다. 이 작업을 할 수있는 기회가 있습니까? 현재 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