Search...Scroll(3pm)
π Search::Elasticsearch::Client::7_0::Scroll β bestie edition π
okay so welcome to Search::Elasticsearch::Client::7_0::Scroll, a lil helper module (from the User Contributed Perl Documentation, hi) that makes scrolled searches soooo much easier. version 7.715, no cap. π¦
β¨ VERSION β¨
version 7.715 β yes bestie, that's the one. π
πΈ SYNOPSIS πΈ
use Search::Elasticsearch;
my $es = Search::Elasticsearch->new;
my $scroll = $es->scroll_helper(
index => 'my_index',
body => {
query => {...},
size => 1000,
sort => '_doc'
}
);
say "Total hits: ". $scroll->total;
while (my $doc = $scroll->next) {
# do something
}
like. look at that. it's giving effortless. β¨
π DESCRIPTION π
So a scrolled search is a search that lets you keep pulling results until there are literally no more matching results left. it's basically a cursor in an SQL database but make it Elasticsearch. π
And here's the tea: unlike paginating through results (that's with the from parameter in search()), a scrolled search takes a snapshot of the current state of the index. so even if you keep adding new documents to the index, or updating existing ones, the scrolled search will only ever see the index as it was when the search began. it's frozen in time, main character energy. βοΈπ
This module is a helper utility that wraps the functionality of the search() and scroll() methods to make them easier to use. bestie behaviour, honestly. π«Ά
This class does Search::Elasticsearch::Client::7_0::Role::Scroll and Search::Elasticsearch::Role::Is_Sync. periodt.
π USE CASES π
There are two primary use cases, and they're both lowkey iconic:
π Pulling enough results
Maybe you wanna group your results by some field, and you don't know exactly how many results you'll need to return 10 grouped results. with a scrolled search you can just keep pulling more until you have enough. no stress. for instance, you can search emails in a mailing list and return results grouped by thread_id:
my (%groups,@results);
my $scroll = $es->scroll_helper(
index => 'my_emails',
type => 'email',
body => { query => {... some query ... }}
);
my $doc;
while (@results < 10 and $doc = $scroll->next) {
my $thread = $doc->{_source}{thread_id};
unless ($groups{$thread}) {
$groups{$thread} = [];
push @results, $groups{$thread};
}
push @{$groups{$thread}},$doc;
}
see how it just keeps going until @results < 10 is a no? chef's kiss. π©βπ³π
π¦ Extracting all documents
Often you'll want to extract all (or a subset of) documents in an index. like, if you wanna change your type mappings you're gonna need to reindex all your data. or maybe you wanna move a subset of the data in one index into a new dedicated index. in these cases you don't care about sort order, you just want to retrieve all documents which match a query and do something with them. for instance, to retrieve all the docs for a particular client_id:
my $scroll = $es->scroll_helper(
index => 'my_index',
size => 1000,
body => {
query => {
match => {
client_id => 123
}
},
sort => '_doc'
}
);
while (my $doc = $scroll->next) {
# do something
}
and very often, the something you'll wanna do with these results involves bulk-indexing them into a new index. the easiest way to do that is to use the built-in reindex() in Search::Elasticsearch::Client::7_0::Direct functionality provided by Elasticsearch. slay. π₯
π METHODS π
π new()
use Search::Elasticsearch;
my $es = Search::Elasticsearch->new(...);
my $scroll = $es->scroll_helper(
scroll => '1m', # optional
%search_params
);
The scroll_helper() in Search::Elasticsearch::Client::7_0::Direct method loads the Search::Elasticsearch::Client::7_0::Scroll class and calls new(), passing in any arguments. so it's all handled for you, bestie. π«Ά
You can specify a scroll duration (which defaults to 1m). any other parameters are passed directly to search() in Search::Elasticsearch::Client::7_0::Direct.
The scroll duration tells Elasticearch how long it should keep the scroll alive. > note: this duration doesn't need to be long enough to process all results β just long enough to process a single batch of results. the expiry gets renewed for another scroll period every time a new batch of results is retrieved from the cluster. it's self-renewing, it's giving immortal. β»οΈβ¨
By default, the scroll_id is passed as the body to the scroll request.
The scroll request uses GET by default. to use POST instead, set send_get_body_as to POST.
π next()
$doc = $scroll->next;
@docs = $scroll->next($num);
The next() method returns the next result, or the next $num results (pulling more results if required). if all results have been exhausted, it returns an empty list. that's it, that's the whole vibe. π
π drain_buffer()
@docs = $scroll->drain_buffer;
The drain_buffer() method returns all of the documents currently in the buffer, without fetching any more from the cluster. just emptying the lil bucket you already got. π
π refill_buffer()
$total = $scroll->refill_buffer;
The refill_buffer() method fetches the next batch of results from the cluster, stores them in the buffer, and returns the total number of docs currently in the buffer. top me up bestie. π§
π buffer_size()
$total = $scroll->buffer_size;
The buffer_size() method returns the total number of docs currently in the buffer. a lil peek, no fetching, no drama. π
π finish()
$scroll->finish;
The finish() method clears out the buffer, sets is_finished() to true and tries to clear the scroll_id on Elasticsearch. this API is only supported since v0.90.6, but the call to clear_scroll is wrapped in an eval so the finish() method can be safely called with any version of Elasticsearch. safe, no matter what. πβ‘οΈπ
When the $scroll instance goes out of scope, finish() is called automatically if required. so it cleans up after itself, unlike some people. π
π is_finished()
$bool = $scroll->is_finished;
A flag which returns true if all results have been processed or finish() has been called. a little yes/no bestie. β
π INFO ACCESSORS π
The information from the original search is returned via the following accessors β all the receipts from your original search, right here:
β total
The total number of documents that matched your query.
β max_score
The maximum score of any documents in your query.
β aggregations
Any aggregations that were specified, or undef
β facets
Any facets that were specified, or undef
β suggest
Any suggestions that were specified, or undef
β took
How long the original search took, in milliseconds
β took_total
How long the original search plus all subsequent batches took, in milliseconds.
π SEE ALSO π
search()inSearch::Elasticsearch::Client::7_0::Directscroll()inSearch::Elasticsearch::Client::7_0::Directreindex()inSearch::Elasticsearch::Client::7_0::Direct
go read those too, iykyk. πβ¨
π«Ά AUTHOR π«Ά
Enrico Zimuel <enrico.zimuel@elastic.co> β legend behaviour, honestly. π
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2021 by Elasticsearch BV.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
okay that's the whole page bestie, you ate it up fr fr π
β¨ this was Search...Scroll(3pm), running on perl v5.32.1, dated 2021-10-23 β mwah, bye! ππ¦π
π¦ where this page comes from
- ubuntu jammy: libsearch-elasticsearch-perl 7.715-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms