| 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::7_0::Scroll; | ||
| 6 | $Search::Elasticsearch::Client::7_0::Scroll::VERSION = '7.30_1'; # TRIAL | ||
| 7 | |||
| 8 | $Search::Elasticsearch::Client::7_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::7_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 | my $results = $es->search($params); | ||
| 26 | |||
| 27 | my $total = $results->{hits}{total}; | ||
| 28 | if (ref $total) { | ||
| 29 | $total = $total->{value} | ||
| 30 | } | ||
| 31 | |||
| 32 | return { | ||
| 33 | es => $es, | ||
| 34 | scroll => $scroll, | ||
| 35 | aggregations => $results->{aggregations}, | ||
| 36 | facets => $results->{facets}, | ||
| 37 | suggest => $results->{suggest}, | ||
| 38 | took => $results->{took}, | ||
| 39 | total_took => $results->{took}, | ||
| 40 | total => $total, | ||
| 41 | max_score => $results->{hits}{max_score}, | ||
| 42 | _buffer => $results->{hits}{hits}, | ||
| 43 | $total | ||
| 44 | ? ( _scroll_id => $results->{_scroll_id} ) | ||
| 45 | : ( is_finished => 1 ) | ||
| 46 | }; | ||
| 47 | } | ||
| 48 | |||
| 49 | #=================================== | ||
| 50 | sub next { | ||
| 51 | #=================================== | ||
| 52 | my ( $self, $n ) = @_; | ||
| 53 | $n ||= 1; | ||
| 54 | while ( $self->_has_scroll_id and $self->buffer_size < $n ) { | ||
| 55 | $self->refill_buffer; | ||
| 56 | } | ||
| 57 | my @return = splice( @{ $self->_buffer }, 0, $n ); | ||
| 58 | $self->finish if @return < $n; | ||
| 59 | return wantarray ? @return : $return[-1]; | ||
| 60 | } | ||
| 61 | |||
| 62 | #=================================== | ||
| 63 | sub drain_buffer { | ||
| 64 | #=================================== | ||
| 65 | my $self = shift; | ||
| 66 | return splice( @{ $self->_buffer } ); | ||
| 67 | } | ||
| 68 | |||
| 69 | #=================================== | ||
| 70 | sub buffer_size { 0 + @{ shift->_buffer } } | ||
| 71 | #=================================== | ||
| 72 | |||
| 73 | #=================================== | ||
| 74 | sub refill_buffer { | ||
| 75 | #=================================== | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | return 0 if $self->is_finished; | ||
| 79 | |||
| 80 | my $buffer = $self->_buffer; | ||
| 81 | my $scroll_id = $self->_scroll_id | ||
| 82 | || return 0 + @$buffer; | ||
| 83 | |||
| 84 | my $results = $self->scroll_request; | ||
| 85 | |||
| 86 | my $hits = $results->{hits}{hits}; | ||
| 87 | $self->_set_total_took( $self->total_took + $results->{took} ); | ||
| 88 | |||
| 89 | if ( @$hits == 0 ) { | ||
| 90 | $self->_clear_scroll_id; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $self->_set__scroll_id( $results->{_scroll_id} ); | ||
| 94 | push @$buffer, @$hits; | ||
| 95 | } | ||
| 96 | $self->finish if @$buffer == 0; | ||
| 97 | return 0 + @$buffer; | ||
| 98 | } | ||
| 99 | |||
| 100 | #=================================== | ||
| 101 | sub finish { | ||
| 102 | #=================================== | ||
| 103 | my $self = shift; | ||
| 104 | return if $self->is_finished || $self->_pid != $$; | ||
| 105 | |||
| 106 | $self->_set_is_finished(1); | ||
| 107 | @{ $self->_buffer } = (); | ||
| 108 | |||
| 109 | my $scroll_id = $self->_scroll_id or return; | ||
| 110 | $self->_clear_scroll_id; | ||
| 111 | |||
| 112 | my %args = ( 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::7_0::Scroll - A helper module for scrolled searches | ||
| 126 | |||
| 127 | =head1 VERSION | ||
| 128 | |||
| 129 | version 7.30_1 | ||
| 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::7_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::7_0::Direct/search()> and | ||
| 166 | L<scroll()|Search::Elasticsearch::Client::7_0::Direct/scroll()> methods to make | ||
| 167 | them easier to use. | ||
| 168 | |||
| 169 | This class does L<Search::Elasticsearch::Client::7_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::7_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 | %search_params | ||
| 247 | ); | ||
| 248 | |||
| 249 | The L<Search::Elasticsearch::Client::7_0::Direct/scroll_helper()> method loads | ||
| 250 | L<Search::Elasticsearch::Client::7_0::Scroll> class and calls L</new()>, | ||
| 251 | passing in any arguments. | ||
| 252 | |||
| 253 | You can specify a C<scroll> duration (which defaults to C<"1m">). | ||
| 254 | Any other parameters are passed directly to L<Search::Elasticsearch::Client::7_0::Direct/search()>. | ||
| 255 | |||
| 256 | The C<scroll> duration tells Elasticearch how long it should keep the scroll | ||
| 257 | alive. B<Note>: this duration doesn't need to be long enough to process | ||
| 258 | all results, just long enough to process a single B<batch> of results. | ||
| 259 | The expiry gets renewed for another C<scroll> period every time new | ||
| 260 | a new batch of results is retrieved from the cluster. | ||
| 261 | |||
| 262 | By default, the C<scroll_id> is passed as the C<body> to the | ||
| 263 | L<scroll|Search::Elasticsearch::Client::7_0::Direct/scroll()> request. | ||
| 264 | |||
| 265 | The C<scroll> request uses C<GET> by default. To use C<POST> instead, | ||
| 266 | set L<send_get_body_as|Search::Elasticsearch::Transport/send_get_body_as> to | ||
| 267 | C<POST>. | ||
| 268 | |||
| 269 | =head2 C<next()> | ||
| 270 | |||
| 271 | $doc = $scroll->next; | ||
| 272 | @docs = $scroll->next($num); | ||
| 273 | |||
| 274 | The C<next()> method returns the next result, or the next C<$num> results | ||
| 275 | (pulling more results if required). If all results have been exhausted, | ||
| 276 | it returns an empty list. | ||
| 277 | |||
| 278 | =head2 C<drain_buffer()> | ||
| 279 | |||
| 280 | @docs = $scroll->drain_buffer; | ||
| 281 | |||
| 282 | The C<drain_buffer()> method returns all of the documents currently in the | ||
| 283 | buffer, without fetching any more from the cluster. | ||
| 284 | |||
| 285 | =head2 C<refill_buffer()> | ||
| 286 | |||
| 287 | $total = $scroll->refill_buffer; | ||
| 288 | |||
| 289 | The C<refill_buffer()> method fetches the next batch of results from the | ||
| 290 | cluster, stores them in the buffer, and returns the total number of docs | ||
| 291 | currently in the buffer. | ||
| 292 | |||
| 293 | =head2 C<buffer_size()> | ||
| 294 | |||
| 295 | $total = $scroll->buffer_size; | ||
| 296 | |||
| 297 | The C<buffer_size()> method returns the total number of docs currently in | ||
| 298 | the buffer. | ||
| 299 | |||
| 300 | =head2 C<finish()> | ||
| 301 | |||
| 302 | $scroll->finish; | ||
| 303 | |||
| 304 | The C<finish()> method clears out the buffer, sets L</is_finished()> to C<true> | ||
| 305 | and tries to clear the C<scroll_id> on Elasticsearch. This API is only | ||
| 306 | supported since v0.90.6, but the call to C<clear_scroll> is wrapped in an | ||
| 307 | C<eval> so the C<finish()> method can be safely called with any version | ||
| 308 | of Elasticsearch. | ||
| 309 | |||
| 310 | When the C<$scroll> instance goes out of scope, L</finish()> is called | ||
| 311 | automatically if required. | ||
| 312 | |||
| 313 | =head2 C<is_finished()> | ||
| 314 | |||
| 315 | $bool = $scroll->is_finished; | ||
| 316 | |||
| 317 | A flag which returns C<true> if all results have been processed or | ||
| 318 | L</finish()> has been called. | ||
| 319 | |||
| 320 | =head1 INFO ACCESSORS | ||
| 321 | |||
| 322 | The information from the original search is returned via the following | ||
| 323 | accessors: | ||
| 324 | |||
| 325 | =head2 C<total> | ||
| 326 | |||
| 327 | The total number of documents that matched your query. | ||
| 328 | |||
| 329 | =head2 C<max_score> | ||
| 330 | |||
| 331 | The maximum score of any documents in your query. | ||
| 332 | |||
| 333 | =head2 C<aggregations> | ||
| 334 | |||
| 335 | Any aggregations that were specified, or C<undef> | ||
| 336 | |||
| 337 | =head2 C<facets> | ||
| 338 | |||
| 339 | Any facets that were specified, or C<undef> | ||
| 340 | |||
| 341 | =head2 C<suggest> | ||
| 342 | |||
| 343 | Any suggestions that were specified, or C<undef> | ||
| 344 | |||
| 345 | =head2 C<took> | ||
| 346 | |||
| 347 | How long the original search took, in milliseconds | ||
| 348 | |||
| 349 | =head2 C<took_total> | ||
| 350 | |||
| 351 | How long the original search plus all subsequent batches took, in milliseconds. | ||
| 352 | |||
| 353 | =head1 SEE ALSO | ||
| 354 | |||
| 355 | =over | ||
| 356 | |||
| 357 | =item * L<Search::Elasticsearch::Client::7_0::Direct/search()> | ||
| 358 | |||
| 359 | =item * L<Search::Elasticsearch::Client::7_0::Direct/scroll()> | ||
| 360 | |||
| 361 | =item * L<Search::Elasticsearch::Client::7_0::Direct/reindex()> | ||
| 362 | |||
| 363 | =back | ||
| 364 | |||
| 365 | =head1 AUTHOR | ||
| 366 | |||
| 367 | Enrico Zimuel <enrico.zimuel@elastic.co> | ||
| 368 | |||
| 369 | =head1 COPYRIGHT AND LICENSE | ||
| 370 | |||
| 371 | This software is Copyright (c) 2020 by Elasticsearch BV. | ||
| 372 | |||
| 373 | This is free software, licensed under: | ||
| 374 | |||
| 375 | The Apache License, Version 2.0, January 2004 | ||
| 376 | |||
| 377 | =cut | ||
| 378 | |||
| 379 | __END__ |