Class: Elasticsearch::Helpers::BulkHelper
- Inherits:
-
Object
- Object
- Elasticsearch::Helpers::BulkHelper
- Defined in:
- lib/elasticsearch/helpers/bulk_helper.rb
Overview
Elasticsearch Client Helper for the Bulk API
Instance Attribute Summary collapse
-
#index ⇒ Object
Returns the value of attribute index.
Instance Method Summary collapse
-
#delete(ids, params = {}, body = {}) ⇒ Object
Delete documents using the Bulk API.
-
#ingest(docs, params = {}, body = {}, &block) {|response, ingest_docs| ... } ⇒ Object
Index documents using the Bulk API.
-
#ingest_json(file, params = {}) {|response, ingest_docs| ... } ⇒ Object
Ingest data directly from a JSON file.
-
#initialize(client, index, params = {}) ⇒ BulkHelper
constructor
Create a BulkHelper.
-
#update(docs, params = {}, body = {}, &block) {|response, ingest_docs| ... } ⇒ Object
Update documents using the Bulk API.
Constructor Details
#initialize(client, index, params = {}) ⇒ BulkHelper
Create a BulkHelper
33 34 35 36 37 |
# File 'lib/elasticsearch/helpers/bulk_helper.rb', line 33 def initialize(client, index, params = {}) @client = client @index = index @params = params end |
Instance Attribute Details
#index ⇒ Object
Returns the value of attribute index.
25 26 27 |
# File 'lib/elasticsearch/helpers/bulk_helper.rb', line 25 def index @index end |
Instance Method Details
#delete(ids, params = {}, body = {}) ⇒ Object
Delete documents using the Bulk API
64 65 66 67 |
# File 'lib/elasticsearch/helpers/bulk_helper.rb', line 64 def delete(ids, params = {}, body = {}) delete_docs = ids.map { |id| { delete: { _index: @index, _id: id} } } @client.bulk({ body: delete_docs }.merge(params.merge(@params))) end |
#ingest(docs, params = {}, body = {}, &block) {|response, ingest_docs| ... } ⇒ Object
Index documents using the Bulk API.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/elasticsearch/helpers/bulk_helper.rb', line 48 def ingest(docs, params = {}, body = {}, &block) ingest_docs = docs.map { |doc| { index: { _index: @index, data: doc} } } if (slice = params.delete(:slice)) ingest_docs.each_slice(slice) do |items| ingest(items.map { |item| item[:index][:data] }, params, &block) end else bulk_request(ingest_docs, params, &block) end end |
#ingest_json(file, params = {}) {|response, ingest_docs| ... } ⇒ Object
Ingest data directly from a JSON file
110 111 112 113 114 115 116 117 118 |
# File 'lib/elasticsearch/helpers/bulk_helper.rb', line 110 def ingest_json(file, params = {}, &block) data = JSON.parse(File.read(file)) if (keys = params.delete(:keys)) keys = keys.split(',') if keys.is_a?(String) data = data.dig(*keys) end ingest(data, params, &block) end |
#update(docs, params = {}, body = {}, &block) {|response, ingest_docs| ... } ⇒ Object
Update documents using the Bulk API
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/elasticsearch/helpers/bulk_helper.rb', line 78 def update(docs, params = {}, body = {}, &block) ingest_docs = docs.map do |doc| { update: { _index: @index, _id: doc.delete('id'), data: { doc: doc } } } end if (slice = params.delete(:slice)) ingest_docs.each_slice(slice) { |items| update(items, params, &block) } else bulk_request(ingest_docs, params, &block) end end |