class Elastic::Index
Attributes
alias_name[RW]
client[RW]
mappings[RW]
settings[RW]
index_name[R]
Public Class Methods
generate_index_name()
click to toggle source
# File lib/elastic/index.rb, line 18 def generate_index_name "#{alias_name}-#{Time.now.to_i}".downcase end
inherited(klass)
click to toggle source
# File lib/elastic/index.rb, line 6 def inherited(klass) klass.alias_name = Elastic::Helpers.to_alias_name(klass) klass.settings = {} klass.mappings = {} end
new(index_name = nil)
click to toggle source
# File lib/elastic/index.rb, line 25 def initialize(index_name = nil) @index_name = (index_name || self.class.generate_index_name).downcase end
resolve()
click to toggle source
# File lib/elastic/index.rb, line 12 def resolve if index_name = client.resolve_alias(name: alias_name).first new(index_name) end end
Public Instance Methods
==(other)
click to toggle source
# File lib/elastic/index.rb, line 131 def ==(other) self.class == other.class && index_name == other.index_name end
alias_name()
click to toggle source
# File lib/elastic/index.rb, line 127 def alias_name self.class.alias_name end
buffer()
click to toggle source
# File lib/elastic/index.rb, line 119 def buffer @buffer ||= Buffer.new do |operations| @dirty = true client.bulk(operations) end end
bulk(action, id, data: {}, query_params: {})
click to toggle source
# File lib/elastic/index.rb, line 94 def bulk(action, id, data: {}, query_params: {}) buffer << bulk_operation(action, id, data, query_params) end
bulk_operation(action, id, data = {}, query_params = {})
click to toggle source
# File lib/elastic/index.rb, line 98 def bulk_operation(action, id, data = {}, query_params = {}) metadata = { _index: index_name, _id: id, retry_on_conflict: 3, } metadata.merge!(query_params) unless query_params.empty? if action.to_sym == :upsert data ||= {} data[:doc_as_upsert] = true action = :update end metadata[:data] = data unless data.empty? { action.to_sym => metadata } end
count(query = {})
click to toggle source
# File lib/elastic/index.rb, line 63 def count(query = {}) response = client.count(index: index_name, body: query) response['count'] end
create()
click to toggle source
# File lib/elastic/index.rb, line 29 def create body = { settings: self.class.settings, mappings: self.class.mappings, } client.create_index(index: index_name, body: body) end
delete()
click to toggle source
# File lib/elastic/index.rb, line 38 def delete client.delete_index(index: index_name) end
dirty?()
click to toggle source
# File lib/elastic/index.rb, line 59 def dirty? !!@dirty end
document_ids(query = {}, options = {})
click to toggle source
# File lib/elastic/index.rb, line 86 def document_ids(query = {}, options = {}) defaults = {} scroll_options = defaults.merge(options).merge(stored_fields: ['_id']) docs = documents(query, scroll_options) docs.lazy.map { |doc| doc['_id'] } end
documents(query = {}, options = {})
click to toggle source
# File lib/elastic/index.rb, line 77 def documents(query = {}, options = {}) defaults = { body: query } scroll_options = defaults.merge(options) scroll = Scroll.new(client, index_name, scroll_options) docs = scroll.each docs.lazy.map { |doc| source_with_id(doc) } end
exists?()
click to toggle source
# File lib/elastic/index.rb, line 42 def exists? client.index_exists?(index: index_name) end
get(id, query_params: {})
click to toggle source
# File lib/elastic/index.rb, line 68 def get(id, query_params: {}) mget([id], query_params: query_params).first end
mget(ids, query_params: {})
click to toggle source
# File lib/elastic/index.rb, line 72 def mget(ids, query_params: {}) docs = client.mget(index_name, ids, query_params) docs.map { |doc| source_with_id(doc) } end
promote()
click to toggle source
# File lib/elastic/index.rb, line 46 def promote client.alias_index(name: alias_name, index: index_name) end
promoted?()
click to toggle source
# File lib/elastic/index.rb, line 50 def promoted? client.index_aliased?(name: alias_name, index: index_name) end
refresh()
click to toggle source
# File lib/elastic/index.rb, line 54 def refresh @dirty = false client.refresh_index(index: index_name) end
Private Instance Methods
client()
click to toggle source
# File lib/elastic/index.rb, line 141 def client self.class.client end
source_with_id(doc)
click to toggle source
# File lib/elastic/index.rb, line 137 def source_with_id(doc) (doc['_source'] || {}).merge('_id' => doc['_id']) end