Setup Ruby fuzzily gem – small scale alternative to elasticsearch

This is a setup guide for implementing the Ruby gem fuzzily.

Sometimes you have small scale projects that have only a few thousend records (e.g. user database) to be searched in. In those situations you may think that taking out the big guns, like elasticsearch, for a tiny bit of searching is too much. But nonetheless searching should have simple features as taken care of misspelling and be fit for autosuggest. This is where fuzzy search comes in. It uses trigram logic; meaning breaking down every word into as many groups of three adjacent characters as possible and building a searchable index of them. This works fine with MariaDB, Postgress, MySQL and of course Oracle.

1. In your Gemfile add the following line:

2. Run the bundler from you application directory

3. Create a new db migration file in your db/migration/ folder. I prefer the date and time as part of my filename. Thus resulting in a filename 20160608153800_add_trigrams_model.rb containing:

4. Run a migration from your application directory

Now we start implemententing functionality.

5. Create a new model named trigam.rb in your app/models/ folder containing

6. Declare what field should be included in the searchable index. In my case lastname from the user model

Once this is done all future additions and modification to the database will automatically get a trigram index. What about those already in the db.

7. Go into your Rails console (rails console) and manually kickstart the update

8. The final step is make use of the new functionality in your corresponding controller. /app/controllers/users_controller.rb


0 Noone thinks it's helpful 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *