Question

Is there a way of getting the pg_search results from the model and not the indexing table entries? My code works, but I have to loop through the results.

search = PgSearch.multisearch(params[:search])

@items = Array.new
search.find_each do |result|
  @items.push(result.searchable)
end
Was it helpful?

Solution

Multisearch will always return PgSearch::Document records.

If you want to query your model directly, you can define a pg_search_scope, e.g.:

pg_search_scope :custom_search, :against => [:title] # Can use multiple fields

And then use it with search = Model.custom_search(params[:search]), which will return Model records.

Also, a cleaner way to write your code above would be:

search = PgSearch.multisearch(params[:search])
@items = search.map(&:searchable)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top