++ed by:

3 PAUSE user(s)
4 non-PAUSE user(s).

Clinton Gormley


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


version 0.28


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




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.



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


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.


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.




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



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.



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.



Clears any docs that are still in the queue.


Clinton Gormley <drtech@cpan.org>


This software is copyright (c) 2014 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.

Hosting generously
sponsored by Bytemark