1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
[[activemodel_activerecord]]
=== ActiveModel / ActiveRecord
The `elasticsearch-model` http://rubygems.org/gems/elasticsearch-model[Rubygem]
provides integration with Ruby domain objects ("models"), commonly found for
example, in Ruby on Rails applications.
It uses the `elasticsearch` Rubygem as the client communicating with the {es}
cluster.
[discrete]
==== Features
* ActiveModel integration with adapters for ActiveRecord and Mongoid
* 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
* Support for Kaminari and WillPaginate pagination
* Extension implemented via proxy object to shield model namespace from collisions
* Convenience methods for (re)creating the index, setting up mappings, indexing documents, ...
[discrete]
==== Usage
Add the library to your Gemfile:
[source,ruby]
------------------------------------
gem 'elasticsearch-rails'
------------------------------------
Include the extension module in your model class:
[source,ruby]
------------------------------------
class Article < ActiveRecord::Base
include Elasticsearch::Model
end
------------------------------------
Import some data and perform a search:
[source,ruby]
------------------------------------
Article.import
response = Article.search 'fox dog'
response.took
# => 3
------------------------------------
It is possible to either return results as model instances, or decorated
documents from {es}, with the `records` and `results` methods, respectively:
[source,ruby]
------------------------------------
response.records.first
# Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE ...
=> #<Article id: 3, title: "Foo " ...>
response.results.first._score
# => 0.02250402
response.results.first._source.title
# => "Quick brown fox"
------------------------------------
Consult the
https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model[documentation]
for more information.
|