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