Simplified polymorphic full text + similarity search based on postgres
NOTE: This project is narrower in scope and more opinionated than pg_search.
- Postgresql >= 11
- Rails >= 6.0
Add the gem to your project
bundle add polysearch
Run the generator
bundle exec rails g polysearch:migration
You can also specify a datatype that your app uses for primary keys (default is
bigint). For example, if your application uses
uuidprimary keys, you install the migration like this.
bundle exec rails g polysearch:migration uuid
Migrate the database
bundle exec rails db:migrate
Update the model(s) you'd like to search
class User < ApplicationRecord include Polysearch::Searchable after_save_commit :update_polysearch def to_tsvectors [ make_tsvector(first_name, weight: "A"), make_tsvector(last_name, weight: "A"), make_tsvector(email, weight: "B") ] end end
If you have existing records that need to create/update a polysearch record, you can save them like this.
User.create first_name: "Nate", last_name: "Hopkins", email: "[email protected]" User.polysearch("nate") User.polysearch("ntae") # misspellings also return results User.polysearch("nate").where(created_at: 1.day.ago..Current.time) # active record chaining User.polysearch("nate").order(created_at: :desc) # chain additional ordering after the polysearch scope
The gem is available as open source under the terms of the MIT License.