1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
package Search::Elasticsearch::Client::8_0::Bulk;
$Search::Elasticsearch::Client::8_0::Bulk::VERSION = '8.12';
use Moo;
with 'Search::Elasticsearch::Client::8_0::Role::Bulk',
'Search::Elasticsearch::Role::Is_Sync';
use Search::Elasticsearch::Util qw(parse_params throw);
use Try::Tiny;
use namespace::clean;
#===================================
sub add_action {
#===================================
my $self = shift;
my $buffer = $self->_buffer;
my $max_size = $self->max_size;
my $max_count = $self->max_count;
my $max_time = $self->max_time;
while (@_) {
my @json = $self->_encode_action( splice( @_, 0, 2 ) );
push @$buffer, @json;
my $size = $self->_buffer_size;
$size += length($_) + 1 for @json;
$self->_buffer_size($size);
my $count = $self->_buffer_count( $self->_buffer_count + 1 );
$self->flush
if ( $max_size and $size >= $max_size )
|| ( $max_count and $count >= $max_count )
|| ( $max_time and time >= $self->_last_flush + $max_time );
}
return 1;
}
#===================================
sub flush {
#===================================
my $self = shift;
$self->_last_flush(time);
return { items => [] }
unless $self->_buffer_size;
if ( $self->verbose ) {
local $| = 1;
print ".";
}
my $buffer = $self->_buffer;
my $results = try {
my $res = $self->es->bulk( %{ $self->_bulk_args }, body => $buffer );
$self->clear_buffer;
return $res;
}
catch {
my $error = $_;
$self->clear_buffer
unless $error->is( 'Cxn', 'NoNodes' );
die $error;
};
$self->_report( $buffer, $results );
return defined wantarray ? $results : undef;
}
1;
=pod
=encoding UTF-8
=head1 NAME
Search::Elasticsearch::Client::8_0::Bulk - A helper module for the Bulk API
=head1 VERSION
version 8.12
=head1 SYNOPSIS
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;
=head1 DESCRIPTION
This module provides a wrapper for the L<Search::Elasticsearch::Client::8_0::Direct/bulk()>
method which makes it easier to run multiple create, index, update or delete
actions in a single request.
The L<Search::Elasticsearch::Client::8_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, at which point it issues a C<bulk()> request.
Once you have finished adding actions, call L</flush()> to force the final
C<bulk()> request on the items left in the queue.
This class does L<Search::Elasticsearch::Client::8_0::Role::Bulk> and
L<Search::Elasticsearch::Role::Is_Sync>.
=head1 CREATING A NEW INSTANCE
=head2 C<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 C<new()> method returns a new C<$bulk> object. You must pass your
Search::Elasticsearch client as the C<es> argument.
The C<index> and C<type> parameters provide default values for
C<index> and C<type>, which can be overridden in each action.
You can also pass any other values which are accepted
by the L<bulk()|Search::Elasticsearch::Client::8_0::Direct/bulk()> method.
See L</flush()> for more information about the other parameters.
=head1 FLUSHING THE BUFFER
=head2 C<flush()>
$result = $bulk->flush;
The C<flush()> method sends all buffered actions to Elasticsearch using
a L<bulk()|Search::Elasticsearch::Client::8_0::Direct/bulk()> request.
=head2 Auto-flushing
An automatic L</flush()> is triggered whenever the C<max_count>, C<max_size>,
or C<max_time> threshold is breached. This causes all actions in the buffer to be
sent to Elasticsearch.
=over
=item * C<max_count>
The maximum number of actions to allow before triggering a L</flush()>.
This can be disabled by setting C<max_count> to C<0>. Defaults to
C<1,000>.
=item * C<max_size>
The maximum size of JSON request body to allow before triggering a
L</flush()>. This can be disabled by setting C<max_size> to C<0>. Defaults
to C<1_000,000> bytes.
=item * C<max_time>
The maximum number of seconds to wait before triggering a flush. Defaults
to C<0> seconds, which means that it is disabled. B<Note:> This timeout
is only triggered when new items are added to the queue, not in the background.
=back
=head2 Errors when flushing
There are two types of error which can be thrown when L</flush()>
is called, either manually or automatically.
=over
=item * Temporary Elasticsearch errors
A C<Cxn> error like a C<NoNodes> error which indicates that your cluster is down.
These errors do not clear the buffer, as they can be retried later on.
=item * Action errors
Individual actions may fail. For instance, a C<create> action will fail
if a document with the same C<index>, C<type> and C<id> already exists.
These action errors are reported via L<callbacks|/Using callbacks>.
=back
=head2 Using callbacks
By default, any I<Action errors> (see above) cause warnings to be
written to C<STDERR>. However, you can use the C<on_error>, C<on_conflict>
and C<on_success> callbacks for more fine-grained control.
All callbacks receive the following arguments:
=over
=item C<$action>
The name of the action, ie C<index>, C<create>, C<update> or C<delete>.
=item C<$response>
The response that Elasticsearch returned for this action.
=item C<$i>
The index of the action, ie the first action in the flush request
will have C<$i> set to C<0>, the second will have C<$i> set to C<1> etc.
=back
=head3 C<on_success>
my $bulk = $es->bulk_helper(
on_success => sub {
my ($action,$response,$i) = @_;
# do something
},
);
The C<on_success> callback is called for every action that has a successful
response.
=head3 C<on_conflict>
my $bulk = $es->bulk_helper(
on_conflict => sub {
my ($action,$response,$i,$version) = @_;
# do something
},
);
The C<on_conflict> callback is called for actions that have triggered
a C<Conflict> error, eg trying to C<create> a document which already
exists. The C<$version> argument will contain the version number
of the document currently stored in Elasticsearch (if found).
=head3 C<on_error>
my $bulk = $es->bulk_helper(
on_error => sub {
my ($action,$response,$i) = @_;
# do something
},
);
The C<on_error> callback is called for any error (unless the C<on_conflict>)
callback has already been called).
=head2 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:
my $bulk = $es->bulk_helper(
max_count => 0,
max_size => 0,
on_error => undef
);
$bulk->add_actions(....);
$response = $bulk->flush;
=head1 CREATE, INDEX, UPDATE, DELETE
=head2 C<add_action()>
$bulk->add_action(
create => { ...params... },
index => { ...params... },
update => { ...params... },
delete => { ...params... }
);
The C<add_action()> method allows you to add multiple C<create>, C<index>,
C<update> and C<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.
B<Note:> Parameters like C<index> or C<type> can be specified as C<index> or as
C<_index>, so the following two lines are equivalent:
index => { index => 'index', type => 'type', id => 1, source => {...}},
index => { _index => 'index', _type => 'type', _id => 1, source => {...}},
B<Note:> The C<index> and C<type> parameters can be specified in the
params for any action, but if not specified, will default to the C<index>
and C<type> values specified in L</new()>. These are required parameters:
they must be specified either in L</new()> or in every action.
=head2 C<create()>
$bulk->create(
{ index => 'custom_index', source => { doc body }},
{ type => 'custom_type', id => 1, source => { doc body }},
...
);
The C<create()> helper method allows you to add multiple C<create> actions.
It accepts the same parameters as L<Search::Elasticsearch::Client::8_0::Direct/create()>
except that the document body should be passed as the C<source> or C<_source>
parameter, instead of as C<body>.
=head2 C<create_docs()>
$bulk->create_docs(
{ doc body },
{ doc body },
...
);
The C<create_docs()> helper is a shorter form of L</create()> which can be used
when you are using the default C<index> and C<type> as set in L</new()>
and you are not specifying a custom C<id> per document. In this case,
you can just pass the individual document bodies.
=head2 C<index()>
$bulk->index(
{ index => 'custom_index', source => { doc body }},
{ type => 'custom_type', id => 1, source => { doc body }},
...
);
The C<index()> helper method allows you to add multiple C<index> actions.
It accepts the same parameters as L<Search::Elasticsearch::Client::8_0::Direct/index()>
except that the document body should be passed as the C<source> or C<_source>
parameter, instead of as C<body>.
=head2 C<delete()>
$bulk->delete(
{ index => 'custom_index', id => 1},
{ type => 'custom_type', id => 2},
...
);
The C<delete()> helper method allows you to add multiple C<delete> actions.
It accepts the same parameters as L<Search::Elasticsearch::Client::8_0::Direct/delete()>.
=head2 C<delete_ids()>
$bulk->delete_ids(1,2,3...)
The C<delete_ids()> helper method can be used when all of the documents you
want to delete have the default C<index> and C<type> as set in L</new()>.
In this case, all you have to do is to pass in a list of IDs.
=head2 C<update()>
$bulk->update(
{ id => 1,
doc => { partial doc },
doc_as_upsert => 1
},
{ id => 2,
script => { script }
upsert => { upsert doc }
},
...
);
The C<update()> helper method allows you to add multiple C<update> actions.
It accepts the same parameters as L<Search::Elasticsearch::Client::8_0::Direct/update()>.
An update can either use a I<partial doc> which gets merged with an existing
doc (example 1 above), or can use a C<script> to update an existing doc
(example 2 above). More information on C<script> can be found here:
L<Search::Elasticsearch::Client::8_0::Direct/update()>.
=head1 AUTHOR
Enrico Zimuel <enrico.zimuel@elastic.co>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2024 by Elasticsearch BV.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
__END__
# ABSTRACT: A helper module for the Bulk API
|