| 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::8_0::Scroll; | ||
| 19 | $Search::Elasticsearch::Client::8_0::Scroll::VERSION = '8.12'; | ||
| 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::8_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 | my $results = $es->search($params); | ||
| 37 | |||
| 38 | my $total = $results->{hits}{total}; | ||
| 39 | if (ref $total) { | ||
| 40 | $total = $total->{value} | ||
| 41 | } | ||
| 42 | |||
| 43 | return { | ||
| 44 | es => $es, | ||
| 45 | scroll => $scroll, | ||
| 46 | aggregations => $results->{aggregations}, | ||
| 47 | facets => $results->{facets}, | ||
| 48 | suggest => $results->{suggest}, | ||
| 49 | took => $results->{took}, | ||
| 50 | total_took => $results->{took}, | ||
| 51 | total => $total, | ||
| 52 | max_score => $results->{hits}{max_score}, | ||
| 53 | _buffer => $results->{hits}{hits}, | ||
| 54 | $total | ||
| 55 | ? ( _scroll_id => $results->{_scroll_id} ) | ||
| 56 | : ( is_finished => 1 ) | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | #=================================== | ||
| 61 | sub next { | ||
| 62 | #=================================== | ||
| 63 | my ( $self, $n ) = @_; | ||
| 64 | $n ||= 1; | ||
| 65 | while ( $self->_has_scroll_id and $self->buffer_size < $n ) { | ||
| 66 | $self->refill_buffer; | ||
| 67 | } | ||
| 68 | my @return = splice( @{ $self->_buffer }, 0, $n ); | ||
| 69 | $self->finish if @return < $n; | ||
| 70 | return wantarray ? @return : $return[-1]; | ||
| 71 | } | ||
| 72 | |||
| 73 | #=================================== | ||
| 74 | sub drain_buffer { | ||
| 75 | #=================================== | ||
| 76 | my $self = shift; | ||
| 77 | return splice( @{ $self->_buffer } ); | ||
| 78 | } | ||
| 79 | |||
| 80 | #=================================== | ||
| 81 | sub buffer_size { 0 + @{ shift->_buffer } } | ||
| 82 | #=================================== | ||
| 83 | |||
| 84 | #=================================== | ||
| 85 | sub refill_buffer { | ||
| 86 | #=================================== | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | return 0 if $self->is_finished; | ||
| 90 | |||
| 91 | my $buffer = $self->_buffer; | ||
| 92 | my $scroll_id = $self->_scroll_id | ||
| 93 | || return 0 + @$buffer; | ||
| 94 | |||
| 95 | my $results = $self->scroll_request; | ||
| 96 | |||
| 97 | my $hits = $results->{hits}{hits}; | ||
| 98 | $self->_set_total_took( $self->total_took + $results->{took} ); | ||
| 99 | |||
| 100 | if ( @$hits == 0 ) { | ||
| 101 | $self->_clear_scroll_id; | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | $self->_set__scroll_id( $results->{_scroll_id} ); | ||
| 105 | push @$buffer, @$hits; | ||
| 106 | } | ||
| 107 | $self->finish if @$buffer == 0; | ||
| 108 | return 0 + @$buffer; | ||
| 109 | } | ||
| 110 | |||
| 111 | #=================================== | ||
| 112 | sub finish { | ||
| 113 | #=================================== | ||
| 114 | my $self = shift; | ||
| 115 | return if $self->is_finished || $self->_pid != $$; | ||
| 116 | |||
| 117 | $self->_set_is_finished(1); | ||
| 118 | @{ $self->_buffer } = (); | ||
| 119 | |||
| 120 | my $scroll_id = $self->_scroll_id or return; | ||
| 121 | $self->_clear_scroll_id; | ||
| 122 | |||
| 123 | my %args = ( body => { scroll_id => $scroll_id } ); | ||
| 124 | eval { $self->es->clear_scroll(%args) }; | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; | ||
| 129 | |||
| 130 | =pod | ||
| 131 | |||
| 132 | =encoding UTF-8 | ||
| 133 | |||
| 134 | =head1 NAME | ||
| 135 | |||
| 136 | Search::Elasticsearch::Client::8_0::Scroll - A helper module for scrolled searches | ||
| 137 | |||
| 138 | =head1 VERSION | ||
| 139 | |||
| 140 | version 8.12 | ||
| 141 | |||
| 142 | =head1 SYNOPSIS | ||
| 143 | |||
| 144 | use Search::Elasticsearch; | ||
| 145 | |||
| 146 | my $es = Search::Elasticsearch->new; | ||
| 147 | |||
| 148 | my $scroll = $es->scroll_helper( | ||
| 149 | index => 'my_index', | ||
| 150 | body => { | ||
| 151 | query => {...}, | ||
| 152 | size => 1000, | ||
| 153 | sort => '_doc' | ||
| 154 | } | ||
| 155 | ); | ||
| 156 | |||
| 157 | say "Total hits: ". $scroll->total; | ||
| 158 | |||
| 159 | while (my $doc = $scroll->next) { | ||
| 160 | # do something | ||
| 161 | } | ||
| 162 | |||
| 163 | =head1 DESCRIPTION | ||
| 164 | |||
| 165 | A I<scrolled search> is a search that allows you to keep pulling results | ||
| 166 | until there are no more matching results, much like a cursor in an SQL | ||
| 167 | database. | ||
| 168 | |||
| 169 | Unlike paginating through results (with the C<from> parameter in | ||
| 170 | L<search()|Search::Elasticsearch::Client::8_0::Direct/search()>), | ||
| 171 | scrolled searches take a snapshot of the current state of the index. Even | ||
| 172 | if you keep adding new documents to the index or updating existing documents, | ||
| 173 | a scrolled search will only see the index as it was when the search began. | ||
| 174 | |||
| 175 | This module is a helper utility that wraps the functionality of the | ||
| 176 | L<search()|Search::Elasticsearch::Client::8_0::Direct/search()> and | ||
| 177 | L<scroll()|Search::Elasticsearch::Client::8_0::Direct/scroll()> methods to make | ||
| 178 | them easier to use. | ||
| 179 | |||
| 180 | This class does L<Search::Elasticsearch::Client::8_0::Role::Scroll> and | ||
| 181 | L<Search::Elasticsearch::Role::Is_Sync>. | ||
| 182 | |||
| 183 | =head1 USE CASES | ||
| 184 | |||
| 185 | There are two primary use cases: | ||
| 186 | |||
| 187 | =head2 Pulling enough results | ||
| 188 | |||
| 189 | Perhaps you want to group your results by some field, and you don't know | ||
| 190 | exactly how many results you will need in order to return 10 grouped | ||
| 191 | results. With a scrolled search you can keep pulling more results | ||
| 192 | until you have enough. For instance, you can search emails in a mailing | ||
| 193 | list, and return results grouped by C<thread_id>: | ||
| 194 | |||
| 195 | my (%groups,@results); | ||
| 196 | |||
| 197 | my $scroll = $es->scroll_helper( | ||
| 198 | index => 'my_emails', | ||
| 199 | type => 'email', | ||
| 200 | body => { query => {... some query ... }} | ||
| 201 | ); | ||
| 202 | |||
| 203 | my $doc; | ||
| 204 | while (@results < 10 and $doc = $scroll->next) { | ||
| 205 | |||
| 206 | my $thread = $doc->{_source}{thread_id}; | ||
| 207 | |||
| 208 | unless ($groups{$thread}) { | ||
| 209 | $groups{$thread} = []; | ||
| 210 | push @results, $groups{$thread}; | ||
| 211 | } | ||
| 212 | push @{$groups{$thread}},$doc; | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | =head2 Extracting all documents | ||
| 217 | |||
| 218 | Often you will want to extract all (or a subset of) documents in an index. | ||
| 219 | If you want to change your type mappings, you will need to reindex all of your | ||
| 220 | data. Or perhaps you want to move a subset of the data in one index into | ||
| 221 | a new dedicated index. In these cases, you don't care about sort | ||
| 222 | order, you just want to retrieve all documents which match a query, and do | ||
| 223 | something with them. For instance, to retrieve all the docs for a particular | ||
| 224 | C<client_id>: | ||
| 225 | |||
| 226 | my $scroll = $es->scroll_helper( | ||
| 227 | index => 'my_index', | ||
| 228 | size => 1000, | ||
| 229 | body => { | ||
| 230 | query => { | ||
| 231 | match => { | ||
| 232 | client_id => 123 | ||
| 233 | } | ||
| 234 | }, | ||
| 235 | sort => '_doc' | ||
| 236 | } | ||
| 237 | ); | ||
| 238 | |||
| 239 | while (my $doc = $scroll->next) { | ||
| 240 | # do something | ||
| 241 | } | ||
| 242 | |||
| 243 | Very often the I<something> that you will want to do with these results | ||
| 244 | involves bulk-indexing them into a new index. The easiest way to | ||
| 245 | do this is to use the built-in L<Search::Elasticsearch::Client::8_0::Direct/reindex()> | ||
| 246 | functionality provided by Elasticsearch. | ||
| 247 | |||
| 248 | =head1 METHODS | ||
| 249 | |||
| 250 | =head2 C<new()> | ||
| 251 | |||
| 252 | use Search::Elasticsearch; | ||
| 253 | |||
| 254 | my $es = Search::Elasticsearch->new(...); | ||
| 255 | my $scroll = $es->scroll_helper( | ||
| 256 | scroll => '1m', # optional | ||
| 257 | %search_params | ||
| 258 | ); | ||
| 259 | |||
| 260 | The L<Search::Elasticsearch::Client::8_0::Direct/scroll_helper()> method loads | ||
| 261 | L<Search::Elasticsearch::Client::8_0::Scroll> class and calls L</new()>, | ||
| 262 | passing in any arguments. | ||
| 263 | |||
| 264 | You can specify a C<scroll> duration (which defaults to C<"1m">). | ||
| 265 | Any other parameters are passed directly to L<Search::Elasticsearch::Client::8_0::Direct/search()>. | ||
| 266 | |||
| 267 | The C<scroll> duration tells Elasticearch how long it should keep the scroll | ||
| 268 | alive. B<Note>: this duration doesn't need to be long enough to process | ||
| 269 | all results, just long enough to process a single B<batch> of results. | ||
| 270 | The expiry gets renewed for another C<scroll> period every time new | ||
| 271 | a new batch of results is retrieved from the cluster. | ||
| 272 | |||
| 273 | By default, the C<scroll_id> is passed as the C<body> to the | ||
| 274 | L<scroll|Search::Elasticsearch::Client::8_0::Direct/scroll()> request. | ||
| 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.6, 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::8_0::Direct/search()> | ||
| 369 | |||
| 370 | =item * L<Search::Elasticsearch::Client::8_0::Direct/scroll()> | ||
| 371 | |||
| 372 | =item * L<Search::Elasticsearch::Client::8_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) 2024 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__ |