Question

If I understand the pg_search documentation well, there is only one table storing "searchable" documents for the entire website, so my question is:

Is there any way, other than filtering the search results by language, that allows to do a language-specific search? Can I e.g. store with each "document" the language it's written in, and use this in some way to limit the query?

Was it helpful?

Solution

I finally wrote my controller action as follows

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

where each searchable model has a document property, "deciding" whether to include the result or not, and if so, which "document". This can be as simple as checking its language:

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

or can be more complex, propagating its "document" property to its parent, e.g. in case of a polymorphic comment:

def document
  commentable.document
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top