File Coverage

blib/lib/Catmandu/Store/Solr/Searcher.pm
Criterion Covered Total %
statement 6 37 16.2
branch 0 10 0.0
condition 0 10 0.0
subroutine 2 6 33.3
pod 0 3 0.0
total 8 66 12.1


line stmt bran cond sub pod time code
1             package Catmandu::Store::Solr::Searcher;
2              
3 3     3   11 use Catmandu::Sane;
  3         5  
  3         14  
4 3     3   415 use Moo;
  3         5  
  3         15  
5              
6             our $VERSION = "0.0302";
7              
8             with 'Catmandu::Iterable';
9              
10             has bag => (is => 'ro', required => 1);
11             has query => (is => 'ro', required => 1);
12             has start => (is => 'ro', required => 1);
13             has limit => (is => 'ro', required => 1);
14             has sort => (is => 'ro', required => 0);
15             has total => (is => 'ro');
16             has fl => (
17             is => 'ro',
18             lazy => 1,
19             default => sub { "*" }
20             );
21              
22             sub generator {
23 0     0 0   my ($self) = @_;
24 0           my $store = $self->bag->store;
25 0           my $name = $self->bag->name;
26 0           my $limit = $self->limit;
27 0           my $query = $self->query;
28 0           my $bag_field = $self->bag->bag_field;
29 0           my $fq = qq/{!type=lucene}$bag_field:"$name"/;
30             sub {
31 0     0     state $start = $self->start;
32 0           state $total = $self->total;
33 0           state $hits;
34 0 0         if (defined $total) {
35 0 0         return unless $total;
36             }
37 0 0 0       unless ($hits && @$hits) {
38 0 0 0       if ( $total && $limit > $total ) {
39 0           $limit = $total;
40             }
41             $hits = $store->solr->search($query, {
42             start => $start,
43             rows => $limit,
44             fq => $fq,
45             sort => $self->sort,
46             fl => $self->fl,
47             facet => "false",
48             spellcheck => "false"
49 0           })->content->{response}{docs};
50 0           $start += $limit;
51             }
52 0 0         if ($total) {
53 0           $total--;
54             }
55 0   0       my $hit = shift(@$hits) || return;
56 0           $self->bag->map_fields($hit);
57 0           $hit;
58 0           };
59             }
60              
61             sub slice {
62 0     0 0   my ($self, $start, $total) = @_;
63 0   0       $start //= 0;
64 0           $self->new(
65             bag => $self->bag,
66             query => $self->query,
67             start => $self->start + $start,
68             limit => $self->limit,
69             sort => $self->sort,
70             total => $total,
71             );
72             }
73              
74             sub count {
75 0     0 0   my ($self) = @_;
76 0           my $name = $self->bag->name;
77 0           my $bag_field = $self->bag->bag_field;
78 0           my $res = $self->bag->store->solr->search(
79             $self->query,
80             {
81             rows => 0,
82             fq => qq/{!type=lucene}$bag_field:"$name"/,
83             facet => "false",
84             spellcheck => "false"
85             }
86             );
87 0           $res->content->{response}{numFound};
88             }
89              
90             1;