model.asciidoc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. [[activemodel_activerecord]]
  2. == ActiveModel / ActiveRecord
  3. The `elasticsearch-model` http://rubygems.org/gems/elasticsearch-model[Rubygem]
  4. provides integration with Ruby domain objects ("models"), commonly found e.g. in Ruby on Rails applications.
  5. It uses the `elasticsearch` Rubygem as the client communicating with the Elasticsearch cluster.
  6. === Features at a Glance
  7. * ActiveModel integration with adapters for ActiveRecord and Mongoid
  8. * Enumerable-based wrapper for search results
  9. * ActiveRecord::Relation-based wrapper for returning search results as records
  10. * Convenience model methods such as `search`, `mapping`, `import`, etc
  11. * Support for Kaminari and WillPaginate pagination
  12. * Extension implemented via proxy object to shield model namespace from collisions
  13. * Convenience methods for (re)creating the index, setting up mappings, indexing documents, ...
  14. === Usage
  15. Add the library to your Gemfile:
  16. [source,ruby]
  17. ------------------------------------
  18. gem 'elasticsearch-rails'
  19. ------------------------------------
  20. Include the extension module in your model class:
  21. [source,ruby]
  22. ------------------------------------
  23. class Article < ActiveRecord::Base
  24. include Elasticsearch::Model
  25. end
  26. ------------------------------------
  27. Import some data and perform a search:
  28. [source,ruby]
  29. ------------------------------------
  30. Article.import
  31. response = Article.search 'fox dog'
  32. response.took
  33. # => 3
  34. ------------------------------------
  35. It is possible to either return results as model instances, or decorated documents from Elasticsearch,
  36. with the `records` and `results` methods, respectively:
  37. [source,ruby]
  38. ------------------------------------
  39. response.records.first
  40. # Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE ...
  41. => #<Article id: 3, title: "Foo " ...>
  42. response.results.first._score
  43. # => 0.02250402
  44. response.results.first._source.title
  45. # => "Quick brown fox"
  46. ------------------------------------
  47. Please see the full https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model[documentation]
  48. for more information.