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