The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Elastic::Model::Bulk - Bulk-saving of multiple docs for increased throughput

VERSION

version 0.51

SYNOPSIS

    $bulk = $model->bulk(
        size        => 1000,
        on_conflict => sub {...},
        on_error    => sub {...}
    );

    $bulk->save($doc);
    $bulk->overwrite($doc);
    ...

    $bulk->commit;

DESCRIPTION

If you need to create or update multiple docs at once, then bulk indexing is the way to go. It batches up the documents and saves size (default 1000) documents in a single request, which is much faster than writing each doc individually.

Once you are finished adding docs to the $bulk indexer, call "commit()" to save any docs that haven't been saved yet. If $bulk goes out of scope, then "commit()" will be called for you, but it is safer to call it yourself.

Note: Bulk indexing is not supported for classes which have unique key constraints.

ATTRIBUTES

size

The number of docs that will be saved in a single request. Defaults to 1000.

on_success

An optional callback which will be called when a document has been indexed successfully. It is called with a single argument: the current document.

on_conflict

A callback which will be called if there is any conflict when saving a doc, for instance, trying to create a doc that already exists, or trying to save a doc when a newer version already exists in Elasticsearch.

The callback is called with two arguments:

  • The doc you are trying to save

  • The current version of the doc which exists in Elasticsearch

See "save()" in Elastic::Model::Role::Doc for more.

on_error

The on_error callback will be called for any non-conflict error (or for conflict errors if no "on_conflict" handler has been specified). It is called with two arguments:

  • The doc you are trying to save

  • The error string returned by Elasticsearch

If no on_error handler is specified, then bulk indexing will die with an error message.

METHODS

save()

    $bulk->save($doc);

Adds a doc to the internal queue to be saved later.

overwrite()

    $bulk->overwrite($doc);

Adds a doc to the interal queue to be overwritten later. In other words, no version checking is done - if a newer version of the doc exists in Elasticsearch, it will be overwritten.

commit()

    $bulk->commit()

Writes all docs in the queue to Elasticsearch. This is called automatically when there are "size" docs in the queue, or when the $bulk instance goes out of scope, although you should call "commit()" yourself once you are finished adding docs, just to be on the safe side.

clear()

    $bulk->clear()

Clears any docs that are still in the queue.

AUTHOR

Clinton Gormley <drtech@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Clinton Gormley.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.