File Coverage

blib/lib/Catmandu/Store/ElasticSearch/Searcher.pm
Criterion Covered Total %
statement 6 25 24.0
branch 0 10 0.0
condition 0 4 0.0
subroutine 2 6 33.3
pod 0 3 0.0
total 8 48 16.6


line stmt bran cond sub pod time code
1             package Catmandu::Store::ElasticSearch::Searcher;
2              
3 1     1   4 use Catmandu::Sane;
  1         1  
  1         6  
4 1     1   181 use Moo;
  1         2  
  1         5  
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             sub {
18 0     0     state $total = $self->total;
19 0 0         if (defined $total) {
20 0 0         return unless $total;
21             }
22              
23 0           state $scroll = do {
24 0           my $body = {query => $self->query};
25 0 0         $body->{sort} = $self->sort if $self->sort;
26 0 0         $self->bag->store->es->scroll_helper(
27             index => $self->bag->store->index_name,
28             type => $self->bag->name,
29             search_type => $self->sort ? 'query_then_fetch' : 'scan',
30             from => $self->start,
31             size => $self->bag->buffer_size, # TODO divide by number of shards
32             body => $body,
33             );
34             };
35              
36 0   0       my $data = $scroll->next // return;
37 0 0         if ($total) {
38 0           $total--;
39             }
40 0           $data->{_source};
41 0           };
42             }
43              
44             sub slice { # TODO constrain total?
45 0     0 0   my ($self, $start, $total) = @_;
46 0   0       $start //= 0;
47 0           $self->new(
48             bag => $self->bag,
49             query => $self->query,
50             start => $self->start + $start,
51             limit => $self->limit,
52             total => $total,
53             sort => $self->sort,
54             );
55             }
56              
57             sub count {
58 0     0 0   my ($self) = @_;
59 0           my $store = $self->bag->store;
60 0           $store->es->count(
61             index => $store->index_name,
62             type => $self->bag->name,
63             body => {
64             query => $self->query,
65             },
66             )->{count};
67             }
68              
69             1;