0

如果我理解pg_search文檔很好,只有一個表,用於存儲整個網站的「搜索」的文檔,所以我的問題是:如何在多語言網站中使用pg_search?

有什麼辦法,比過濾通過語言的搜索結果等,允許做一個語言特定的搜索?我可以用每個「文檔」存儲它所寫入的語言,並以某種方式使用它來限制查詢?

回答

-1

我終於寫了我的控制器操作如下

require 'will_paginate/array' 

def search 
    @items = PgSearch.multisearch(params["search"]) 
    @items = @items.map { |item| item.searchable.document }.compact.uniq 
    @items = @items.paginate(per_page: 5, page: params[:page]) 
end 

其中每個搜索模式有一個文檔屬性「決定」是否包括結果與否,如果是的話,它的「文件」。這可以是作爲檢查其語言簡單:

def document 
    self if locale == I18n.locale.to_s 
end 

,或者可以是更復雜的,它的傳播「文件」屬性到它的父,例如在多態評論的情況下:

def document 
    commentable.document 
end 
+0

此解決方案不允許鏈接範圍,如果數據集很大,可能會非常慢和/或CPU密集型! – Vala