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