Class: ElasticSearch::Bulk

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_search/bulk.rb

Overview

The ElasticSearch::Bulk class implements the bulk support, ie it collects single requests and emits batches of requests.

Examples:

ElasticSearch::Bulk.new "http://127.0.0.1:9200/index/type/_bulk" do |bulk|
  bulk.create record.id, JSON.generate(MyIndex.serialize(record))
  bulk.index record.id, JSON.generate(MyIndex.serialize(record)), version: record.version, version_type: "external"
  bulk.delete record.id, routing: record.user_id
  bulk.update record.id, JSON.generate(MyIndex.serialize(record))
end

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, count = 1_000, options = {}) {|_self| ... } ⇒ Bulk

Builds and yields a new Bulk object, ie initiates the buffer, yields, sends batches of records each time the buffer is full, and sends a final batch after the yielded code returns and there are still documents present within the buffer.

Examples:

Basic use

ElasticSearch::Bulk.new "http://127.0.0.1:9200/index/type/_bulk" do |bulk|
  # ...
end

Ignore certain errors

ElasticSearch::Bulk.new "http://127.0.0.1:9200/index/type/_bulk", 1_000, ignore_errors: [409] do |bulk|
  # ...
end

Parameters:

  • url (String)

    The endpoint to send bulk requests to

  • count (Fixnum) (defaults to: 1_000)

    The maximum number of documents per bulk request

  • options (Hash) (defaults to: {})

    Options for the bulk requests

Options Hash (options):

  • ignore_errors (Array, Fixnum)

    Errors that should be ignored. If you eg want to ignore errors resulting from conflicts, you can specify to ignore 409 here.

  • raise (Boolean)

    If you want the bulk requests to never raise any exceptions (fire and forget), you can pass false here. Default is true.

Yields:

  • (_self)

Yield Parameters:


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/elastic_search/bulk.rb', line 44

def initialize(url, count = 1_000, options = {})
  self.url = url
  self.count = count
  self.options = options
  self.ignore_errors = Array(options[:ignore_errors]).to_set if options[:ignore_errors]

  init

  yield self

  upload if @num > 0
end

Instance Attribute Details

#countObject

Returns the value of attribute count


17
18
19
# File 'lib/elastic_search/bulk.rb', line 17

def count
  @count
end

#ignore_errorsObject

Returns the value of attribute ignore_errors


17
18
19
# File 'lib/elastic_search/bulk.rb', line 17

def ignore_errors
  @ignore_errors
end

#optionsObject

Returns the value of attribute options


17
18
19
# File 'lib/elastic_search/bulk.rb', line 17

def options
  @options
end

#urlObject

Returns the value of attribute url


17
18
19
# File 'lib/elastic_search/bulk.rb', line 17

def url
  @url
end

Instance Method Details

#create(id, json, options = {}) ⇒ Object

Adds a create request to the bulk batch.

Parameters:

  • id (Fixnum, String)

    The document/record id

  • json (String)

    The json document

  • options (options) (defaults to: {})

    Options for the index request, like eg routing and versioning


83
84
85
# File 'lib/elastic_search/bulk.rb', line 83

def create(id, json, options = {})
  perform :create, id, json, options
end

#delete(id, options = {}) ⇒ Object

Adds a delete request to the bulk batch.

Parameters:

  • id (Fixnum, String)

    The document/record id

  • options (options) (defaults to: {})

    Options for the index request, like eg routing and versioning


104
105
106
# File 'lib/elastic_search/bulk.rb', line 104

def delete(id, options = {})
  perform :delete, id, nil, options
end

#import(*args) ⇒ Object

Adds an index request to the bulk batch

See Also:


72
73
74
# File 'lib/elastic_search/bulk.rb', line 72

def import(*args)
  index(*args)
end

#index(id, json, options = {}) ⇒ Object

Adds an index request to the bulk batch.

Parameters:

  • id (Fixnum, String)

    The document/record id

  • json (String)

    The json document

  • options (options) (defaults to: {})

    Options for the index request, like eg routing and versioning


64
65
66
# File 'lib/elastic_search/bulk.rb', line 64

def index(id, json, options = {})
  perform :index, id, json, options
end

#update(id, json, options = {}) ⇒ Object

Adds a update request to the bulk batch.

Parameters:

  • id (Fixnum, String)

    The document/record id

  • json (String)

    The json document

  • options (options) (defaults to: {})

    Options for the index request, like eg routing and versioning


94
95
96
# File 'lib/elastic_search/bulk.rb', line 94

def update(id, json, options = {})
  perform :update, id, json, options
end