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