Plucene/Search/IndexSearcher.pm

filter: Statementstmt 92.6% 63 / 68 Branchbran 61.1% 11 / 18 Conditioncond 73.3% 11 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 95.0% 19 / 20 Podpod 100% 7 / 7 totaltotal 83.9% 115 / 137 CC 15 SCAR 11.4
CC
15
Cov
84%
__ANON__
6.6
__ANON__
6.0
new
4.0
CRAP 3.1
« Plucene/Search/BooleanQuery.pm ↑ Summary Plucene/Search/PrefixQuery.pm »
1 package Plucene::Search::IndexSearcher;
2  
3 =head1 NAME
4  
5 Plucene::Search::IndexSearcher - The index searcher
6  
7 =head1 SYNOPSIS
8  
9 # isa Plucene::Search::Searcher
10  
11 my $searcher = Plucene::Search::IndexSearcher
12 ->new(Plucene::Index::Reader $reader);
13  
14 my Plucene::Index::Reader $reader = $searcher->reader;
15 my Plucene::Document $doc = $reader->doc($id);
16  
17 $searcher->close;
18
19 =head1 DESCRIPTION
20  
21 Search over an IndexReader
22  
23 =head1 METHODS
24  
25 =cut
26  
27 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 16 ▶use Bit::Vector::Minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 16 ▶use Plucene::Index::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 16 ▶use Plucene::Search::HitCollector;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 16 ▶use Plucene::Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 16 ▶use Plucene::Search::TopDocs;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 16 ▶use base 'Plucene::Search::Searcher';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 =head2 new
41  
42 my $searcher = Plucene::Search::IndexSearcher
43 ->new(Plucene::Index::Reader $reader);
44  
45 This will create a new Searcher object with the passed Plucene::Index::Reader
46 or subclass thereof.
47
48 =cut
49  
50 190 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
51 190 my ($self, $thing) = @_;
52 190 ▶ if (not ref $thing and -d $thing) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $thing and -d $thing) 189 1 190
Condition66%2/3
not ref $thing and -d $thing
!ll&&!rl&&r
01189
MC/DC50%1/2
not ref $thing and -d $thing
not ref $thing -d $thing
Truth table66%2/3
not(A) and B
A= not ref $thingB= -d $thing
ABresult
0X0
100
111
53 189 $thing = Plucene::Index::Reader->open($thing);
54 }
55 190 ▶ croak "Don't know how to turn $thing into an index reader"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($thing, "Plucene::Index::Reader") 1 189 190
56 unless UNIVERSAL::isa($thing, "Plucene::Index::Reader");
57 189 bless { reader => $thing }, $self;
58 }
59  
60 =head2 reader
61  
62 my Plucene::Index::Reader $reader = $searcher->reader;
63  
64 This will return the reader this searcher was made with.
65  
66 =head2 search_top
67  
68 The top search results.
69  
70 =head2 doc
71  
72 my Plucene::Document $doc = $reader->doc($id);
73  
74 This will return the Plucene::Document $id.
75  
76 =head2 doc_freq / max_doc
77  
78 get / set these
79  
80 =cut
81  
82 222 ▶sub doc_freq { shift->reader->doc_freq(@_) }
Subroutine doc_freq: called CC=1 SCAR=0.0
Pod documented
83 222 ▶sub max_doc { shift->reader->max_doc(@_) }
Subroutine max_doc: called CC=1 SCAR=0.0
Pod documented
84  
85 655 ▶sub reader { shift->{reader} }
Subroutine reader: called CC=1 SCAR=0.0
Pod documented
86  
87 210 ▶sub doc { shift->reader->document(@_); }
Subroutine doc: called CC=1 SCAR=0.0
Pod documented
88  
89 189 ▶sub search_top {
Subroutine search_top: called CC=3 SCAR=11.2
Pod documented
90 189 my ($self, $query, $filter, $n_docs) = @_;
91 189 my $scorer = Plucene::Search::Query->scorer($query, $self, $self->{reader});
92 189 ▶ return Plucene::Search::TopDocs->new({ total_hits => 0, score_docs => [] })
Branch: 1 branch
BranchTrueFalseTotal
unless $scorer 0 189 189
93 unless $scorer;
94 189 ▶ my $bits = $filter && $filter->bits($self->{reader});
Condition66%2/3
$filter && $filter->bits($self->{'reader'})
!ll&&!rl&&r
18801
MC/DC50%1/2
$filter && $filter->bits($self->{'reader'})
$filter $filter->bits($self->{'reader'})
Truth table66%2/3
A && B
A= $filterB= $filter->bits($self->{'reader'})
ABresult
0X0
100
111
95  
96 # This is the hitqueue class, essentially
97 548 ▶ tie my @hq, "Tie::Array::Sorted", sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
98 548 my ($hit_a, $hit_b) = @_;
99 return ($hit_a->{score} <=> $hit_b->{score})
100 548 ▶ || ($hit_b->{doc} <=> $hit_a->{doc});
Condition66%2/3
$hit_a->{'score'} <=> $hit_b->{'score'} || $hit_b->{'doc'} <=> $hit_a->{'doc'}
l!l&&r!l&&!r
1124360
MC/DC0%0/2
$hit_a->{'score'} <=> $hit_b->{'score'} || $hit_b->{'doc'} <=> $hit_a->{'doc'}
$hit_a->{'score'} <=> $hit_b->{'score'} $hit_b->{'doc'} <=> $hit_a->{'doc'}
Truth table66%2/3
A || B
A= $hit_a->{'score'} <=> $hit_b->{'score'}B= $hit_b->{'doc'} <=> $hit_a->{'doc'}
ABresult
1X1
011
000
101 189 };
102 189 my $total_hits = 0; # Dunno why this is an array in Java
103  
104 # This is where it turns ugly
105 $scorer->score(
106 Plucene::Search::HitCollector->new(
107 collect => do {
108 189 my $min_score = 0;
109 363 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.8
110 363 my ($self, $doc, $score) = @_;
111 return
112 363 ▶ if $score == 0
Branch: 1 branch
BranchTrueFalseTotal
if $score == 0 or $bits and not $bits->get($doc) 2 361 363
Condition83%5/6
$score == 0 or $bits and not $bits->get($doc)
l!l&&r!l&&!r
02361
$bits and not $bits->get($doc)
!ll&&!rl&&r
35922
MC/DC66%2/3
$score == 0 or $bits and not $bits->get($doc)
$score == 0 $bits not $bits->get($doc)
Truth table75%3/4
A or B and not(C)
A= $score == 0B= $bitsC= not $bits->get($doc)
ABCresult
1XX1
0111
00X0
0100
113 || ($bits && !$bits->get($doc));
114 361 $total_hits++;
115 361 ▶ if ($score >= $min_score) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score >= $min_score) 361 0 361
116 361 push @hq, { doc => $doc, score => $score };
117 361 ▶ if (@hq > $n_docs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@hq > $n_docs) 0 361 361
118 0 shift @hq;
119 0 $min_score = $hq[0]->{score};
120 }
121 }
122 }
123 189 }
124 ),
125 $self->{reader}->max_doc
126 189 );
127  
128 189 my @array = @hq; # Copy out of tied array
129 189 return Plucene::Search::TopDocs->new({
130 total_hits => $total_hits,
131 score_docs => \@array
132 });
133 }
134  
135 2 ▶sub _search_hc {
Subroutine _search_hc: called CC=3 SCAR=13.6
136 2 my ($self, $query, $filter, $results) = @_;
137 2 my $collector = $results;
138 2 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 0 2 2
139 0 my $bits = $filter->bits($self->{reader});
140 $collector = Plucene::Search::HitCollector->new(
141 0 ▶ collect => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
142 0 ▶ $results->collect(@_) if $bits->get($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $bits->get($_[0]) 0 0 0
143 0 });
144 }
145  
146 2 my $scorer = Plucene::Search::Query->scorer($query, $self, $self->{reader});
147 2 ▶ return unless $scorer;
Branch: 1 branch
BranchTrueFalseTotal
unless $scorer 0 2 2
148 2 $scorer->score($collector, $self->{reader}->max_doc);
149 }
150  
151 =head2 close
152  
153 This will close the reader(s) associated with the searcher.
154  
155 =cut
156  
157 1 ▶sub close { shift->{reader}->close }
Subroutine close: called CC=1 SCAR=0.0
Pod documented
158  
159 16 1;
« Plucene/Search/BooleanQuery.pm ↑ Summary Plucene/Search/PrefixQuery.pm »