module Sequel::Plugins::Elasticsearch::InstanceMethods
The instance methods that will be added to the Sequel::Model
Public Instance Methods
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
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
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.
# File lib/sequel/plugins/elasticsearch.rb, line 167 def after_create super self.class.call_es { _index_document } end
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.
# File lib/sequel/plugins/elasticsearch.rb, line 174 def after_destroy super self.class.call_es { _destroy_document } end
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.
# File lib/sequel/plugins/elasticsearch.rb, line 181 def after_update super self.class.call_es { _index_document } end
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
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
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
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
# File lib/sequel/plugins/elasticsearch.rb, line 157 def elasticsearch_index self.class.elasticsearch_index end
# File lib/sequel/plugins/elasticsearch.rb, line 161 def elasticsearch_type self.class.elasticsearch_type end
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
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
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