model.asciidoc 2.0 KB

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