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