2014-06-05 28 views
0

我使用轮胎来实现Elasticsearch。我做了一个表继承一切工作正常,但问题是我也想对所有的职位搜索看看我的代码:Rails中单表继承的轮胎搜索

# Post is Parent Class 
class Post < ActiveRecord::Base 
    include Tire::Model::Search 
    include Tire::Model::Callbacks  

    mapping do 
    indexes :title,  type: 'string' 
    indexes :location, type: 'string' 
    indexes :key_words, type: 'string' 
    indexes :work_type, type: 'string' 
    end 

    def self.search(params) 
    search = tire.search do 
     if params[:location].present? || params[:query].present? 
     query do 
      string "location:#{params[:location]}" + "*", default_operator: 'AND' if params[:location].present? 
      string "title:#{params[:query]}" + "*",  default_operator: 'AND' if params[:query].present? 
      string "key_words:#{params[:query]}" + "*", default_operator: 'AND' if params[:query].present? 
     end 
     end 
     filter :terms, work_type: params[:work_types] if params[:work_types].present? 
    end 
    search.results 
    end 

# child class 

class Job < Post 
    tire.index_name 'posts' 
end 

# another child class 

class Bid < Post 
    tire.index_name 'posts' 
end 

以下是几件事情我想做的事:

Post.search(params) 
OR 
Job.search(params) 
Bid.search(params) 

如何使搜索与STI发生?

回答

1

除了tire.index_name你必须定义tire.document_type,让您的孩子班级应该像下面

# child class 

class Job < Post 
    tire.index_name 'posts' 
    tire.document_type 'post' 
end 

# another child class 

class Bid < Post 
    tire.index_name 'posts' 
    tire.document_type 'post' 
end 

轮胎是死的项目,你应该考虑使用新的官方elasticsearch gem