https://github.com/hopsoft/polysearch
Simplified polymorphic full text + similarity search based on postgres
https://github.com/hopsoft/polysearch
Last synced: about 1 month ago
JSON representation
Simplified polymorphic full text + similarity search based on postgres
- Host: GitHub
- URL: https://github.com/hopsoft/polysearch
- Owner: hopsoft
- License: mit
- Created: 2021-05-18T21:29:32.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-24T17:57:50.000Z (over 4 years ago)
- Last Synced: 2025-08-04T19:12:12.524Z (2 months ago)
- Language: Ruby
- Size: 31.3 KB
- Stars: 39
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
# Polysearch
Simplified polymorphic full text + similarity search based on postgres.
> NOTE: This project is narrower in scope and more opinionated than [pg_search](https://github.com/Casecommons/pg_search).
## Requirements
- Postgresql >= 11
- Rails >= 6.0## Usage
1. Add the gem to your project
```sh
bundle add polysearch
```1. Run the generator
```sh
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 `uuid` primary keys, you install the migration like this.```sh
bundle exec rails g polysearch:migration uuid
```1. Migrate the database
```sh
bundle exec rails db:migrate
```1. Update the model(s) you'd like to search
```ruby
class User < ApplicationRecord
include Polysearch::Searchableafter_save_commit :update_polysearch
def to_tsvectors
[
make_tsvector(first_name, weight: "A"),
make_tsvector(last_name, weight: "A"),
make_tsvector(nickname, weight: "B")
]
end
end
```If you have existing records that need to create/update a polysearch record, you can save them like this.
```ruby
User.find_each(&:update_polysearch)
```1. Start searching
```ruby
User.create first_name: "Shawn", last_name: "Spencer", nickname: "Maverick"# find natural language matches (faster)
User.full_text_search("shawn")# find similarity matches, best for misspelled search terms (slower)
User.similarity_search("shwn")# perform both a full text search and similarity search
User.combined_search("shwn")# perform a full text search and fall back to similarity search (faster than combined_search)
User.polysearch("shwn")# calculate counts (explicitly pass :id to omit search rankings)
User.full_text_search("shawn").count(:id)
User.similarity_search("shwn").count(:id)
User.combined_search("shwn").count(:id)
User.polysearch("shwn").count(:id)
```## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).