module Sequel::Plugins::Elasticsearch::InstanceMethods

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

Public Instance Methods

_destroy_document(opts = {}) click to toggle source

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

# File lib/sequel/plugins/elasticsearch.rb, line 212
def _destroy_document(opts = {})
  destroy_document(opts)
end
_index_document(opts = {}) click to toggle source

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

# File lib/sequel/plugins/elasticsearch.rb, line 199
def _index_document(opts = {})
  index_document(opts)
end
after_create() click to toggle source

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.

Calls superclass method
# File lib/sequel/plugins/elasticsearch.rb, line 167
def after_create
  super
  self.class.call_es { _index_document }
end
after_destroy() click to toggle source

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.

Calls superclass method
# File lib/sequel/plugins/elasticsearch.rb, line 174
def after_destroy
  super
  self.class.call_es { _destroy_document }
end
after_update() click to toggle source

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.

Calls superclass method
# File lib/sequel/plugins/elasticsearch.rb, line 181
def after_update
  super
  self.class.call_es { _index_document }
end
as_indexed_json() click to toggle source

Mirror the Elasticsearch Rails plugin. Use this to override what data is sent to Elasticsearch

# File lib/sequel/plugins/elasticsearch.rb, line 193
def as_indexed_json
  indexed_values
end
destroy_document(opts = {}) click to toggle source

Remove the document from the Elasticsearch cluster.

# File lib/sequel/plugins/elasticsearch.rb, line 217
def destroy_document(opts = {})
  es_client.delete document_path(opts)
end
document_id() click to toggle source

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.

# File lib/sequel/plugins/elasticsearch.rb, line 232
def document_id
  doc_id = pk
  doc_id = doc_id.join('_') if doc_id.is_a? Array
  doc_id
end
document_path(opts = {}) click to toggle source

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

# File lib/sequel/plugins/elasticsearch.rb, line 222
def document_path(opts = {})
  {
    index: opts.delete(:index) || elasticsearch_index,
    type: opts.delete(:type) || elasticsearch_type,
    id: opts.delete(:id) || document_id
  }
end
elasticsearch_index() click to toggle source
# File lib/sequel/plugins/elasticsearch.rb, line 157
def elasticsearch_index
  self.class.elasticsearch_index
end
elasticsearch_type() click to toggle source
# File lib/sequel/plugins/elasticsearch.rb, line 161
def elasticsearch_type
  self.class.elasticsearch_type
end
es_client() click to toggle source

Return the Elasticsearch client used to communicate with the cluster.

# File lib/sequel/plugins/elasticsearch.rb, line 187
def es_client
  self.class.es_client
end
index_document(opts = {}) click to toggle source

Create or update the document on the Elasticsearch cluster.

# File lib/sequel/plugins/elasticsearch.rb, line 204
def index_document(opts = {})
  params = document_path(opts)
  params[:body] = as_indexed_json
  es_client.index params
end

Private Instance Methods

indexed_values() click to toggle source

Values to be indexed

# File lib/sequel/plugins/elasticsearch.rb, line 241
def indexed_values
  # TODO: Deprecate this method in favour of as_indexed_json
  values.each_key { |k| values[k] = values[k].strftime('%FT%T%:z') if values[k].is_a?(Time) }
end