Elastic::Model::Results::Scrolled - An iterator over unbounded search results
version 0.27
All active users:
$all_users = $model->view ->index ( 'my_domain' ) ->type ( 'user' ) ->filterb( 'status' => 'active' ) ->size ( 100 ) ->scan while ( my $user = $users->next ) { say $user->name; }
An Elastic::Model::Results::Scrolled object is returned when you call "scroll()" in Elastic::Model::View or "scan()" in Elastic::Model::View, and is intended for searches that could potentially retrieve many results. Results are retrieved from Elasticsearch in chunks.
A $results object can iterate through Elastic::Model::Result objects (with all the result metadata), or just the DocClass object itself (eg MyApp::User). For instance, you can do:
$results
MyApp::User
$result = $results->next_result; $object = $results->next_object;
Or you can set the default type to return:
$results->as_objects; $object = $results->next; $results->as_results; $result = $results->next;
By default, scroll() will set the short accessors to return Elastic::Model::Result objects, and scan() will set them to default to the original objects.
Most attributes and accessors in this class come from Elastic::Model::Role::Results and Elastic::Model::Role::Iterator.
Also, see Elastic::Manual::Searching.
$size = $results->size;
Initially the same as the "total" attribute, as you can potentially retrieve all matching results. (This is different from the "size" in Elastic::Model::Results.) If you use "shift", the "size" will decrease, while the "total" will remain the same.
$total_matching = $results->total
The total number of matching docs found by Elasticsearch.
$max_score = $results->max_score
The highest score (relevance) found by Elasticsearch. Note: if you are sorting by a field other than _score then you will need to set "track_scores" in Elastic::Model::View to true to retrieve the "max_score".
_score
$facets = $results->facets $facet = $results->facet($facet_name)
Facet results, if any were requested with "facets" in Elastic::Model::View.
\@elements = $results->elements;
An array ref containing all of the data structures that we can iterate over.
\%search_args = $results->search
Contains the hash ref of the search request passed to "scrolled_search()" in Elastic::Model::Role::Store
$index = $results->index; # index of the current element, or undef $results->index(0); # set the current element to the first element $results->index(-1); # set the current element to the last element $results->index(undef); # resets the iterator, no current element
"index" contains the current index of the iterator. Before you start iterating, it will return undef.
$results->reset;
Resets the iterator so that the next call to "next" will return the first element. Note: any calls to "shift" means that those elements have been discarded. "reset" will not reload these.
Returns the number of "elements".
$bool = $results->even
Is the current "index" even?
$bool = $results->odd
Is the current "index" odd?
$parity = $results->parity
Returns 'odd' or 'even'. Useful for alternating the colour of rows:
'odd'
'even'
while ( my $el = $results->next ) { my $css_class = $el->parity; # display row }
$bool = $results->is_first
Is the "current" element the first element?
$bool = $results->is_last
Is the "current" element the last element?
$bool = $results->has_next
Is there a "next" element?
$bool = $results->has_prev
Is there a "prev" element?
$results = $results->as_results;
Sets the "short" accessors (eg "next", "prev") to return Elastic::Model::Result objects.
$objects = $objects->as_objects;
Sets the "short" accessors (eg "next", "prev") to return the object itself, eg MyApp::User
$results->as_elements()
Sets the "short" accessors (eg "next", "prev") to return the raw result returned by Elasticsearch.
All of the accessors below have 4 forms:
Result, eg next_result which returns the full result metadata as an Elastic::Model::Result object.
next_result
Object, eg next_object which returns the original matching object, eg an instance of MyApp::User
next_object
Element, eg next_element which returns the raw hashref from Elasticsearch
next_element
Short, which can return any one of the above, depending on which Wrapper is currently in effect.
Typically you would select the type that you need, then use the short accessors, eg:
$results->as_objects; while (my $object = $result->next ) {...}
$el = $results->first
Returns the first element, and resets the iterator so that a call to "next" will return the second element. If there is no first element, it returns undef.
Also first_result, first_object, first_element
first_result
first_object
first_element
$el = $results->next;
Returns the next element, and advances the iterator by one. If there is no next element, it returns undef. If the next element is the last element, then it will work like this:
$results->next; # returns last element $results->next; # returns undef, and resets iterator $results->next; # returns first element
Also next_result, next_object, next_element
$el = $results->prev
Returns the previous element, and moves the iterator one step in reverse. If there is no previous element, it returns undef. If the previous element is the first element, then it will work like this:
$results->prev; # returns prev element $results->prev; # returns undef, and resets iterator to end $results->prev; # returns last element
Also prev_result, prev_object, prev_element
prev_result
prev_object
prev_element
$el = $results->current
Returns the current element, or undef
Also current_result, current_object, current_element
current_result
current_object
current_element
$el = $results->last
Returns the last element, and resets the iterator so that a call to "next" will return undef, and a second call to "next" will return the first element If there is no last element, it returns undef.
Also last_result, last_object, last_element
last_result
last_object
last_element
$el = $results->peek_next
Returns the next element (or undef), but doesn't move the iterator.
Also peek_next_result, peek_next_object, peek_next_element
peek_next_result
peek_next_object
peek_next_element
$el = $results->peek_prev
Returns the previous element (or undef), but doesn't move the iterator.
Also peek_prev_result, peek_prev_object, peek_prev_element
peek_prev_result
peek_prev_object
peek_prev_element
$el = $results->shift
Returns the "first" element and removes it from from the list. "size" will decrease by 1. Returns undef if there are no more elements.
Also shift_result, shift_object, shift_element
shift_result
shift_object
shift_element
@els = $results->slice($offset,$length);
Returns a list of (max) $length elements, starting at $offset (which is zero-based):
$length
$offset
$results->slice(); # all elements; $results->slice(5); # elements 5..size $results->slice(-5); # elements size-5..size $results->slice(0,10); # elements 0..9 $results->slice(5,10); # elements 5..14
If your iterator only contains 5 elements:
$results->slice(3,10); # elements 3..4 $results->slice(10,10); # an empty list
Also slice_results, slice_objects, slice_elements
slice_results
slice_objects
slice_elements
@els = $results->all
Returns all "elements" as a list.
Also all_results, all_objects, all_elements
all_results
all_objects
all_elements
Clinton Gormley <drtech@cpan.org>
This software is copyright (c) 2013 by Clinton Gormley.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Elastic::Model, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Elastic::Model
CPAN shell
perl -MCPAN -e shell install Elastic::Model
For more information on module installation, please visit the detailed CPAN module installation guide.