| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# |
|
2
|
|
|
|
|
|
|
# This file is part of ElasticSearchX-Model |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
# This software is Copyright (c) 2016 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.2'; |
|
12
|
11
|
|
|
11
|
|
5027
|
use Search::Elasticsearch::Bulk; |
|
|
11
|
|
|
|
|
324140
|
|
|
|
11
|
|
|
|
|
358
|
|
|
13
|
11
|
|
|
11
|
|
67
|
use Moose; |
|
|
11
|
|
|
|
|
13
|
|
|
|
11
|
|
|
|
|
76
|
|
|
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
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
|
26
|
1
|
|
|
|
|
30
|
$self->es->bulk_helper( max_count => $self->size ); |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub add { |
|
30
|
1
|
|
|
1
|
0
|
2
|
my ( $self, $action, $payload ) = ( shift, %{ $_[0] } ); |
|
|
1
|
|
|
|
|
2
|
|
|
31
|
1
|
|
|
|
|
4
|
$payload->{source} = delete $payload->{body}; |
|
32
|
1
|
|
|
|
|
28
|
$self->stash->add_action( $action => $payload ); |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub update { |
|
36
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $doc, $qs ) = @_; |
|
37
|
0
|
0
|
|
|
|
0
|
$self->add( |
|
38
|
|
|
|
|
|
|
{ |
|
39
|
|
|
|
|
|
|
index => ref $doc eq 'HASH' |
|
40
|
|
|
|
|
|
|
? $doc |
|
41
|
|
|
|
|
|
|
: { $doc->_put( $doc->_update($qs) ) } |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
); |
|
44
|
0
|
|
|
|
|
0
|
return $self; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub create { |
|
48
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $doc, $qs ) = @_; |
|
49
|
0
|
0
|
|
|
|
0
|
$self->add( |
|
50
|
|
|
|
|
|
|
{ create => ref $doc eq 'HASH' ? $doc : { $doc->_put($qs) } } ); |
|
51
|
0
|
|
|
|
|
0
|
return $self; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub put { |
|
55
|
1
|
|
|
1
|
1
|
1037
|
my ( $self, $doc, $qs ) = @_; |
|
56
|
|
|
|
|
|
|
$self->add( |
|
57
|
|
|
|
|
|
|
{ |
|
58
|
|
|
|
|
|
|
index => ref $doc eq 'HASH' |
|
59
|
|
|
|
|
|
|
? $doc |
|
60
|
1
|
50
|
|
|
|
9
|
: { $doc->_put, %{ $qs || {} } } |
|
|
1
|
50
|
|
|
|
15
|
|
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
); |
|
63
|
1
|
|
|
|
|
675
|
return $self; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub delete { |
|
67
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $doc, $qs ) = @_; |
|
68
|
0
|
0
|
|
|
|
0
|
$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
|
0
|
|
|
|
|
0
|
return $self; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub clear { |
|
83
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
84
|
0
|
|
|
|
|
0
|
$self->stash->clear_buffer; |
|
85
|
0
|
|
|
|
|
0
|
return $self; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub DEMOLISH { |
|
89
|
1
|
|
|
1
|
0
|
1120
|
my ($self, $in_gd) = @_; |
|
90
|
1
|
50
|
|
|
|
4
|
return if $in_gd; |
|
91
|
1
|
50
|
|
|
|
33
|
$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.2 |
|
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) 2016 by Moritz Onken. |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
This is free software, licensed under: |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
The (three-clause) BSD License |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |