Class: Elastic::Index
- Inherits:
-
Object
- Object
- Elastic::Index
- Defined in:
- lib/elastic/index.rb
Class Attribute Summary collapse
-
.alias_name ⇒ Object
Returns the value of attribute alias_name.
-
.client ⇒ Object
Returns the value of attribute client.
-
.mappings ⇒ Object
Returns the value of attribute mappings.
-
.settings ⇒ Object
Returns the value of attribute settings.
Instance Attribute Summary collapse
-
#index_name ⇒ Object
readonly
Returns the value of attribute index_name.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #alias_name ⇒ Object
- #buffer ⇒ Object
- #bulk(action, id, data = {}) ⇒ Object
- #bulk_operation(action, id, data = {}) ⇒ Object
- #count(query = {}) ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
- #dirty? ⇒ Boolean
- #document_ids(query = {}, options = {}) ⇒ Object
- #documents(query = {}, options = {}) ⇒ Object
- #exists? ⇒ Boolean
- #get(id) ⇒ Object
-
#initialize(index_name = nil) ⇒ Index
constructor
A new instance of Index.
- #mget(ids) ⇒ Object
- #promote ⇒ Object
- #promoted? ⇒ Boolean
- #refresh ⇒ Object
Constructor Details
#initialize(index_name = nil) ⇒ Index
Returns a new instance of Index.
25 26 27 |
# File 'lib/elastic/index.rb', line 25 def initialize(index_name = nil) @index_name = (index_name || self.class.generate_index_name).downcase end |
Class Attribute Details
.alias_name ⇒ Object
Returns the value of attribute alias_name.
4 5 6 |
# File 'lib/elastic/index.rb', line 4 def alias_name @alias_name end |
.client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/elastic/index.rb', line 4 def client @client end |
.mappings ⇒ Object
Returns the value of attribute mappings.
4 5 6 |
# File 'lib/elastic/index.rb', line 4 def mappings @mappings end |
.settings ⇒ Object
Returns the value of attribute settings.
4 5 6 |
# File 'lib/elastic/index.rb', line 4 def settings @settings end |
Instance Attribute Details
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
23 24 25 |
# File 'lib/elastic/index.rb', line 23 def index_name @index_name end |
Class Method Details
.generate_index_name ⇒ Object
18 19 20 |
# File 'lib/elastic/index.rb', line 18 def generate_index_name "#{alias_name}-#{Time.now.to_i}".downcase end |
.inherited(klass) ⇒ Object
6 7 8 9 10 |
# File 'lib/elastic/index.rb', line 6 def inherited(klass) klass.alias_name = Elastic::Helpers.to_alias_name(klass) klass.settings = {} klass.mappings = {} end |
.resolve ⇒ Object
12 13 14 15 16 |
# File 'lib/elastic/index.rb', line 12 def resolve if index_name = client.resolve_alias(name: alias_name).first new(index_name) end end |
Instance Method Details
#==(other) ⇒ Object
129 130 131 |
# File 'lib/elastic/index.rb', line 129 def ==(other) self.class == other.class && index_name == other.index_name end |
#alias_name ⇒ Object
125 126 127 |
# File 'lib/elastic/index.rb', line 125 def alias_name self.class.alias_name end |
#buffer ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/elastic/index.rb', line 117 def buffer @buffer ||= Buffer.new do |operations| @dirty = true client.bulk(operations) end end |
#bulk(action, id, data = {}) ⇒ Object
94 95 96 |
# File 'lib/elastic/index.rb', line 94 def bulk(action, id, data = {}) buffer << bulk_operation(action, id, data) end |
#bulk_operation(action, id, data = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/elastic/index.rb', line 98 def bulk_operation(action, id, data = {}) = { _index: index_name, _id: id, retry_on_conflict: 3 } if action.to_sym == :upsert data ||= {} data[:doc_as_upsert] = true action = :update end [:data] = data unless data.empty? { action.to_sym => } end |
#count(query = {}) ⇒ Object
63 64 65 66 |
# File 'lib/elastic/index.rb', line 63 def count(query = {}) response = client.count(index: index_name, body: query) response['count'] end |
#create ⇒ Object
29 30 31 32 33 34 35 36 |
# 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 ⇒ Object
38 39 40 |
# File 'lib/elastic/index.rb', line 38 def delete client.delete_index(index: index_name) end |
#dirty? ⇒ Boolean
59 60 61 |
# File 'lib/elastic/index.rb', line 59 def dirty? !!@dirty end |
#document_ids(query = {}, options = {}) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/elastic/index.rb', line 86 def document_ids(query = {}, = {}) defaults = {} = defaults.merge().merge(stored_fields: ['_id']) docs = documents(query, ) docs.lazy.map { |doc| doc['_id'] } end |
#documents(query = {}, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/elastic/index.rb', line 77 def documents(query = {}, = {}) defaults = { body: query } = defaults.merge() scroll = Scroll.new(client, index_name, ) docs = scroll.each docs.lazy.map { |doc| source_with_id(doc) } end |
#exists? ⇒ Boolean
42 43 44 |
# File 'lib/elastic/index.rb', line 42 def exists? client.index_exists?(index: index_name) end |
#get(id) ⇒ Object
68 69 70 |
# File 'lib/elastic/index.rb', line 68 def get(id) mget([id]).first end |
#mget(ids) ⇒ Object
72 73 74 75 |
# File 'lib/elastic/index.rb', line 72 def mget(ids) docs = client.mget(index_name, ids) docs.map { |doc| source_with_id(doc) } end |
#promote ⇒ Object
46 47 48 |
# File 'lib/elastic/index.rb', line 46 def promote client.alias_index(name: alias_name, index: index_name) end |
#promoted? ⇒ Boolean
50 51 52 |
# File 'lib/elastic/index.rb', line 50 def promoted? client.index_aliased?(name: alias_name, index: index_name) end |
#refresh ⇒ Object
54 55 56 57 |
# File 'lib/elastic/index.rb', line 54 def refresh @dirty = false client.refresh_index(index: index_name) end |