Plucene/Search/Hits.pm

filter: Statementstmt 90.7% 49 / 54 Branchbran 70.0% 7 / 10 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 84.2% 75 / 89 CC 9 SCAR 12.4
CC
9
Cov
84%
_get_more_hits
6.1
doc
6.0
hit_doc
3.0
CRAP 3.5
« Plucene/Index/SegmentTermDocs.pm ↑ Summary Plucene/Analysis/CharTokenizer.pm »
1 package Plucene::Search::Hits;
2  
3 =head1 NAME
4  
5 Plucene::Search::Hits - A list of ranked documents
6  
7 =head1 SYNOPSIS
8  
9 my $hits = Plucene::Search::Hits->new;
10  
11 my $doc = $hits->doc($n);
12 my $score = $hits->score($n);
13 my $hit_doc = $hits->hit_doc($n);
14
15 =head1 DESCRIPTION
16  
17 This is a list of ranked documents, used to hold search results.
18  
19 =head1 METHODS
20  
21 =cut
22  
23 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 16 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 16 ▶use Plucene::Search::TopDocs;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 16 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 __PACKAGE__->mk_accessors(
32 qw/ query searcher filter length hit_docs
33 first last num_docs max_docs /
34 );
35  
36 =head2 new
37  
38 my $hits = Plucene::Search::Hits->new;
39  
40 =head2 query / searcher / filter / length / hit_docs / first /
41 last / num_docs / max_docs
42  
43 Get / set these attributes.
44  
45 =cut
46  
47 189 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
48 189 my $self = shift->SUPER::new(@_);
49 189 $self->num_docs(0);
50 189 $self->max_docs(200);
51 189 $self->hit_docs([]);
52 189 $self->_get_more_hits(50);
53 189 return $self;
54 }
55  
56 189 ▶sub _get_more_hits {
Subroutine _get_more_hits: called CC=6 SCAR=18.2
57 189 my ($self, $min) = @_;
58 0 ▶ if (@{ $self->{hit_docs} } > $min) { $min = @{ $self->{hit_docs} }; }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"hit_docs"};} > $min) 0 189 189
59 189 my $n = $min * 2;
60 189 my $top_docs = $self->searcher->search_top($self->query, $self->filter, $n);
61 189 $self->length($top_docs->total_hits);
62  
63 189 my @score_docs = $top_docs->score_docs;
64  
65 189 my $score_norm = 1.0;
66 $score_norm = 1 / $score_docs[0]->{score}
67 if $self->length > 0
68 189 ▶ and $score_docs[0]->{score} > 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->length > 0 and $score_docs[0]{"score"} > 1 6 183 189
Condition100%3/3
$self->length > 0 and $score_docs[0]{"score"} > 1
!ll&&!rl&&r
61776
MC/DC100%2/2
$self->length > 0 and $score_docs[0]{"score"} > 1
$self->length > 0 $score_docs[0]{"score"} > 1
Truth table100%3/3
A and B
A= $self->length > 0B= $score_docs[0]{"score"} > 1
ABresult
0X0
100
111
69  
70 189 ▶ my $end = $#score_docs < $self->length ? $#score_docs : $self->length;
Branch: 1 branch
BranchTrueFalseTotal
$#score_docs < $self->length ? : 189 0 189
71 189 for my $score_doc (@score_docs[ @{ $self->{hit_docs} } .. $end ]) {
72 361 push @{ $self->{hit_docs} },
73 Plucene::Search::HitDoc->new({
74 score => $score_doc->{score} * $score_norm,
75 id => $score_doc->{doc},
76 361 });
77 }
78 }
79  
80 =head2 doc
81  
82 my $doc = $hits->doc($n);
83  
84 Returns the nth document.
85
86 =cut
87  
88 0 ▶sub doc {
Subroutine doc: not called CC=2 SCAR=17.9
Pod documented
89 0 my ($self, $n) = @_;
90 0 my $hit = $self->hit_doc($n);
91  
92 # Not sure we need the LRU for now
93  
94 0 ▶ return $hit->doc || $hit->doc($self->searcher->doc($hit->id));
Condition0%0/3
$hit->doc || $hit->doc($self->searcher->doc($hit->id))
l!l&&r!l&&!r
000
MC/DC0%0/2
$hit->doc || $hit->doc($self->searcher->doc($hit->id))
$hit->doc $hit->doc($self->searcher->doc($hit->id))
Truth table0%0/3
A || B
A= $hit->docB= $hit->doc($self->searcher->doc($hit->id))
ABresult
1X1
011
000
95 }
96  
97 =head2 score
98  
99 my $score = $hits->score($n);
100  
101 The score of the nth document.
102
103 =cut
104  
105 1 ▶sub score {
Subroutine score: called CC=1 SCAR=0.0
Pod documented
106 1 my ($self, $n) = @_;
107 1 return $self->hit_doc($n)->score;
108 }
109  
110 =head2 hit_doc
111  
112 my $hit_doc = $hits->hit_doc($n);
113  
114 Returns the nth hit document.
115  
116 =cut
117  
118 2 ▶sub hit_doc {
Subroutine hit_doc: called CC=3 SCAR=11.0
Pod documented
119 2 my ($self, $n) = @_;
120 2 ▶ if ($n >= $self->length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $self->length) 1 1 2
121 1 croak("Not a valid hit number: $n");
122 }
123 1 ▶ $self->_get_more_hits($n) if $n >= @{ $self->{hit_docs} };
Branch: 1 branch
BranchTrueFalseTotal
if $n >= @{$self->{"hit_docs"};} 0 1 1
124 1 return $self->{hit_docs}[$n];
125 }
126  
127 package Plucene::Search::HitDoc;
128  
129 16 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 16 __PACKAGE__->mk_accessors(qw/score id doc/);
132  
133 16 1;
« Plucene/Index/SegmentTermDocs.pm ↑ Summary Plucene/Analysis/CharTokenizer.pm »