Elasticsearch gem for Rails 2.3
Currently our app is 8 years old and it's still based on Rails 2.3 .
Recently we're adding Elasticsearch to improve performance for some searches, but the existing Elasticsearch-Rails ruby gem requires Rails 3+.
For that reason I develop an Elasticsearch Rails gem for Rails 2.3.
The elasticsearch-rails2 library is based on the elasticsearch-model (one of the components of the elasticsearch-rails gem) and builds on top of the elasticsearch ruby library.
It aims to simplify integration of Ruby on Rails 2.3 models (ActiveRecord) with the Elasticsearch search.
require 'elasticsearch/rails2' class Article < ActiveRecord::Base include Elasticsearch::Rails2 end
response = Article.search 'fox dogs' response.took # => 3 response.results.total # => 2 response.results.first._score # => 0.02250402 response.results.first._source.title # => "Quick brown fox"
The returned response object is a rich wrapper around the JSON returned from Elasticsearch, providing access to response metadata and the actual results ("hits").
Each "hit" is wrapped in the Result class, and provides method access to its properties via Hashie::Mash.
The results object supports the Enumerable interface:
response.results.map { |r| r._source.title } # => ["Quick brown fox", "Fast black dogs"] response.results.select { |r| r.title =~ /^Q/ } # => [#{"title"=>"Quick brown fox"}}>]
In fact, the response object will delegate Enumerable methods to results:
response.any? { |r| r.title =~ /fox|dog/ } # => true
To use Array's methods (including any ActiveSupport extensions), just call to_a on the object:
response.to_a.last.title # "Fast black dogs"
For more info check the README in the github repo