2014-06-05 15 views
0

나는 Elasticsearch 구현을 위해 타이어를 사용하고 있습니다. 내가 수행 한 단일 테이블 상속 모든 것이 잘 작동하지만 문제는 내가 아니라 내 코드를 살펴있는 모든 게시물에 검색 할 수 있습니다 :레일에서 단일 테이블 상속을 사용하여 타이어 검색

: 다음

# 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를 사용하여 검색을 수행하려면 어떻게해야합니까? 당신이 tire.document_type을 정의해야 tire.index_name 외에

답변

1

, 그래서 당신의 아이 클래스

# 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 새를 사용하는 것이 좋습니다.