2014-04-01 54 views
1
Want to search for the title from the board with live_flag true. 

class Board < ActiveRecord::Base 
    has_many :deals 
    include Tire::Model::Search 

    tire.mapping do 
    indexes :title, type: 'string' 
    indexes :deals do 
     indexes :title, type: 'string' 
    end 
    end 

    def to_indexed_json 
    { 
    :title   => title, 
    :deals  => {:title => self.deals.map(&:title)}, 
    }.to_json 
    end 

    def self.search(params) 
    tire.search(load: true) do 
     query {string params[:query]+"*"} 
     filter :term, live_flag: true 
     sort { by :created_at, "desc" } if params[:query].blank? 
    end 
    end 

end 



Now it will not search anything. 


It works properly when below code is used. Difference is that i have removed filter text from it. 

    def self.search(params) 
    tire.search(load: true) do 
     query {string params[:query]+"*"} 
     sort { by :created_at, "desc" } if params[:query].blank? 
    end 
    end 

    **But i want to get boards whose live_flag is true.** 

回答

3

现在你的指数不包括live_flag 只需添加live_flag您to_indexed_json和映射

tire.mapping do 
    indexes :title, type: 'string' 
    indexes :live_flag, type: 'boolean' 
    indexes :deals do 
     indexes :title, type: 'string' 
    end 
    end 

    def to_indexed_json 
    { 
    :title   => title, 
    :live_flag  => live_flag, 
    :deals  => {:title => self.deals.map(&:title)}, 
    }.to_json 
    end 
+1

现在你已经编辑您的帖子后,这是一个完全合格的答卷。第一个版本(只有一句话,没有代码)'被Stack Overflow'评为低质量文章。这是我的责任,我有权评估你的答案。详细了解SO的工作原理。不要亲自评论。我们都致力于提高问题和答案的质量。这种方法最好的证明是,你已经编辑了你的答案,并且现在它符合SO标准 - 这是为了其他用户的好处。 – ElmoVanKielmo

+0

我以为你在错误的问题在哪里,你是我说的。无论如何。 –

+0

非常感谢。它开始工作 –