5_0/Scroll.pm untested

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