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