2017-01-21 1 views
1

"전체", "키워드"및 "설명"을 포함하여 검색 기준이 적은 "문서"를 검색하기위한 검색 양식이 있습니다.pg_search_scope : 연결 범위가 불가능한 것 같습니다

나는 pg_search_scope을 사용하고 있지만 2 가지 범위가 있습니다.

이 내 document.rb에 있습니다

pg_search_scope :search_entire_text, 
       :against => :entire_text, 
       :using => { 
        :tsearch => { 
         :prefix => true, 
         :dictionary => "french" 
         } 
        } 

pg_search_scope :search_keywords, 
       :associated_against => { 
        :keywords => [:keyword] 
       }, 
       :using => { 
        :tsearch => { 
         :any_word => true 
        } 
       } 

각 개별적으로 잘 작동합니다. 그러나 나는 이것을 할 수 없다 :

@resultats = Document.search_keywords(params[:ch_document][:keywords]).search_entire_text(params[:ch_document][:entire_text]) 

이 문제를 해결할 방법이 있습니까?

감사

답변

1

나는 pg_search_scope을 사용한 적이 있지만 실제로 두 pg_search_scope 년대를 결합 할 수 없습니다 것 같습니다.

당신이 할 수있는 일은 pg_search_scope와 함께 :search_entire_text을 사용하고 Document.where([1,2,3])에 결과 ID를 사용하여 나머지 키워드 검색에 표준 레일 범위를 사용할 수 있습니다.

예 :

그것은 작동
# If pluck doesn't work you can also use map(&:id) 
txt_res_ids = Document.search_entire_text(params[:ch_document][:entire_text]).pluck(:id) 
final_results = Document.where(id: txt_res_ids).some_keyword_scope.all 
+0

실제로 작동합니다. 나는지도를 사용했으나 뽑아 내기가 더 쉽다. – thiebo

0

.

Acte.rb

pg_search_scope :cherche_texte_complet, #i.e. find entire text 
       :against => :texte_complet, 
       :using => { 
        :tsearch => { 
         :prefix => true, 
         :dictionary => "french" 
         } 
        } 

pg_search_scope :cherche_mots_clefs, #find keywords 
       :associated_against => { 
        :motclefs => [:motcle] 
       }, 
       :using => { 
        :tsearch => { 
         :any_word => true 
        } 
       } 

def self.cherche_date(debut, fin) #find date between 
    where("acte_date BETWEEN :date_debut AND :date_fin", {date_debut: debut, date_fin: fin}) 
end 

def self.cherche_mots(mots) 
    if mots.present? #the if ... else is necessary, see controller.rb 
     cherche_mots_clefs(mots) 
    else 
     order("id DESC") 
    end 
end 

def self.ids_texte_compl(ids) 
    if ids.any? 
     where("id = any (array #{ids})") 
    else 
     where("id IS NOT NULL") 
    end 
end 

actes_controller.rb

(나는 영어로 번역하지 않았다, 그 설명은 위의 질문에 대응 주석) : 지금이 사람을 도울 수 있다면 여기에 전체 코드는 ...입니다
ids = Acte.cherche_texte_complet(params[:ch_acte][:texte_complet]).pluck(:id) 

@resultats = Acte.cherche_date(params[:ch_acte][:date_debut],params[:ch_acte][:date_fin]) 
.ids_texte_compl(ids) 
.cherche_mots(params[:ch_acte][:mots]) 

감사합니다.

관련 문제