2013-07-22 96 views
0

我試圖將嵌套標籤索引到我的產品模型。 產品索引良好,但沒有與產品關聯的嵌套標籤。 我該怎麼辦?我的映射是否正確?Rails elasticsearch輪胎嵌套映射

Product Class 

    include Tire::Model::Search 
    include Tire::Model::Callbacks 

mapping do 
    indexes :id, type: 'integer', index: :not_analyzed 
    indexes :name, type: 'string', analyzer: 'snowball', boost: 100 
    indexes :description, analyzer: 'snowball' 
    indexes :price, type: 'float' 
    indexes :category, type: 'string' 
    indexes :location, type: 'string' 
    indexes :online, type: 'boolean' 
    indexes :created_at, type: 'date', index: :not_analyzed 
    indexes :updated_at, type: 'date', index: :not_analyzed 

    indexes :tags do 
     indexes :id, type: 'integer', index: :not_analyzed 
     indexes :name, type: 'string', analyzer: 'snowball', boost: 100 
    end 
    end 

    def to_indexed_json 
    { 
     id: id, 
     name: name, 
     description: description, 
     price: price, 
     category: category, 
     location: location, 
     online: online, 
     created_at: created_at, 
     updated_at: updated_at, 
     include: { tags: { only: [:name] } } 
    }.to_json 
    end 

謝謝!

回答

1

好吧,我已經找到了答案:

def to_indexed_json 
    { 
     name: name, 
     description: description, 
     price: price, 
     category: category, 
     location: location, 
     online: online, 
     created_at: created_at, 
     updated_at: updated_at, 
     tags: tags 
    }.to_json 
    end 

而且不需要包括ID,和的updated_at created_at的映射,因爲它會自動建立索引。謝謝輪胎!