Module: Sequel::Plugins::Elasticsearch::InstanceMethods

Defined in:
lib/sequel/plugins/elasticsearch.rb

Overview

The instance methods that will be added to the Sequel::Model

Instance Method Summary collapse

Instance Method Details

#after_createObject

Sequel::Model after_create hook to add the new record to the Elasticsearch index. It’s “safe” in that it won’t raise an error if it fails.



90
91
92
93
# File 'lib/sequel/plugins/elasticsearch.rb', line 90

def after_create
  super
  self.class.call_es { index_document }
end

#after_destroyObject

Sequel::Model after_destroy hook to remove the record from the Elasticsearch index. It’s “safe” in that it won’t raise an error if it fails.



97
98
99
100
# File 'lib/sequel/plugins/elasticsearch.rb', line 97

def after_destroy
  super
  self.class.call_es { destroy_document }
end

#after_updateObject

Sequel::Model after_update hook to update the record in the Elasticsearch index. It’s “safe” in that it won’t raise an error if it fails.



104
105
106
107
# File 'lib/sequel/plugins/elasticsearch.rb', line 104

def after_update
  super
  self.class.call_es { index_document }
end

#as_indexed_jsonObject



114
115
116
# File 'lib/sequel/plugins/elasticsearch.rb', line 114

def as_indexed_json
  indexed_values
end

#destroy_documentObject

Remove the document from the Elasticsearch cluster.



126
127
128
# File 'lib/sequel/plugins/elasticsearch.rb', line 126

def destroy_document
  es_client.delete document_path
end

#document_pathObject

Determine the complete path to a document (/index/type/id) in the Elasticsearch cluster.



131
132
133
134
135
136
137
# File 'lib/sequel/plugins/elasticsearch.rb', line 131

def document_path
  {
    index: self.class.elasticsearch_index,
    type: self.class.elasticsearch_type,
    id: document_id
  }
end

#es_clientObject

Return the Elasticsearch client used to communicate with the cluster.



110
111
112
# File 'lib/sequel/plugins/elasticsearch.rb', line 110

def es_client
  self.class.es_client
end

#index_documentObject

Create or update the document on the Elasticsearch cluster.



119
120
121
122
123
# File 'lib/sequel/plugins/elasticsearch.rb', line 119

def index_document
  params = document_path
  params[:body] = indexed_values
  es_client.index params
end