5_0/Scroll.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 114 CC 10 SCAR 24.0
CC
10
Cov
0%
refill_buffer
20.0
BUILDARGS
12.0
next
12.0
CRAP 11.0
1 package Search::Elasticsearch::Client::5_0::Scroll;
2 $Search::Elasticsearch::Client::5_0::Scroll::VERSION = '6.81';
3 use Moo;
4 use Search::Elasticsearch::Util qw(parse_params throw);
5 use namespace::clean;
6  
7 has '_buffer' => ( is => 'ro' );
8  
9 with 'Search::Elasticsearch::Role::Is_Sync',
10 'Search::Elasticsearch::Client::5_0::Role::Scroll';
11  
12 #===================================
13 sub BUILDARGS {
14 #===================================
15 my ( $class, $params ) = parse_params(@_);
16 my $es = delete $params->{es};
17 my $scroll = $params->{scroll} ||= '1m';
18  
19 throw( 'Param',
20 'The (scroll_in_body) parameter has been replaced by (scroll_in_qs)' )
21 if exists $params->{scroll_in_body};
22  
23 my $scroll_in_qs = delete $params->{scroll_in_qs};
24 my $results = $es->search($params);
25  
26 my $total = $results->{hits}{total};
27  
28 return {
29 es => $es,
30 scroll => $scroll,
31 scroll_in_qs => $scroll_in_qs,
32 aggregations => $results->{aggregations},
33 facets => $results->{facets},
34 suggest => $results->{suggest},
35 took => $results->{took},
36 total_took => $results->{took},
37 total => $total,
38 max_score => $results->{hits}{max_score},
39 _buffer => $results->{hits}{hits},
40 $total
41 ? ( _scroll_id => $results->{_scroll_id} )
42 : ( is_finished => 1 )
43 };
44 }
45  
46 #===================================
47 sub next {
48 #===================================
49 my ( $self, $n ) = @_;
50 $n ||= 1;
51 while ( $self->_has_scroll_id and $self->buffer_size < $n ) {
52 $self->refill_buffer;
53 }
54 my @return = splice( @{ $self->_buffer }, 0, $n );
55 $self->finish if @return < $n;
56 return wantarray ? @return : $return[-1];
57 }
58  
59 #===================================
60 sub drain_buffer {
61 #===================================
62 my $self = shift;
63 return splice( @{ $self->_buffer } );
64 }
65  
66 #===================================
67 sub buffer_size { 0 + @{ shift->_buffer } }
68 #===================================
69  
70 #===================================
71 sub refill_buffer {
72 #===================================
73 my $self = shift;
74  
75 return 0 if $self->is_finished;
76  
77 my $buffer = $self->_buffer;
78 my $scroll_id = $self->_scroll_id
79 || return 0 + @$buffer;
80  
81 my $results = $self->scroll_request;
82  
83 my $hits = $results->{hits}{hits};
84 $self->_set_total_took( $self->total_took + $results->{took} );
85  
86 if ( @$hits == 0 ) {
87 $self->_clear_scroll_id;
88 }
89 else {
90 $self->_set__scroll_id( $results->{_scroll_id} );
91 push @$buffer, @$hits;
92 }
93 $self->finish if @$buffer == 0;
94 return 0 + @$buffer;
95 }
96  
97 #===================================
98 sub finish {
99 #===================================
100 my $self = shift;
101 return if $self->is_finished || $self->_pid != $$;
102  
103 $self->_set_is_finished(1);
104 @{ $self->_buffer } = ();
105  
106 my $scroll_id = $self->_scroll_id or return;
107 $self->_clear_scroll_id;
108  
109 my %args
110 = $self->scroll_in_qs
111 ? ( scroll_id => $scroll_id )
112 : ( body => { scroll_id => [$scroll_id] } );
113 eval { $self->es->clear_scroll(%args) };
114 return 1;
115 }
116  
117 1;
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 Search::Elasticsearch::Client::5_0::Scroll - A helper module for scrolled searches
126  
127 =head1 VERSION
128  
129 version 6.81
130  
131 =head1 SYNOPSIS
132  
133 use Search::Elasticsearch;
134  
135 my $es = Search::Elasticsearch->new;
136  
137 my $scroll = $es->scroll_helper(
138 index => 'my_index',
139 body => {
140 query => {...},
141 size => 1000,
142 sort => '_doc'
143 }
144 );
145  
146 say "Total hits: ". $scroll->total;
147  
148 while (my $doc = $scroll->next) {
149 # do something
150 }
151  
152 =head1 DESCRIPTION
153  
154 A I<scrolled search> is a search that allows you to keep pulling results
155 until there are no more matching results, much like a cursor in an SQL
156 database.
157  
158 Unlike paginating through results (with the C<from> parameter in
159 L<search()|Search::Elasticsearch::Client::5_0::Direct/search()>),
160 scrolled searches take a snapshot of the current state of the index. Even
161 if you keep adding new documents to the index or updating existing documents,
162 a scrolled search will only see the index as it was when the search began.
163  
164 This module is a helper utility that wraps the functionality of the
165 L<search()|Search::Elasticsearch::Client::5_0::Direct/search()> and
166 L<scroll()|Search::Elasticsearch::Client::5_0::Direct/scroll()> methods to make
167 them easier to use.
168  
169 This class does L<Search::Elasticsearch::Client::5_0::Role::Scroll> and
170 L<Search::Elasticsearch::Role::Is_Sync>.
171  
172 =head1 USE CASES
173  
174 There are two primary use cases:
175  
176 =head2 Pulling enough results
177  
178 Perhaps you want to group your results by some field, and you don't know
179 exactly how many results you will need in order to return 10 grouped
180 results. With a scrolled search you can keep pulling more results
181 until you have enough. For instance, you can search emails in a mailing
182 list, and return results grouped by C<thread_id>:
183  
184 my (%groups,@results);
185  
186 my $scroll = $es->scroll_helper(
187 index => 'my_emails',
188 type => 'email',
189 body => { query => {... some query ... }}
190 );
191  
192 my $doc;
193 while (@results < 10 and $doc = $scroll->next) {
194  
195 my $thread = $doc->{_source}{thread_id};
196  
197 unless ($groups{$thread}) {
198 $groups{$thread} = [];
199 push @results, $groups{$thread};
200 }
201 push @{$groups{$thread}},$doc;
202  
203 }
204  
205 =head2 Extracting all documents
206  
207 Often you will want to extract all (or a subset of) documents in an index.
208 If you want to change your type mappings, you will need to reindex all of your
209 data. Or perhaps you want to move a subset of the data in one index into
210 a new dedicated index. In these cases, you don't care about sort
211 order, you just want to retrieve all documents which match a query, and do
212 something with them. For instance, to retrieve all the docs for a particular
213 C<client_id>:
214  
215 my $scroll = $es->scroll_helper(
216 index => 'my_index',
217 size => 1000,
218 body => {
219 query => {
220 match => {
221 client_id => 123
222 }
223 },
224 sort => '_doc'
225 }
226 );
227  
228 while (my $doc = $scroll->next) {
229 # do something
230 }
231  
232 Very often the I<something> that you will want to do with these results
233 involves bulk-indexing them into a new index. The easiest way to
234 do this is to use the built-in L<Search::Elasticsearch::Client::5_0::Direct/reindex()>
235 functionality provided by Elasticsearch.
236  
237 =head1 METHODS
238  
239 =head2 C<new()>
240  
241 use Search::Elasticsearch;
242  
243 my $es = Search::Elasticsearch->new(...);
244 my $scroll = $es->scroll_helper(
245 scroll => '1m', # optional
246 scroll_in_qs => 0|1, # optional
247 %search_params
248 );
249  
250 The L<Search::Elasticsearch::Client::5_0::Direct/scroll_helper()> method loads
251 L<Search::Elasticsearch::Client::5_0::Scroll> class and calls L</new()>,
252 passing in any arguments.
253  
254 You can specify a C<scroll> duration (which defaults to C<"1m">) and
255 C<scroll_in_qs> (which defaults to C<false>). Any other parameters are
256 passed directly to L<Search::Elasticsearch::Client::5_0::Direct/search()>.
257  
258 The C<scroll> duration tells Elasticearch how long it should keep the scroll
259 alive. B<Note>: this duration doesn't need to be long enough to process
260 all results, just long enough to process a single B<batch> of results.
261 The expiry gets renewed for another C<scroll> period every time new
262 a new batch of results is retrieved from the cluster.
263  
264 By default, the C<scroll_id> is passed as the C<body> to the
265 L<scroll|Search::Elasticsearch::Client::5_0::Direct/scroll()> request.
266 To send it in the query string instead, set C<scroll_in_qs> to a true value,
267 but be aware: when querying very many indices, the scroll ID can become
268 too long for intervening proxies.
269  
270 The C<scroll> request uses C<GET> by default. To use C<POST> instead,
271 set L<send_get_body_as|Search::Elasticsearch::Transport/send_get_body_as> to
272 C<POST>.
273  
274 =head2 C<next()>
275  
276 $doc = $scroll->next;
277 @docs = $scroll->next($num);
278  
279 The C<next()> method returns the next result, or the next C<$num> results
280 (pulling more results if required). If all results have been exhausted,
281 it returns an empty list.
282  
283 =head2 C<drain_buffer()>
284  
285 @docs = $scroll->drain_buffer;
286  
287 The C<drain_buffer()> method returns all of the documents currently in the
288 buffer, without fetching any more from the cluster.
289  
290 =head2 C<refill_buffer()>
291  
292 $total = $scroll->refill_buffer;
293  
294 The C<refill_buffer()> method fetches the next batch of results from the
295 cluster, stores them in the buffer, and returns the total number of docs
296 currently in the buffer.
297  
298 =head2 C<buffer_size()>
299  
300 $total = $scroll->buffer_size;
301  
302 The C<buffer_size()> method returns the total number of docs currently in
303 the buffer.
304  
305 =head2 C<finish()>
306  
307 $scroll->finish;
308  
309 The C<finish()> method clears out the buffer, sets L</is_finished()> to C<true>
310 and tries to clear the C<scroll_id> on Elasticsearch. This API is only
311 supported since v0.90.5, but the call to C<clear_scroll> is wrapped in an
312 C<eval> so the C<finish()> method can be safely called with any version
313 of Elasticsearch.
314  
315 When the C<$scroll> instance goes out of scope, L</finish()> is called
316 automatically if required.
317  
318 =head2 C<is_finished()>
319  
320 $bool = $scroll->is_finished;
321  
322 A flag which returns C<true> if all results have been processed or
323 L</finish()> has been called.
324  
325 =head1 INFO ACCESSORS
326  
327 The information from the original search is returned via the following
328 accessors:
329  
330 =head2 C<total>
331  
332 The total number of documents that matched your query.
333  
334 =head2 C<max_score>
335  
336 The maximum score of any documents in your query.
337  
338 =head2 C<aggregations>
339  
340 Any aggregations that were specified, or C<undef>
341  
342 =head2 C<facets>
343  
344 Any facets that were specified, or C<undef>
345  
346 =head2 C<suggest>
347  
348 Any suggestions that were specified, or C<undef>
349  
350 =head2 C<took>
351  
352 How long the original search took, in milliseconds
353  
354 =head2 C<took_total>
355  
356 How long the original search plus all subsequent batches took, in milliseconds.
357  
358 =head1 SEE ALSO
359  
360 =over
361  
362 =item * L<Search::Elasticsearch::Client::5_0::Direct/search()>
363  
364 =item * L<Search::Elasticsearch::Client::5_0::Direct/scroll()>
365  
366 =item * L<Search::Elasticsearch::Client::5_0::Direct/reindex()>
367  
368 =back
369  
370 =head1 AUTHOR
371  
372 Enrico Zimuel <enrico.zimuel@elastic.co>
373  
374 =head1 COPYRIGHT AND LICENSE
375  
376 This software is Copyright (c) 2020 by Elasticsearch BV.
377  
378 This is free software, licensed under:
379  
380 The Apache License, Version 2.0, January 2004
381  
382 =cut
383  
384 __END__