Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Latest commit 7815039 Dec 4, 2017
karmi committed Dec 4, 2017 [MODEL] Fixed the handling of changed attributes in `Indexing` to wor…
…k with older Rails versions

This patch builds on work in #738 by @jkeam and #758 by @Geesu to prevent deprecation warnings on Rails 5.1
due to changes in the handling of "changed attributes", originally reported by @davedash in #714.

It's primary focus is to restore the compatibility with older Rails versions (so we don't break compatibility
without proper version bump and in a single isolated commit), and to make the naming a bit more descriptive.

First, the condition has been changed to work with the `changes_to_save` and `changes` methods, as opposed
to the original `changed_attributes` / `attributes_in_database` naming. This communicates much more clearly
the intent, and the original usage of `changed_attributes` has been misleading.

Also, the "internal instance variable" which keeps track of the changes has been renamed to `@__changed_model_attributes`,
in order to cleary differentiate it from the `changed_attributes` method name in older ActiveRecord versions.

Closes #758



This repository contains various Ruby and Rails integrations for Elasticsearch:

  • ActiveModel integration with adapters for ActiveRecord and Mongoid
  • Repository pattern based persistence layer for Ruby objects
  • Active Record pattern based persistence layer for Ruby models
  • Enumerable-based wrapper for search results
  • ActiveRecord::Relation-based wrapper for returning search results as records
  • Convenience model methods such as search, mapping, import, etc
  • Rake tasks for importing the data
  • Support for Kaminari and WillPaginate pagination
  • Integration with Rails' instrumentation framework
  • Templates for generating example Rails application

Elasticsearch client and Ruby API is provided by the elasticsearch-ruby project.


The libraries are compatible with Ruby 1.9.3 and higher.

The version numbers follow the Elasticsearch major versions, and the master branch is compatible with the Elasticsearch master branch, therefore, with the next major version.

Rubygem Elasticsearch
0.1 1.x
2.x 2.x
5.x 5.x
master master


Install each library from Rubygems:

gem install elasticsearch-model
gem install elasticsearch-rails

To use an unreleased version, add it to your Gemfile for Bundler:

gem 'elasticsearch-model', github: 'elastic/elasticsearch-rails', branch: '5.x'
gem 'elasticsearch-rails', github: 'elastic/elasticsearch-rails', branch: '5.x'


This project is split into three separate gems:

Example of a basic integration into an ActiveRecord-based model:

require 'elasticsearch/model'

class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks


@articles = Article.search('foobar').records

You can generate a simple Ruby on Rails application with a single command (see the other available templates):

rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/01-basic.rb

Example of using Elasticsearch as a repository for a Ruby domain object:

require 'virtus'
class Article
  include Virtus.model
  attribute :title, String

require 'elasticsearch/persistence'
repository = Elasticsearch::Persistence::Repository.new

repository.save Article.new(title: 'Test')
# POST http://localhost:9200/repository/article
# => {"_index"=>"repository", "_type"=>"article", "_id"=>"Ak75E0U9Q96T5Y999_39NA", ...}

Example of using Elasticsearch as a persistence layer for a Ruby model:

require 'elasticsearch/persistence/model'
class Article
  include Elasticsearch::Persistence::Model
  attribute :title, String, mapping: { analyzer: 'snowball' }

Article.create title: 'Test'
# POST http://localhost:9200/articles/article
# => #<Article {title: "Test", id: "lUOQ9lhHToWa7oYPxwjqPQ", ...}>

Please refer to each library documentation for detailed information and examples.





Build Status Code Climate

To work on the code, clone the repository and install all dependencies first:

git clone https://github.com/elastic/elasticsearch-rails.git
cd elasticsearch-rails/
bundle install
rake bundle:install

Running the Test Suite

You can run unit and integration tests for each sub-project by running the respective Rake tasks in their folders.

You can also unit, integration, or both tests for all sub-projects from the top-level directory:

rake test:all

The test suite expects an Elasticsearch cluster running on port 9250, and will delete all the data. You can launch an isolated, in-memory Elasticsearch cluster with the following Rake task:

TEST_CLUSTER_COMMAND=/tmp/builds/elasticsearch-2.0.0-SNAPSHOT/bin/elasticsearch TEST_CLUSTER_NODES=1 bundle exec rake test:cluster:start

See more information in the documentation for the elasticsearch-extensions gem.


This software is licensed under the Apache 2 license, quoted below.

Copyright (c) 2014 Elasticsearch <http://www.elasticsearch.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.