Class: Metacrunch::Elasticsearch::Writer
- Inherits:
-
Object
- Object
- Metacrunch::Elasticsearch::Writer
- Defined in:
- lib/metacrunch/elasticsearch/writer.rb
Instance Method Summary collapse
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(uri, log: false, bulk_size: 250, autoflush: true) ⇒ Writer
constructor
A new instance of Writer.
- #write(data, options = {}) ⇒ Object
Constructor Details
#initialize(uri, log: false, bulk_size: 250, autoflush: true) ⇒ Writer
Returns a new instance of Writer.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/metacrunch/elasticsearch/writer.rb', line 8 def initialize(uri, log: false, bulk_size: 250, autoflush: true) unless uri.starts_with?("elasticsearch://") raise ArgumentError, "URI must be an elasticsearch URI (elasticsearch://...)" end @uri = URI(uri) @log = log @bulk_size = bulk_size @buffer = [] @autoflush = autoflush end |
Instance Method Details
#close ⇒ Object
47 48 49 |
# File 'lib/metacrunch/elasticsearch/writer.rb', line 47 def close flush end |
#flush ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/metacrunch/elasticsearch/writer.rb', line 36 def flush if @buffer.length > 0 result = client.bulk(body: @buffer.inject([]){ |_body, _data| _body << { index: _data } }) raise RuntimeError if result["errors"] end true ensure @buffer = [] end |
#write(data, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/metacrunch/elasticsearch/writer.rb', line 20 def write(data, = {}) id = data.delete(:id) || data.delete(:_id) raise ArgumentError, "Missing id. You must provide 'id' or '_id' as part of the data" unless id @buffer << { _index: @uri.index, _type: @uri.type, _id: id, data: data } flush if @autoflush && @bulk_size > 0 && @buffer.length >= @bulk_size true end |