| 1 | # Licensed to Elasticsearch B.V under one or more agreements. | ||
| 2 | # Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| 3 | # See the LICENSE file in the project root for more information | ||
| 4 | |||
| 5 | package Search::Elasticsearch::Client::2_0::Bulk; | ||
| 6 | $Search::Elasticsearch::Client::2_0::Bulk::VERSION = '7.30_1'; # TRIAL | ||
| 7 | |||
| 8 | $Search::Elasticsearch::Client::2_0::Bulk::VERSION = '7.301'; | ||
| 9 | use Moo; | ||
| 10 | with 'Search::Elasticsearch::Client::2_0::Role::Bulk', | ||
| 11 | 'Search::Elasticsearch::Role::Is_Sync'; | ||
| 12 | use Search::Elasticsearch::Util qw(parse_params throw); | ||
| 13 | use Try::Tiny; | ||
| 14 | use namespace::clean; | ||
| 15 | |||
| 16 | #=================================== | ||
| 17 | sub add_action { | ||
| 18 | #=================================== | ||
| 19 | my $self = shift; | ||
| 20 | my $buffer = $self->_buffer; | ||
| 21 | my $max_size = $self->max_size; | ||
| 22 | my $max_count = $self->max_count; | ||
| 23 | my $max_time = $self->max_time; | ||
| 24 | |||
| 25 | while (@_) { | ||
| 26 | my @json = $self->_encode_action( splice( @_, 0, 2 ) ); | ||
| 27 | |||
| 28 | push @$buffer, @json; | ||
| 29 | |||
| 30 | my $size = $self->_buffer_size; | ||
| 31 | $size += length($_) + 1 for @json; | ||
| 32 | $self->_buffer_size($size); | ||
| 33 | |||
| 34 | my $count = $self->_buffer_count( $self->_buffer_count + 1 ); | ||
| 35 | |||
| 36 | $self->flush | ||
| 37 | if ( $max_size and $size >= $max_size ) | ||
| 38 | || ( $max_count and $count >= $max_count ) | ||
| 39 | || ( $max_time and time >= $self->_last_flush + $max_time ); | ||
| 40 | } | ||
| 41 | return 1; | ||
| 42 | } | ||
| 43 | |||
| 44 | #=================================== | ||
| 45 | sub flush { | ||
| 46 | #=================================== | ||
| 47 | my $self = shift; | ||
| 48 | $self->_last_flush(time); | ||
| 49 | |||
| 50 | return { items => [] } | ||
| 51 | unless $self->_buffer_size; | ||
| 52 | |||
| 53 | if ( $self->verbose ) { | ||
| 54 | local $| = 1; | ||
| 55 | print "."; | ||
| 56 | } | ||
| 57 | my $buffer = $self->_buffer; | ||
| 58 | my $results = try { | ||
| 59 | my $res = $self->es->bulk( %{ $self->_bulk_args }, body => $buffer ); | ||
| 60 | $self->clear_buffer; | ||
| 61 | return $res; | ||
| 62 | } | ||
| 63 | catch { | ||
| 64 | my $error = $_; | ||
| 65 | $self->clear_buffer | ||
| 66 | unless $error->is( 'Cxn', 'NoNodes' ); | ||
| 67 | |||
| 68 | die $error; | ||
| 69 | }; | ||
| 70 | $self->_report( $buffer, $results ); | ||
| 71 | return defined wantarray ? $results : undef; | ||
| 72 | } | ||
| 73 | |||
| 74 | #=================================== | ||
| 75 | sub reindex { | ||
| 76 | #=================================== | ||
| 77 | my ( $self, $params ) = parse_params(@_); | ||
| 78 | my $src = $params->{source} | ||
| 79 | or throw( 'Param', "Missing required param <source>" ); | ||
| 80 | |||
| 81 | my $transform = $self->_doc_transformer($params); | ||
| 82 | |||
| 83 | if ( ref $src eq 'HASH' ) { | ||
| 84 | $src = {%$src}; | ||
| 85 | my $es = delete $src->{es} || $self->es; | ||
| 86 | my $scroll = $es->scroll_helper( | ||
| 87 | search_type => 'scan', | ||
| 88 | size => 500, | ||
| 89 | %$src | ||
| 90 | ); | ||
| 91 | |||
| 92 | $src = sub { | ||
| 93 | $scroll->refill_buffer; | ||
| 94 | $scroll->drain_buffer; | ||
| 95 | }; | ||
| 96 | |||
| 97 | print "Reindexing " . $scroll->total . " docs\n" | ||
| 98 | if $self->verbose; | ||
| 99 | } | ||
| 100 | |||
| 101 | while ( my @docs = grep {defined} $src->() ) { | ||
| 102 | $self->index( grep {$_} map { $transform->($_) } @docs ); | ||
| 103 | } | ||
| 104 | $self->flush; | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | =pod | ||
| 111 | |||
| 112 | =encoding UTF-8 | ||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | Search::Elasticsearch::Client::2_0::Bulk - A helper module for the Bulk API and for reindexing | ||
| 117 | |||
| 118 | =head1 VERSION | ||
| 119 | |||
| 120 | version 7.30_1 | ||
| 121 | |||
| 122 | =head1 SYNOPSIS | ||
| 123 | |||
| 124 | use Search::Elasticsearch; | ||
| 125 | |||
| 126 | my $es = Search::Elasticsearch->new; | ||
| 127 | my $bulk = $es->bulk_helper( | ||
| 128 | index => 'my_index', | ||
| 129 | type => 'my_type' | ||
| 130 | ); | ||
| 131 | |||
| 132 | # Index docs: | ||
| 133 | $bulk->index({ id => 1, source => { foo => 'bar' }}); | ||
| 134 | $bulk->add_action( index => { id => 1, source => { foo=> 'bar' }}); | ||
| 135 | |||
| 136 | # Create docs: | ||
| 137 | $bulk->create({ id => 1, source => { foo => 'bar' }}); | ||
| 138 | $bulk->add_action( create => { id => 1, source => { foo=> 'bar' }}); | ||
| 139 | $bulk->create_docs({ foo => 'bar' }) | ||
| 140 | |||
| 141 | # Delete docs: | ||
| 142 | $bulk->delete({ id => 1}); | ||
| 143 | $bulk->add_action( delete => { id => 1 }); | ||
| 144 | $bulk->delete_ids(1,2,3) | ||
| 145 | |||
| 146 | # Update docs: | ||
| 147 | $bulk->update({ id => 1, script => '...' }); | ||
| 148 | $bulk->add_action( update => { id => 1, script => '...' }); | ||
| 149 | |||
| 150 | # Manual flush | ||
| 151 | $bulk->flush; | ||
| 152 | |||
| 153 | # Reindex docs: | ||
| 154 | my $bulk = $es->bulk_helper( | ||
| 155 | index => 'new_index', | ||
| 156 | verbose => 1 | ||
| 157 | ); | ||
| 158 | |||
| 159 | $bulk->reindex( source => { index => 'old_index' }); | ||
| 160 | |||
| 161 | =head1 DESCRIPTION | ||
| 162 | |||
| 163 | This module provides a wrapper for the L<Search::Elasticsearch::Client::2_0::Direct/bulk()> | ||
| 164 | method which makes it easier to run multiple create, index, update or delete | ||
| 165 | actions in a single request. It also provides a simple interface | ||
| 166 | for L<reindexing documents|/REINDEXING DOCUMENTS>. | ||
| 167 | |||
| 168 | The L<Search::Elasticsearch::Client::2_0::Bulk> module acts as a queue, buffering up actions | ||
| 169 | until it reaches a maximum count of actions, or a maximum size of JSON request | ||
| 170 | body, at which point it issues a C<bulk()> request. | ||
| 171 | |||
| 172 | Once you have finished adding actions, call L</flush()> to force the final | ||
| 173 | C<bulk()> request on the items left in the queue. | ||
| 174 | |||
| 175 | This class does L<Search::Elasticsearch::Client::2_0::Role::Bulk> and | ||
| 176 | L<Search::Elasticsearch::Role::Is_Sync>. | ||
| 177 | |||
| 178 | =head1 CREATING A NEW INSTANCE | ||
| 179 | |||
| 180 | =head2 C<new()> | ||
| 181 | |||
| 182 | my $bulk = $es->bulk_helper( | ||
| 183 | |||
| 184 | index => 'default_index', # optional | ||
| 185 | type => 'default_type', # optional | ||
| 186 | %other_bulk_params # optional | ||
| 187 | |||
| 188 | max_count => 1_000, # optional | ||
| 189 | max_size => 1_000_000, # optional | ||
| 190 | max_time => 5, # optional | ||
| 191 | |||
| 192 | verbose => 0 | 1, # optional | ||
| 193 | |||
| 194 | on_success => sub {...}, # optional | ||
| 195 | on_error => sub {...}, # optional | ||
| 196 | on_conflict => sub {...}, # optional | ||
| 197 | |||
| 198 | |||
| 199 | ); | ||
| 200 | |||
| 201 | The C<new()> method returns a new C<$bulk> object. You must pass your | ||
| 202 | Search::Elasticsearch client as the C<es> argument. | ||
| 203 | |||
| 204 | The C<index> and C<type> parameters provide default values for | ||
| 205 | C<index> and C<type>, which can be overridden in each action. | ||
| 206 | You can also pass any other values which are accepted | ||
| 207 | by the L<bulk()|Search::Elasticsearch::Client::2_0::Direct/bulk()> method. | ||
| 208 | |||
| 209 | See L</flush()> for more information about the other parameters. | ||
| 210 | |||
| 211 | =head1 FLUSHING THE BUFFER | ||
| 212 | |||
| 213 | =head2 C<flush()> | ||
| 214 | |||
| 215 | $result = $bulk->flush; | ||
| 216 | |||
| 217 | The C<flush()> method sends all buffered actions to Elasticsearch using | ||
| 218 | a L<bulk()|Search::Elasticsearch::Client::2_0::Direct/bulk()> request. | ||
| 219 | |||
| 220 | =head2 Auto-flushing | ||
| 221 | |||
| 222 | An automatic L</flush()> is triggered whenever the C<max_count>, C<max_size>, | ||
| 223 | or C<max_time> threshold is breached. This causes all actions in the buffer to be | ||
| 224 | sent to Elasticsearch. | ||
| 225 | |||
| 226 | =over | ||
| 227 | |||
| 228 | =item * C<max_count> | ||
| 229 | |||
| 230 | The maximum number of actions to allow before triggering a L</flush()>. | ||
| 231 | This can be disabled by setting C<max_count> to C<0>. Defaults to | ||
| 232 | C<1,000>. | ||
| 233 | |||
| 234 | =item * C<max_size> | ||
| 235 | |||
| 236 | The maximum size of JSON request body to allow before triggering a | ||
| 237 | L</flush()>. This can be disabled by setting C<max_size> to C<0>. Defaults | ||
| 238 | to C<1_000,000> bytes. | ||
| 239 | |||
| 240 | =item * C<max_time> | ||
| 241 | |||
| 242 | The maximum number of seconds to wait before triggering a flush. Defaults | ||
| 243 | to C<0> seconds, which means that it is disabled. B<Note:> This timeout | ||
| 244 | is only triggered when new items are added to the queue, not in the background. | ||
| 245 | |||
| 246 | =back | ||
| 247 | |||
| 248 | =head2 Errors when flushing | ||
| 249 | |||
| 250 | There are two types of error which can be thrown when L</flush()> | ||
| 251 | is called, either manually or automatically. | ||
| 252 | |||
| 253 | =over | ||
| 254 | |||
| 255 | =item * Temporary Elasticsearch errors | ||
| 256 | |||
| 257 | A C<Cxn> error like a C<NoNodes> error which indicates that your cluster is down. | ||
| 258 | These errors do not clear the buffer, as they can be retried later on. | ||
| 259 | |||
| 260 | =item * Action errors | ||
| 261 | |||
| 262 | Individual actions may fail. For instance, a C<create> action will fail | ||
| 263 | if a document with the same C<index>, C<type> and C<id> already exists. | ||
| 264 | These action errors are reported via L<callbacks|/Using callbacks>. | ||
| 265 | |||
| 266 | =back | ||
| 267 | |||
| 268 | =head2 Using callbacks | ||
| 269 | |||
| 270 | By default, any I<Action errors> (see above) cause warnings to be | ||
| 271 | written to C<STDERR>. However, you can use the C<on_error>, C<on_conflict> | ||
| 272 | and C<on_success> callbacks for more fine-grained control. | ||
| 273 | |||
| 274 | All callbacks receive the following arguments: | ||
| 275 | |||
| 276 | =over | ||
| 277 | |||
| 278 | =item C<$action> | ||
| 279 | |||
| 280 | The name of the action, ie C<index>, C<create>, C<update> or C<delete>. | ||
| 281 | |||
| 282 | =item C<$response> | ||
| 283 | |||
| 284 | The response that Elasticsearch returned for this action. | ||
| 285 | |||
| 286 | =item C<$i> | ||
| 287 | |||
| 288 | The index of the action, ie the first action in the flush request | ||
| 289 | will have C<$i> set to C<0>, the second will have C<$i> set to C<1> etc. | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | =head3 C<on_success> | ||
| 294 | |||
| 295 | my $bulk = $es->bulk_helper( | ||
| 296 | on_success => sub { | ||
| 297 | my ($action,$response,$i) = @_; | ||
| 298 | # do something | ||
| 299 | }, | ||
| 300 | ); | ||
| 301 | |||
| 302 | The C<on_success> callback is called for every action that has a successful | ||
| 303 | response. | ||
| 304 | |||
| 305 | =head3 C<on_conflict> | ||
| 306 | |||
| 307 | my $bulk = $es->bulk_helper( | ||
| 308 | on_conflict => sub { | ||
| 309 | my ($action,$response,$i,$version) = @_; | ||
| 310 | # do something | ||
| 311 | }, | ||
| 312 | ); | ||
| 313 | |||
| 314 | The C<on_conflict> callback is called for actions that have triggered | ||
| 315 | a C<Conflict> error, eg trying to C<create> a document which already | ||
| 316 | exists. The C<$version> argument will contain the version number | ||
| 317 | of the document currently stored in Elasticsearch (if found). | ||
| 318 | |||
| 319 | =head3 C<on_error> | ||
| 320 | |||
| 321 | my $bulk = $es->bulk_helper( | ||
| 322 | on_error => sub { | ||
| 323 | my ($action,$response,$i) = @_; | ||
| 324 | # do something | ||
| 325 | }, | ||
| 326 | ); | ||
| 327 | |||
| 328 | The C<on_error> callback is called for any error (unless the C<on_conflict>) | ||
| 329 | callback has already been called). | ||
| 330 | |||
| 331 | =head2 Disabling callbacks and autoflush | ||
| 332 | |||
| 333 | If you want to be in control of flushing, and you just want to receive | ||
| 334 | the raw response that Elasticsearch sends instead of using callbacks, | ||
| 335 | then you can do so as follows: | ||
| 336 | |||
| 337 | my $bulk = $es->bulk_helper( | ||
| 338 | max_count => 0, | ||
| 339 | max_size => 0, | ||
| 340 | on_error => undef | ||
| 341 | ); | ||
| 342 | |||
| 343 | $bulk->add_actions(....); | ||
| 344 | $response = $bulk->flush; | ||
| 345 | |||
| 346 | =head1 CREATE, INDEX, UPDATE, DELETE | ||
| 347 | |||
| 348 | =head2 C<add_action()> | ||
| 349 | |||
| 350 | $bulk->add_action( | ||
| 351 | create => { ...params... }, | ||
| 352 | index => { ...params... }, | ||
| 353 | update => { ...params... }, | ||
| 354 | delete => { ...params... } | ||
| 355 | ); | ||
| 356 | |||
| 357 | The C<add_action()> method allows you to add multiple C<create>, C<index>, | ||
| 358 | C<update> and C<delete> actions to the queue. The first value is the action | ||
| 359 | type, and the second value is the parameters that describe that action. | ||
| 360 | See the individual helper methods below for details. | ||
| 361 | |||
| 362 | B<Note:> Parameters like C<index> or C<type> can be specified as C<index> or as | ||
| 363 | C<_index>, so the following two lines are equivalent: | ||
| 364 | |||
| 365 | index => { index => 'index', type => 'type', id => 1, source => {...}}, | ||
| 366 | index => { _index => 'index', _type => 'type', _id => 1, _source => {...}}, | ||
| 367 | |||
| 368 | B<Note:> The C<index> and C<type> parameters can be specified in the | ||
| 369 | params for any action, but if not specified, will default to the C<index> | ||
| 370 | and C<type> values specified in L</new()>. These are required parameters: | ||
| 371 | they must be specified either in L</new()> or in every action. | ||
| 372 | |||
| 373 | =head2 C<create()> | ||
| 374 | |||
| 375 | $bulk->create( | ||
| 376 | { index => 'custom_index', source => { doc body }}, | ||
| 377 | { type => 'custom_type', id => 1, source => { doc body }}, | ||
| 378 | ... | ||
| 379 | ); | ||
| 380 | |||
| 381 | The C<create()> helper method allows you to add multiple C<create> actions. | ||
| 382 | It accepts the same parameters as L<Search::Elasticsearch::Client::2_0::Direct/create()> | ||
| 383 | except that the document body should be passed as the C<source> or C<_source> | ||
| 384 | parameter, instead of as C<body>. | ||
| 385 | |||
| 386 | =head2 C<create_docs()> | ||
| 387 | |||
| 388 | $bulk->create_docs( | ||
| 389 | { doc body }, | ||
| 390 | { doc body }, | ||
| 391 | ... | ||
| 392 | ); | ||
| 393 | |||
| 394 | The C<create_docs()> helper is a shorter form of L</create()> which can be used | ||
| 395 | when you are using the default C<index> and C<type> as set in L</new()> | ||
| 396 | and you are not specifying a custom C<id> per document. In this case, | ||
| 397 | you can just pass the individual document bodies. | ||
| 398 | |||
| 399 | =head2 C<index()> | ||
| 400 | |||
| 401 | $bulk->index( | ||
| 402 | { index => 'custom_index', source => { doc body }}, | ||
| 403 | { type => 'custom_type', id => 1, source => { doc body }}, | ||
| 404 | ... | ||
| 405 | ); | ||
| 406 | |||
| 407 | The C<index()> helper method allows you to add multiple C<index> actions. | ||
| 408 | It accepts the same parameters as L<Search::Elasticsearch::Client::2_0::Direct/index()> | ||
| 409 | except that the document body should be passed as the C<source> or C<_source> | ||
| 410 | parameter, instead of as C<body>. | ||
| 411 | |||
| 412 | =head2 C<delete()> | ||
| 413 | |||
| 414 | $bulk->delete( | ||
| 415 | { index => 'custom_index', id => 1}, | ||
| 416 | { type => 'custom_type', id => 2}, | ||
| 417 | ... | ||
| 418 | ); | ||
| 419 | |||
| 420 | The C<delete()> helper method allows you to add multiple C<delete> actions. | ||
| 421 | It accepts the same parameters as L<Search::Elasticsearch::Client::2_0::Direct/delete()>. | ||
| 422 | |||
| 423 | =head2 C<delete_ids()> | ||
| 424 | |||
| 425 | $bulk->delete_ids(1,2,3...) | ||
| 426 | |||
| 427 | The C<delete_ids()> helper method can be used when all of the documents you | ||
| 428 | want to delete have the default C<index> and C<type> as set in L</new()>. | ||
| 429 | In this case, all you have to do is to pass in a list of IDs. | ||
| 430 | |||
| 431 | =head2 C<update()> | ||
| 432 | |||
| 433 | $bulk->update( | ||
| 434 | { id => 1, | ||
| 435 | doc => { partial doc }, | ||
| 436 | doc_as_upsert => 1 | ||
| 437 | }, | ||
| 438 | { id => 2, | ||
| 439 | lang => 'mvel', | ||
| 440 | script => { script } | ||
| 441 | upsert => { upsert doc } | ||
| 442 | }, | ||
| 443 | ... | ||
| 444 | ); | ||
| 445 | |||
| 446 | The C<update()> helper method allows you to add multiple C<update> actions. | ||
| 447 | It accepts the same parameters as L<Search::Elasticsearch::Client::2_0::Direct/update()>. | ||
| 448 | An update can either use a I<partial doc> which gets merged with an existing | ||
| 449 | doc (example 1 above), or can use a C<script> to update an existing doc | ||
| 450 | (example 2 above). More information on C<script> can be found here: | ||
| 451 | L<Search::Elasticsearch::Client::2_0::Direct/update()>. | ||
| 452 | |||
| 453 | =head1 REINDEXING DOCUMENTS | ||
| 454 | |||
| 455 | A common use case for bulk indexing is to reindex a whole index when | ||
| 456 | changing the type mappings or analysis chain. This typically | ||
| 457 | combines bulk indexing with L<scrolled searches|Search::Elasticsearch::Client::2_0::Scroll>: | ||
| 458 | the scrolled search pulls all of the data from the source index, and | ||
| 459 | the bulk indexer indexes the data into the new index. | ||
| 460 | |||
| 461 | =head2 C<reindex()> | ||
| 462 | |||
| 463 | $bulk->reindex( | ||
| 464 | source => $source, # required | ||
| 465 | transform => \&transform, # optional | ||
| 466 | version_type => 'external|internal', # optional | ||
| 467 | ); | ||
| 468 | |||
| 469 | The C<reindex()> method requires a C<$source> parameter, which provides | ||
| 470 | the source for the documents which are to be reindexed. | ||
| 471 | |||
| 472 | =head2 Reindexing from another index | ||
| 473 | |||
| 474 | If the C<source> argument is a HASH ref, then the hash is passed to | ||
| 475 | L<Search::Elasticsearch::Client::2_0::Scroll/new()> to create a new scrolled search. | ||
| 476 | |||
| 477 | my $bulk = $es->bulk_helper( | ||
| 478 | index => 'new_index', | ||
| 479 | verbose => 1 | ||
| 480 | ); | ||
| 481 | |||
| 482 | $bulk->reindex( | ||
| 483 | source => { | ||
| 484 | index => 'old_index', | ||
| 485 | size => 500, # default | ||
| 486 | search_type => 'scan' # default | ||
| 487 | } | ||
| 488 | ); | ||
| 489 | |||
| 490 | If a default C<index> or C<type> has been specified in the call to | ||
| 491 | L</new()>, then it will replace the C<index> and C<type> values for | ||
| 492 | the docs returned from the scrolled search. In the example above, | ||
| 493 | all docs will be retrieved from C<"old_index"> and will be bulk indexed | ||
| 494 | into C<"new_index">. | ||
| 495 | |||
| 496 | =head2 Reindexing from a generic source | ||
| 497 | |||
| 498 | The C<source> parameter also accepts a coderef or an anonymous sub, | ||
| 499 | which should return one or more new documents every time it is executed. | ||
| 500 | This allows you to pass any iterator, wrapped in an anonymous sub: | ||
| 501 | |||
| 502 | my $iter = get_iterator_from_somewhere(); | ||
| 503 | |||
| 504 | $bulk->reindex( | ||
| 505 | source => sub { $iter->next } | ||
| 506 | ); | ||
| 507 | |||
| 508 | =head2 Transforming docs on the fly | ||
| 509 | |||
| 510 | The C<transform> parameter allows you to change documents on the fly, | ||
| 511 | using a callback. The callback receives the document as the only argument, | ||
| 512 | and should return the updated document, or C<undef> if the document should | ||
| 513 | not be indexed: | ||
| 514 | |||
| 515 | $bulk->reindex( | ||
| 516 | source => { index => 'old_index' }, | ||
| 517 | transform => sub { | ||
| 518 | my $doc = shift; | ||
| 519 | |||
| 520 | # don't index doc marked as valid:false | ||
| 521 | return undef unless $doc->{_source}{valid}; | ||
| 522 | |||
| 523 | # convert $tag to @tags | ||
| 524 | $doc->{_source}{tags} = [ delete $doc->{_source}{tag}]; | ||
| 525 | return $doc | ||
| 526 | } | ||
| 527 | ); | ||
| 528 | |||
| 529 | =head2 Reindexing from another cluster | ||
| 530 | |||
| 531 | By default, L</reindex()> expects the source and destination indices | ||
| 532 | to be in the same cluster. To pull data from one cluster and index it into | ||
| 533 | another, you can use two separate C<$es> objects: | ||
| 534 | |||
| 535 | $es_target = Search::Elasticsearch->new( nodes => 'localhost:9200' ); | ||
| 536 | $es_source = Search::Elasticsearch->new( nodes => 'search1:9200' ); | ||
| 537 | |||
| 538 | my $bulk = $es_targert->bulk_helper( | ||
| 539 | verbose => 1 | ||
| 540 | ) | ||
| 541 | -> reindex( | ||
| 542 | source => { | ||
| 543 | es => $es_source, | ||
| 544 | index => 'my_index' | ||
| 545 | } | ||
| 546 | ); | ||
| 547 | |||
| 548 | =head2 Parents and routing | ||
| 549 | |||
| 550 | If you are using parent-child relationships or custom C<routing> values, | ||
| 551 | and you want to preserve these when you reindex your documents, then | ||
| 552 | you will need to request these values specifically, as follows: | ||
| 553 | |||
| 554 | $bulk->reindex( | ||
| 555 | source => { | ||
| 556 | index => 'old_index', | ||
| 557 | fields => ['_source','_parent','_routing'] | ||
| 558 | } | ||
| 559 | ); | ||
| 560 | |||
| 561 | =head2 Working with version numbers | ||
| 562 | |||
| 563 | Every document in Elasticsearch has a current C<version> number, which | ||
| 564 | is used for L<optimistic concurrency control|http://en.wikipedia.org/wiki/Optimistic_concurrency_control>, | ||
| 565 | that is, to ensure that you don't overwrite changes that have been made | ||
| 566 | by another process. | ||
| 567 | |||
| 568 | All CRUD operations accept a C<version> parameter and a C<version_type> | ||
| 569 | parameter which tells Elasticsearch that the change should only be made | ||
| 570 | if the current document corresponds to these parameters. The | ||
| 571 | C<version_type> parameter can have the following values: | ||
| 572 | |||
| 573 | =over | ||
| 574 | |||
| 575 | =item * C<internal> | ||
| 576 | |||
| 577 | Use Elasticsearch version numbers. Documents are only changed if the | ||
| 578 | document in Elasticsearch has the B<same> C<version> number that is | ||
| 579 | specified in the CRUD operation. After the change, the new | ||
| 580 | version number is C<version+1>. | ||
| 581 | |||
| 582 | =item * C<external> | ||
| 583 | |||
| 584 | Use an external versioning system, such as timestamps or version numbers | ||
| 585 | from an external database. Documents are only changed if the document | ||
| 586 | in Elasticsearch has a B<lower> C<version> number than the one | ||
| 587 | specified in the CRUD operation. After the change, the new version | ||
| 588 | number is C<version>. | ||
| 589 | |||
| 590 | =back | ||
| 591 | |||
| 592 | If you would like to reindex documents from one index to another, preserving | ||
| 593 | the C<version> numbers from the original index, then you need the following: | ||
| 594 | |||
| 595 | $bulk->reindex( | ||
| 596 | source => { | ||
| 597 | index => 'old_index', | ||
| 598 | version => 1, # retrieve version numbers in search | ||
| 599 | }, | ||
| 600 | version_type => 'external' # use these "external" version numbers | ||
| 601 | ); | ||
| 602 | |||
| 603 | =head1 AUTHOR | ||
| 604 | |||
| 605 | Enrico Zimuel <enrico.zimuel@elastic.co> | ||
| 606 | |||
| 607 | =head1 COPYRIGHT AND LICENSE | ||
| 608 | |||
| 609 | This software is Copyright (c) 2020 by Elasticsearch BV. | ||
| 610 | |||
| 611 | This is free software, licensed under: | ||
| 612 | |||
| 613 | The Apache License, Version 2.0, January 2004 | ||
| 614 | |||
| 615 | =cut | ||
| 616 | |||
| 617 | __END__ |