File Coverage

blib/lib/Catmandu/Store/ElasticSearch/Searcher.pm
Criterion Covered Total %
statement 6 32 18.7
branch 0 12 0.0
condition 0 7 0.0
subroutine 2 6 33.3
pod 0 3 0.0
total 8 60 13.3


line stmt bran cond sub pod time code
1             package Catmandu::Store::ElasticSearch::Searcher;
2              
3 1     1   6 use Catmandu::Sane;
  1         2  
  1         8  
4 1     1   264 use Moo;
  1         2  
  1         7  
5              
6             with 'Catmandu::Iterable';
7              
8             has bag => (is => 'ro', required => 1);
9             has query => (is => 'ro', required => 1);
10             has start => (is => 'ro', required => 1);
11             has limit => (is => 'ro', required => 1);
12             has total => (is => 'ro');
13             has sort => (is => 'ro');
14              
15             sub generator {
16 0     0 0   my ($self) = @_;
17 0           my $limit = $self->limit;
18             sub {
19 0     0     state $total = $self->total;
20 0 0         if (defined $total) {
21 0 0         return unless $total;
22             }
23 0           state $scroller = do {
24 0           my $args = {
25             query => $self->query,
26             type => $self->bag->name,
27             from => $self->start,
28             };
29 0 0         if ($self->sort) {
30 0           $args->{search_type} = 'query_then_fetch';
31 0           $args->{sort} = $self->sort;
32             } else {
33 0           $args->{search_type} = 'scan';
34             }
35 0           $self->bag->store->elastic_search->scrolled_search($args);
36             };
37 0           state @hits;
38 0 0         unless (@hits) {
39 0 0 0       if ($total && $limit > $total) {
40 0           $limit = $total;
41             }
42 0           @hits = $scroller->next($limit);
43             }
44 0 0         if ($total) {
45 0           $total--;
46             }
47 0   0       (shift(@hits) || return)->{_source};
48 0           };
49             }
50              
51             sub slice { # TODO constrain total?
52 0     0 0   my ($self, $start, $total) = @_;
53 0   0       $start //= 0;
54 0           $self->new(
55             bag => $self->bag,
56             query => $self->query,
57             start => $self->start + $start,
58             limit => $self->limit,
59             total => $total,
60             sort => $self->sort,
61             );
62             }
63              
64             sub count {
65 0     0 0   my ($self) = @_;
66 0           $self->bag->store->elastic_search->count(
67             query => $self->query,
68             type => $self->bag->name,
69             )->{count};
70             }
71              
72             1;