« blib/lib/ElasticSearchX/Model/Document/Trait/Field/ID.pm
↑ Summary
blib/lib/ElasticSearchX/Model/Util.pm »
| 1 | # | ||
| 2 | # This file is part of ElasticSearchX-Model | ||
| 3 | # | ||
| 4 | # This software is Copyright (c) 2018 by Moritz Onken. | ||
| 5 | # | ||
| 6 | # This is free software, licensed under: | ||
| 7 | # | ||
| 8 | # The (three-clause) BSD License | ||
| 9 | # | ||
| 10 | package ElasticSearchX::Model::Bulk; | ||
| 11 | $ElasticSearchX::Model::Bulk::VERSION = '1.0.3'; | ||
| 12 | 13 | ▶ | use Search::Elasticsearch::Bulk; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use Moose; | ||
| 14 | |||
| 15 | has stash => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => "Search::Elasticsearch::Bulk", | ||
| 18 | handles => { stash_size => '_buffer_count', commit => "flush" }, | ||
| 19 | lazy_build => 1, | ||
| 20 | ); | ||
| 21 | has size => ( is => 'ro', isa => 'Int', default => 100 ); | ||
| 22 | has es => ( is => 'ro' ); | ||
| 23 | |||
| 24 | sub _build_stash { | ||
| 25 | my $self = shift; | ||
| 26 | $self->es->bulk_helper( max_count => $self->size ); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub add { | ||
| 30 | my ( $self, $action, $payload ) = ( shift, %{ $_[0] } ); | ||
| 31 | $payload->{source} = delete $payload->{body}; | ||
| 32 | $self->stash->add_action( $action => $payload ); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub update { | ||
| 36 | my ( $self, $doc, $qs ) = @_; | ||
| 37 | $self->add( | ||
| 38 | { | ||
| 39 | index => ref $doc eq 'HASH' | ||
| 40 | ? $doc | ||
| 41 | : { $doc->_put( $doc->_update($qs) ) } | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub create { | ||
| 48 | my ( $self, $doc, $qs ) = @_; | ||
| 49 | $self->add( | ||
| 50 | { create => ref $doc eq 'HASH' ? $doc : { $doc->_put($qs) } } ); | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub put { | ||
| 55 | my ( $self, $doc, $qs ) = @_; | ||
| 56 | $self->add( | ||
| 57 | { | ||
| 58 | index => ref $doc eq 'HASH' | ||
| 59 | ? $doc | ||
| 60 | : { $doc->_put, %{ $qs || {} } } | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub delete { | ||
| 67 | my ( $self, $doc, $qs ) = @_; | ||
| 68 | $self->add( | ||
| 69 | { | ||
| 70 | delete => ref $doc eq 'HASH' | ||
| 71 | ? $doc | ||
| 72 | : { | ||
| 73 | index => $doc->index->name, | ||
| 74 | type => $doc->meta->short_name, | ||
| 75 | id => $doc->_id, | ||
| 76 | } | ||
| 77 | } | ||
| 78 | ); | ||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub clear { | ||
| 83 | my $self = shift; | ||
| 84 | $self->stash->clear_buffer; | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub DEMOLISH { | ||
| 89 | my ($self, $in_gd) = @_; | ||
| 90 | return if $in_gd; | ||
| 91 | $self->commit if $self->has_stash; | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ | ||
| 97 | |||
| 98 | =pod | ||
| 99 | |||
| 100 | =encoding UTF-8 | ||
| 101 | |||
| 102 | =head1 NAME | ||
| 103 | |||
| 104 | ElasticSearchX::Model::Bulk | ||
| 105 | |||
| 106 | =head1 VERSION | ||
| 107 | |||
| 108 | version 1.0.3 | ||
| 109 | |||
| 110 | =head1 SYNOPSIS | ||
| 111 | |||
| 112 | my $bulk = $model->bulk( size => 10 ); | ||
| 113 | my $document = $model->index('default')->type('tweet')->new_document({ | ||
| 114 | message => 'Hello there!', | ||
| 115 | date => DateTime->now, | ||
| 116 | }); | ||
| 117 | $bulk->put( $document ); | ||
| 118 | $bulk->commit; | ||
| 119 | |||
| 120 | =head1 DESCRIPTION | ||
| 121 | |||
| 122 | This class is a wrapper around L<Search::Elasticsearch::Bulk> which adds | ||
| 123 | some convenience. By specifiying a L</size> you set the maximum | ||
| 124 | number of documents that are processed in one request. You can either | ||
| 125 | L</put> or L</delete> documents. Once the C<$bulk> object is out | ||
| 126 | of scope, it will automatically commit its L</stash>. Call L</clear> | ||
| 127 | if before if you don't want that to happen. | ||
| 128 | |||
| 129 | =head1 ATTRIBUTES | ||
| 130 | |||
| 131 | =head2 size | ||
| 132 | |||
| 133 | The maximum number of documents that are processed in one request. | ||
| 134 | Once the stash hits that number, a bulk request will be issued | ||
| 135 | automatically and the stash will be cleared. | ||
| 136 | |||
| 137 | =head2 stash | ||
| 138 | |||
| 139 | The stash includes the documents that will be processed at the | ||
| 140 | next commit. A commit is either automatically issued if the size | ||
| 141 | of the stash is greater then L</size>, if the C<$bulk> object | ||
| 142 | gets out of scope or if you call L</commit> explicitly. | ||
| 143 | |||
| 144 | =head2 es | ||
| 145 | |||
| 146 | The L<Search::Elasticsearch> object. | ||
| 147 | |||
| 148 | =head1 METHODS | ||
| 149 | |||
| 150 | =head2 create | ||
| 151 | |||
| 152 | =head2 update | ||
| 153 | |||
| 154 | =head2 put( $doc ) | ||
| 155 | |||
| 156 | =head2 put( $doc, { %qs } ) | ||
| 157 | |||
| 158 | Put a document. Accepts a document object (see | ||
| 159 | L<ElasticSearchX::Model::Document::Set/new_document>) or a | ||
| 160 | HashRef for better performance. | ||
| 161 | |||
| 162 | =head2 delete | ||
| 163 | |||
| 164 | Delete a document. You can either pass a document object or a | ||
| 165 | HashRef that consists of C<index>, C<type> and C<id>. | ||
| 166 | |||
| 167 | =head2 commit | ||
| 168 | |||
| 169 | Commits the documents in the stash to ElasticSearch. | ||
| 170 | |||
| 171 | =head2 clear | ||
| 172 | |||
| 173 | Clears the stash. | ||
| 174 | |||
| 175 | =head2 stash_size | ||
| 176 | |||
| 177 | Returns the number of documents in the stash. | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Moritz Onken | ||
| 182 | |||
| 183 | =head1 COPYRIGHT AND LICENSE | ||
| 184 | |||
| 185 | This software is Copyright (c) 2018 by Moritz Onken. | ||
| 186 | |||
| 187 | This is free software, licensed under: | ||
| 188 | |||
| 189 | The (three-clause) BSD License | ||
| 190 | |||
| 191 | =cut | ||