blib/lib/KinoSearch1/Search/Hits.pm

filter: Statementstmt 100% 58 / 58 Branchbran 77.2% 17 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 93.2% 97 / 104 CC 13 SCAR 10.8
CC
13
Cov
93%
fetch_hit_hashref
6.0
init_instance
3.1
fetch_hit
3.0
CRAP 2.9
« blib/lib/KinoSearch1/Analysis/Tokenizer.pm ↑ Summary blib/lib/KinoSearch1/Index/FieldsReader.pm »
1 package KinoSearch1::Search::Hits;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use KinoSearch1::Util::ToolSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use base qw( KinoSearch1::Util::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 __PACKAGE__->init_instance_vars(
9 # params/members
10 searcher => undef,
11 query => undef,
12 filter => undef,
13 sort_spec => undef,
14 num_docs => undef,
15  
16 # members
17 weight => undef,
18 highlighter => undef,
19  
20 hit_docs => undef,
21 total_hits => undef,
22 );
23  
24 18 __PACKAGE__->ready_get(qw( hit_docs ));
25 }
26  
27 18 ▶use KinoSearch1::Highlight::Highlighter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶use KinoSearch1::Search::HitCollector;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 280 ▶sub init_instance {
Subroutine init_instance: called CC=3 SCAR=11.4
Pod documented
31 280 my $self = shift;
32  
33 croak("required parameter 'query' not supplied")
34 280 ▶ unless $self->{query};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"query"} 0 280 280
35 croak("required parameter 'searcher' not supplied")
36 280 ▶ unless $self->{searcher};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"searcher"} 0 280 280
37  
38 # turn the Query into a Weight (so the Query won't get mussed)
39 280 $self->{weight} = $self->{searcher}->create_weight( $self->{query} );
40 }
41  
42 281 ▶sub seek {
Subroutine seek: called CC=2 SCAR=7.0
Pod documented
43 281 my ( $self, $start_offset, $num_wanted ) = @_;
44 281 ▶ croak('Usage: $hits->seek( START, NUM_TO_RETRIEVE );')
Branch: 1 branch
BranchTrueFalseTotal
unless @_ = 3 0 281 281
45 unless @_ = 3;
46  
47 # collect enough to satisfy both the offset and the num wanted
48 281 my $collector = KinoSearch1::Search::HitQueueCollector->new(
49 size => $num_wanted + $start_offset, );
50  
51 # execute the search!
52 $self->{searcher}->search_hit_collector(
53 hit_collector => $collector,
54 weight => $self->{weight},
55 filter => $self->{filter},
56 sort_spec => $self->{sort_spec},
57 281 );
58 281 $self->{total_hits} = $collector->get_total_hits;
59 281 my $hit_queue = $collector->get_hit_queue;
60  
61 # turn the HitQueue into an array of Hit objects
62 $self->{hit_docs}
63 281 = $hit_queue->hits( $start_offset, $num_wanted, $self->{searcher} );
64  
65 }
66  
67 271 ▶sub total_hits {
Subroutine total_hits: called CC=2 SCAR=6.9
Pod documented
68 271 my $self = shift;
69 $self->seek( 0, 100 )
70 271 ▶ unless defined $self->{total_hits};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"total_hits"} 29 242 271
71 271 return $self->{total_hits};
72 }
73  
74 4 ▶sub fetch_hit {
Subroutine fetch_hit: called CC=3 SCAR=11.0
Pod documented
75 4 my $self = shift;
76 $self->seek( 0, 100 )
77 4 ▶ unless defined $self->{total_hits};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"total_hits"} 1 3 4
78  
79 4 my $hit = shift @{ $self->{hit_docs} };
80 4 ▶ return unless defined $hit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hit 1 3 4
81 3 return $hit;
82 }
83  
84 44 ▶sub fetch_hit_hashref {
Subroutine fetch_hit_hashref: called CC=6 SCAR=18.0
Pod documented
85 44 my $self = shift;
86 $self->seek( 0, 100 )
87 44 ▶ unless defined $self->{total_hits};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"total_hits"} 2 42 44
88  
89 # bail if there aren't any more *captured* hits
90 44 my $hit = shift @{ $self->{hit_docs} };
91 44 ▶ return unless defined $hit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hit 1 43 44
92  
93 # lazily fetch stored fields
94 43 my $hashref = $hit->get_field_values;
95  
96 43 ▶ if ( !exists $hashref->{score} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hashref->{"score"}) 43 0 43
97 43 $hashref->{score} = $hit->get_score;
98 }
99 43 ▶ if ( defined $self->{highlighter} and !exists $hashref->{excerpt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"highlighter"} and not exists $hashref->{"excerpt"}) 1 42 43
Condition66%2/3
defined $self->{"highlighter"} and not exists $hashref->{"excerpt"}
!ll&&!rl&&r
4201
MC/DC50%1/2
defined $self->{"highlighter"} and not exists $hashref->{"excerpt"}
defined $self->{"highlighter"} not exists $hashref->{"excerpt"}
Truth table66%2/3
A and not(B)
A= defined $self->{"highlighter"}B= not exists $hashref->{"excerpt"}
ABresult
0X0
100
111
100 $hashref->{excerpt}
101 1 = $self->{highlighter}->generate_excerpt( $hit->get_doc );
102 }
103  
104 42 return $hashref;
105 }
106  
107 18 my %create_excerpts_defaults = ( highlighter => undef, );
108  
109 1 ▶sub create_excerpts {
Subroutine create_excerpts: called CC=2 SCAR=7.0
Pod documented
110 1 my $self = shift;
111 1 ▶ confess kerror() unless verify_args( \%create_excerpts_defaults, @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless verify_args(\%create_excerpts_defaults, @_) 0 1 1
112 1 my %args = ( %create_excerpts_defaults, @_ );
113  
114 1 $self->{highlighter} = $args{highlighter};
115 1 $self->{highlighter}->set_terms( [ $self->{query}->extract_terms ] );
116 }
117  
118 18 1;
119  
120 =head1 NAME
121  
122 KinoSearch1::Search::Hits - access search results
123  
124 =head1 SYNOPSIS
125  
126 my $hits = $searcher->search( query => $query );
127 $hits->seek( 0, 10 );
128 while ( my $hashref = $hits->fetch_hit_hashref ) {
129 print "<p>$hashref->{title} <em>$hashref->{score}</em></p>\n";
130 }
131  
132 =head1 DESCRIPTION
133  
134 Hits objects are used to access the results of a search. By default, a hits
135 object provides access to the top 100 matches; the seek() method provides
136 finer-grained control.
137  
138 A classic application would be paging through hits. The first time, seek to a
139 START of 0, and retrieve 10 documents. If the user wants to see more -- and
140 there are more than 10 total hits -- seek to a START of 10, and retrieve 10
141 more documents. And so on.
142  
143 =head1 METHODS
144  
145 =head2 seek
146  
147 $hits->seek( START, NUM_TO_RETRIEVE );
148  
149 Position the Hits iterator at START, and capture NUM_TO_RETRIEVE docs.
150  
151 =head2 total_hits
152  
153 my $num_that_matched = $hits->total_hits;
154  
155 Return the total number of documents which matched the query used to produce
156 the Hits object. (This number is unlikely to match NUM_TO_RETRIEVE.)
157  
158 =head2 fetch_hit
159  
160 while ( my $hit = $hits->fetch_hit ) {
161 # ...
162 }
163  
164 Return the next hit as a KinoSearch1::Search::Hit object.
165  
166 =head2 fetch_hit_hashref
167  
168 while ( my $hashref = $hits->fetch_hit_hashref ) {
169 # ...
170 }
171  
172 Return the next hit as a hashref, with the field names as keys and the field
173 values as values. An entry for C<score> will also be present, as will an
174 entry for C<excerpt> if create_excerpts() was called earlier. However, if the
175 document contains stored fields named "score" or "excerpt", they will not be
176 clobbered.
177  
178 =head2 create_excerpts
179  
180 my $highlighter = KinoSearch1::Highlight::Highlighter->new(
181 excerpt_field => 'bodytext',
182 );
183 $hits->create_excerpts( highlighter => $highlighter );
184  
185 Use the supplied highlighter to generate excerpts. See
186 L<KinoSearch1::Highlight::Highlighter|KinoSearch1::Highlight::Highlighter>.
187  
188 =head1 COPYRIGHT
189  
190 Copyright 2005-2010 Marvin Humphrey
191  
192 =head1 LICENSE, DISCLAIMER, BUGS, etc.
193  
194 See L<KinoSearch1> version 1.01.
195  
196 =cut
197  
« blib/lib/KinoSearch1/Analysis/Tokenizer.pm ↑ Summary blib/lib/KinoSearch1/Index/FieldsReader.pm »