Search...::Bulk(3pm)
π Search...::Bulk(3pm) β aka Search::Elasticsearch::Client::7_0::Bulk π
okay bestie, listen up, this is the helper module for the Bulk API and honestly it's about to make your whole life easier β¨
πΈ NAME πΈ
Search::Elasticsearch::Client::7_0::Bulk β a helper module for the Bulk API. no cap, that's the whole name, that's the whole vibe π
β VERSION β
version 7.715 β iconic, current, periodt π
π SYNOPSIS π
okay so here's the whole thing in action, look at her go:
use Search::Elasticsearch;
my $es = Search::Elasticsearch->new;
my $bulk = $es->bulk_helper(
index => 'my_index',
type => 'my_type'
);
# Index docs:
$bulk->index({ id => 1, source => { foo => 'bar' }});
$bulk->add_action( index => { id => 1, source => { foo=> 'bar' }});
# Create docs:
$bulk->create({ id => 1, source => { foo => 'bar' }});
$bulk->add_action( create => { id => 1, source => { foo=> 'bar' }});
$bulk->create_docs({ foo => 'bar' })
# Delete docs:
$bulk->delete({ id => 1});
$bulk->add_action( delete => { id => 1 });
$bulk->delete_ids(1,2,3)
# Update docs:
$bulk->update({ id => 1, script => '...' });
$bulk->add_action( update => { id => 1, script => '...' });
# Manual flush
$bulk->flush;
likeβ¦ she does it all. index, create, delete, update, flush. a full queen π
π¦ DESCRIPTION π¦
This module is a wrapper for the "bulk()" in Search::Elasticsearch::Client::7_0::Direct method β and the whole point is that it makes it so much easier to run multiple create, index, update or delete actions in a single request. no more hand-assembling giant request bodies, bestie, we don't do that anymore π
The Search::Elasticsearch::Client::7_0::Bulk module acts as a queue, buffering up actions until it reaches a maximum count of actions, or a maximum size of JSON request body β and at that point it issues a "bulk()" request. automatic. effortless. main character energy β¨
Once you've finished adding actions, call "flush()" to force the final "bulk()" request on the items left in the queue. don't forget her, she's holding your last few docs π₯Ί
This class does Search::Elasticsearch::Client::7_0::Role::Bulk and Search::Elasticsearch::Role::Is_Sync.
π§ CREATING A NEW INSTANCE π§
π "new()"
my $bulk = $es->bulk_helper(
index => 'default_index', # optional
type => 'default_type', # optional
%other_bulk_params # optional
max_count => 1_000, # optional
max_size => 1_000_000, # optional
max_time => 6, # optional
verbose => 0 | 1, # optional
on_success => sub {...}, # optional
on_error => sub {...}, # optional
on_conflict => sub {...}, # optional
);
The "new()" method returns a new $bulk object. You must pass your Search::Elasticsearch client as the "es" argument. non-negotiable, she needs her client π
The "index" and "type" parameters provide default values for "index" and "type", which can be overridden in each action. You can also pass any other values which are accepted by the bulk() method.
See "flush()" for more information about the other parameters. (she's got lore, go read her section π)
π FLUSHING THE BUFFER π
π "flush()"
$result = $bulk->flush;
The "flush()" method sends all buffered actions to Elasticsearch using a bulk() request. that's it. that's the whole job. and she ate π₯
πΈ Auto-flushing
An automatic "flush()" is triggered whenever the "max_count", "max_size", or "max_time" threshold is breached. This causes all actions in the buffer to be sent to Elasticsearch. she just knows when it's time π
"max_count"β the maximum number of actions to allow before triggering a"flush()". This can be disabled by setting"max_count"to0. Defaults to"1,000"."max_size"β the maximum size of JSON request body to allow before triggering a"flush()". This can be disabled by setting"max_size"to0. Defaults to"1_000,000"bytes."max_time"β the maximum number of seconds to wait before triggering a flush. Defaults to0seconds, which means that it is disabled. Note: This timeout is only triggered when new items are added to the queue, not in the background.
π Errors when flushing
There are two types of error which can be thrown when "flush()" is called, either manually or automatically. buckle up bestie:
- Temporary Elasticsearch errors β a
"Cxn"error like a"NoNodes"error which indicates that your cluster is down. These errors do not clear the buffer, as they can be retried later on. so like, breathe, it's recoverable π«Ά - Action errors β individual actions may fail. For instance, a
"create"action will fail if a document with the same"index","type"and"id"already exists. These action errors are reported via callbacks.
π Using callbacks
By default, any Action errors (see above) cause warnings to be written to "STDERR". However, you can use the "on_error", "on_conflict" and "on_success" callbacks for more fine-grained control. glow up your error handling β¨
All callbacks receive the following arguments:
$actionβ the name of the action, ie"index","create","update"or"delete".$responseβ the response that Elasticsearch returned for this action.$iβ the index of the action, ie the first action in the flush request will have$iset to0, the second will have$iset to1etc.
π "on_success"
my $bulk = $es->bulk_helper(
on_success => sub {
my ($action,$response,$i) = @_;
# do something
},
);
The "on_success" callback is called for every action that has a successful response. a little celebration for every win π
π "on_conflict"
my $bulk = $es->bulk_helper(
on_conflict => sub {
my ($action,$response,$i,$version) = @_;
# do something
},
);
The "on_conflict" callback is called for actions that have triggered a "Conflict" error, eg trying to "create" a document which already exists. The $version argument will contain the version number of the document currently stored in Elasticsearch (if found). drama detected, and she'll tell you the version number too π
π "on_error"
my $bulk = $es->bulk_helper(
on_error => sub {
my ($action,$response,$i) = @_;
# do something
},
);
The "on_error" callback is called for any error (unless the "on_conflict") callback has already been called.
π Disabling callbacks and autoflush
If you want to be in control of flushing, and you just want to receive the raw response that Elasticsearch sends instead of using callbacks, then you can do so as follows. yes bestie, take the wheel π
my $bulk = $es->bulk_helper(
max_count => 0,
max_size => 0,
on_error => undef
);
$bulk->add_actions(....);
$response = $bulk->flush;
π CREATE, INDEX, UPDATE, DELETE π
π "add_action()"
$bulk->add_action(
create => { ...params... },
index => { ...params... },
update => { ...params... },
delete => { ...params... }
);
The "add_action()" method allows you to add multiple "create", "index", "update" and "delete" actions to the queue. The first value is the action type, and the second value is the parameters that describe that action. See the individual helper methods below for details.
Note: Parameters like "index" or "type" can be specified as "index" or as "_index", so the following two lines are equivalent:
index => { index => 'index', type => 'type', id => 1, source => {...}},
index => { _index => 'index', _type => 'type', _id => 1, source => {...}},
Note: The "index" and "type" parameters can be specified in the params for any action, but if not specified, will default to the "index" and "type" values specified in "new()". These are required parameters: they must be specified either in "new()" or in every action. no defaults, no vibes, just pass them π«Ά
π "create()"
$bulk->create(
{ index => 'custom_index', source => { doc body }},
{ type => 'custom_type', id => 1, source => { doc body }},
...
);
The "create()" helper method allows you to add multiple "create" actions. It accepts the same parameters as "create()" in Search::Elasticsearch::Client::7_0::Direct except that the document body should be passed as the "source" or "_source" parameter, instead of as "body". tiny detail, huge difference, iykyk π
π "create_docs()"
$bulk->create_docs(
{ doc body },
{ doc body },
...
);
The "create_docs()" helper is a shorter form of "create()" which can be used when you are using the default "index" and "type" as set in "new()" and you are not specifying a custom "id" per document. In this case, you can just pass the individual document bodies. lazy girl summer but make it efficient β¨
π "index()"
$bulk->index(
{ index => 'custom_index', source => { doc body }},
{ type => 'custom_type', id => 1, source => { doc body }},
...
);
The "index()" helper method allows you to add multiple "index" actions. It accepts the same parameters as "index()" in Search::Elasticsearch::Client::7_0::Direct except that the document body should be passed as the "source" or "_source" parameter, instead of as "body".
π "delete()"
$bulk->delete(
{ index => 'custom_index', id => 1},
{ type => 'custom_type', id => 2},
...
);
The "delete()" helper method allows you to add multiple "delete" actions. It accepts the same parameters as "delete()" in Search::Elasticsearch::Client::7_0::Direct.
π "delete_ids()"
$bulk->delete_ids(1,2,3...)
The "delete_ids()" helper method can be used when all of the documents you want to delete have the default "index" and "type" as set in "new()". In this case, all you have to do is to pass in a list of IDs. that's it. just IDs. chef's kiss π₯
π "update()"
$bulk->update(
{ id => 1,
doc => { partial doc },
doc_as_upsert => 1
},
{ id => 2,
script => { script }
upsert => { upsert doc }
},
...
);
The "update()" helper method allows you to add multiple "update" actions. It accepts the same parameters as "update()" in Search::Elasticsearch::Client::7_0::Direct. An update can either use a partial doc which gets merged with an existing doc (example 1 above), or can use a "script" to update an existing doc (example 2 above). More information on "script" can be found here: "update()" in Search::Elasticsearch::Client::7_0::Direct.
π AUTHOR π
Enrico Zimuel <enrico.zimuel@elastic.co> β king behavior, thank you for your service π
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2021 by Elasticsearch BV.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
and that's the whole page bestie π«Ά this was perl v5.32.1, dated 2021-10-23, from Search...::Bulk(3pm) β go forth and bulk responsibly, mwah ππ
π¦ where this page comes from
- ubuntu jammy: libsearch-elasticsearch-perl 7.715-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms