module Elasticsearch::Persistence

Persistence for Ruby domain objects and models in Elasticsearch

‘Elasticsearch::Persistence` contains modules for storing and retrieving Ruby domain objects and models in Elasticsearch.

Repository

The repository patterns allows to store and retrieve Ruby objects in Elasticsearch.

require 'elasticsearch/persistence'

class Note
  def to_hash; {foo: 'bar'}; end
end

repository = Elasticsearch::Persistence::Repository.new

repository.save Note.new
# => {"_index"=>"repository", "_type"=>"note", "_id"=>"mY108X9mSHajxIy2rzH2CA", ...}

Customize your repository by including the main module in a Ruby class

class MyRepository
  include Elasticsearch::Persistence::Repository

  index 'my_notes'
  klass Note

  client Elasticsearch::Client.new log: true
end

repository = MyRepository.new

repository.save Note.new
# 2014-04-04 22:15:25 +0200: POST http://localhost:9200/my_notes/note [status:201, request:0.009s, query:n/a]
# 2014-04-04 22:15:25 +0200: > {"foo":"bar"}
# 2014-04-04 22:15:25 +0200: < {"_index":"my_notes","_type":"note","_id":"-d28yXLFSlusnTxb13WIZQ", ...}

Model

The active record pattern allows to use the interface familiar from ActiveRecord models:

require 'elasticsearch/persistence'

class Article
  attribute :title, String, mapping: { analyzer: 'snowball' }
end

article = Article.new id: 1, title: 'Test'
article.save

Article.find(1)

article.update_attributes title: 'Update'

article.destroy

Constants

VERSION

Public Class Methods

eager_load!() click to toggle source
Calls superclass method
# File lib/elasticsearch/persistence.rb, line 148
def self.eager_load!
  super
  Elasticsearch::Persistence::Scoping.eager_load!
end