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

#_destroy_document(opts = {}) ⇒ Object

Internal reference for destroy_document. Override this for alternate implementations of removing the document.



226
227
228
# File 'lib/sequel/plugins/elasticsearch.rb', line 226

def _destroy_document(opts = {})
  destroy_document(opts)
end

#_index_document(opts = {}) ⇒ Object

Internal reference for index_document. Override this for alternate implementations of indexing the document.



213
214
215
# File 'lib/sequel/plugins/elasticsearch.rb', line 213

def _index_document(opts = {})
  index_document(opts)
end

#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.



183
184
185
186
# File 'lib/sequel/plugins/elasticsearch.rb', line 183

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.



190
191
192
193
# File 'lib/sequel/plugins/elasticsearch.rb', line 190

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.



197
198
199
200
# File 'lib/sequel/plugins/elasticsearch.rb', line 197

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

#as_indexed_jsonObject



207
208
209
# File 'lib/sequel/plugins/elasticsearch.rb', line 207

def as_indexed_json
  indexed_values
end

#destroy_document(opts = {}) ⇒ Object

Remove the document from the Elasticsearch cluster.



231
232
233
# File 'lib/sequel/plugins/elasticsearch.rb', line 231

def destroy_document(opts = {})
  es_client.delete document_path(opts)
end

#document_idObject

Determine the ID to be used for the document in the Elasticsearch cluster. It will join the values of a multi field primary key with an underscore.



246
247
248
249
250
# File 'lib/sequel/plugins/elasticsearch.rb', line 246

def document_id
  doc_id = pk
  doc_id = doc_id.join('_') if doc_id.is_a? Array
  doc_id
end

#document_path(opts = {}) ⇒ Object

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



236
237
238
239
240
241
242
# File 'lib/sequel/plugins/elasticsearch.rb', line 236

def document_path(opts = {})
  {
    index: opts.delete(:index) || elasticsearch_index,
    type: opts.delete(:type) || elasticsearch_type,
    id: opts.delete(:id) || document_id
  }
end

#elasticsearch_indexObject



173
174
175
# File 'lib/sequel/plugins/elasticsearch.rb', line 173

def elasticsearch_index
  self.class.elasticsearch_index
end

#elasticsearch_typeObject



177
178
179
# File 'lib/sequel/plugins/elasticsearch.rb', line 177

def elasticsearch_type
  self.class.elasticsearch_type
end

#es_clientObject

Return the Elasticsearch client used to communicate with the cluster.



203
204
205
# File 'lib/sequel/plugins/elasticsearch.rb', line 203

def es_client
  self.class.es_client
end

#index_document(opts = {}) ⇒ Object

Create or update the document on the Elasticsearch cluster.



218
219
220
221
222
# File 'lib/sequel/plugins/elasticsearch.rb', line 218

def index_document(opts = {})
  params = document_path(opts)
  params[:body] = indexed_values
  es_client.index params
end