In this code snippet I’ll show how to integrate a Rails 4 model with Elasticsearch and find related models via matching tags.
Create a new Rails project:
Add the acts-as-taggable-on gem for tagging models. edit file: Gemfile, add:
Add (interests) tags to User model. edit file: app/models/user.rb
Add Elasticsearch gems. Edit file: Gemfile, add the following, and execute:
Integrate User model with Elasticsearch. edit file: app/models/user.rb
Time to populate the user model. For this tutorial I used the faker gem. Edit file: Gemfile, add:
gem 'faker'. Execute:
bundle install to install.
Edit file: db/seeds.rb, add:
Via rails console, create the Elasticsearch index for the User model.
Populate the User model by executing:
Check data structure in Elasticsearch via cURL:
Via Rails console, find related Users via tags:
Source code on GitHub