Last time we used pg_search to search a single model, but we also have the option to search multiple models. To support multisearch we will need to:
Fortunately, this is quick and painless. pg_search includes a migration for us se we just need to copy that over and run it:
rails g pg_search:migration:multisearch
rake db:migrate
pg_search needs to know what to index and search against and keep track of updates to the model. This is done with the multisearchable method:
class MyModel < ApplicationRecord
include PgSearch::Model
multisearchable against: [:title, :description]
end
class MyOtherModel < ApplicationRecord
include PgSearch::Model
multisearchable against: [:title, :body]
end
With multisearch, a index table is created of all searchable content. Any time you add or change the fields configured with multisearchable, you need to rebuild the search index so that it will update with new information.
PgSearch::Multisearch.rebuild(MyModel)
PgSearch::Multisearch.rebuild(MyOtherModel)
With pg_search installed, configured, and re-built, its now ready to be used. All we need to do is update the global searches to use multisearch instead of the single model search:
# app/controllers/global_searches_controller.rb
class GlobalSearchesController < ApplicationController
def show
@results = PgSearch.multisearch(params[:q])
end
end
multisearch returns an ActiveRecord::Relation, so you can chain it with other options, like limit, and use it anywhere you would normally, such as with pagination.