Prog/Searcher.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 57 CC 5 SCAR 26.9
CC
5
Cov
0%
init
30.0
search
2.0
BEGIN
2.0
CRAP 14.7
1 package SWISH::Prog::Searcher;
2 use strict;
3 use warnings;
4 use base qw( SWISH::Prog::Class );
5 use Carp;
6 use Scalar::Util qw( blessed );
7  
8 our $VERSION = '0.75';
9  
10 __PACKAGE__->mk_accessors(
11 qw(
12 max_hits
13 invindex
14 qp_config
15 ),
16 );
17  
18 =head1 NAME
19  
20 SWISH::Prog::Searcher - base searcher class
21  
22 =head1 SYNOPSIS
23  
24 my $searcher = SWISH::Prog::Searcher->new(
25 invindex => 'path/to/index',
26 max_hits => 1000,
27 );
28
29 my $results = $searcher->search( 'foo bar' );
30 while (my $result = $results->next) {
31 printf("%4d %s\n", $result->score, $result->uri);
32 }
33  
34 =head1 DESCRIPTION
35  
36 SWISH::Prog::Searcher is a base searcher class. It defines
37 the APIs that all SWISH::Prog storage backends adhere to in
38 returning results from a SWISH::Prog::InvIndex.
39  
40 =head1 METHODS
41  
42 =head2 init
43  
44 Overrides base method.
45  
46 =head2 invindex
47  
48 A SWISH::Prog::InvIndex object or directory path. Required. Set in new().
49  
50 May be a single value or an array ref of values (for searching multiple
51 indexes at once).
52  
53 =head2 max_hits
54  
55 The maximum number of hits to return. Optional. Default is 1000.
56  
57 =head2 qp_config
58  
59 Optional hashref passed to Search::Query::Parser->new().
60  
61 =cut
62  
63 sub init {
64 my $self = shift;
65 $self->SUPER::init(@_);
66  
67 $self->{max_hits} ||= 1000;
68  
69 # set up invindex
70 if ( !$self->{invindex} ) {
71 croak "invindex required";
72 }
73  
74 # force into an array
75 if ( ref $self->{invindex} ne 'ARRAY' ) {
76 $self->{invindex} = [ $self->{invindex} ];
77 }
78  
79 for my $invindex ( @{ $self->{invindex} } ) {
80 if ( !blessed($invindex) ) {
81  
82 # assume a InvIndex in the same namespace as $self
83 my $class = ref($self);
84 $class =~ s/::Searcher$/::InvIndex/;
85 eval "require $class";
86 croak $@ if $@;
87 $invindex = $class->new( path => $invindex, clobber => 0 );
88  
89 #warn "new invindex in $class";
90  
91 }
92 $invindex->open_ro;
93 }
94  
95 return $self;
96 }
97  
98 =head2 search( I<query> )
99  
100 Returns a SWISH::Prog::Results object.
101  
102 =cut
103  
104 sub search {
105 croak "you must override search() in your subclass";
106 }
107  
108 1;
109  
110 __END__