| 1 | package PGXN::API::Searcher; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use 5.10.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Lucy::Search::QueryParser; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Lucy::Search::IndexSearcher; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Lucy::Highlight::Highlighter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | our $VERSION = v0.10.1; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||
| 14 | 1 | ▶ | my ($class, $path) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 15 | 1 | my %parsers; | |||||||||||||||||||
| 16 | 1 | for my $iname (qw(docs dists extensions users tags)) { | |||||||||||||||||||
| 17 | 5 | my $p = $parsers{$iname} = Lucy::Search::QueryParser->new( | |||||||||||||||||||
| 18 | schema => Lucy::Search::IndexSearcher->new( | ||||||||||||||||||||
| 19 | index => File::Spec->catdir($path, '_index', $iname) | ||||||||||||||||||||
| 20 | )->get_schema, | ||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||
| 22 | 5 | $p->set_heed_colons(1); # XXX Soon to be deprecated. | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | bless { | ||||||||||||||||||||
| 25 | 1 | doc_root => $path, | |||||||||||||||||||
| 26 | parsers => \%parsers, | ||||||||||||||||||||
| 27 | } => $class; | ||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 11 | ▶ | sub doc_root { shift->{doc_root} } | ||||||||||||||||||
|
Subroutine
doc_root: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 31 | 0 | ▶ | sub parsers { shift->{parsers} } | ||||||||||||||||||
|
Subroutine
parsers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | my %highlightable = ( | |||||||||||||||||||
| 34 | docs => 'body', | ||||||||||||||||||||
| 35 | dists => 'readme', | ||||||||||||||||||||
| 36 | extensions => 'abstract', | ||||||||||||||||||||
| 37 | users => 'details', | ||||||||||||||||||||
| 38 | tags => undef, | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 2 | my %fields = ( | |||||||||||||||||||
| 42 | docs => [qw(title abstract dist version docpath date user user_name)], | ||||||||||||||||||||
| 43 | dists => [qw(dist version abstract date user user_name)], | ||||||||||||||||||||
| 44 | extensions => [qw(extension abstract dist version docpath date user user_name)], | ||||||||||||||||||||
| 45 | users => [qw(user name uri)], | ||||||||||||||||||||
| 46 | tags => [qw(tag)], | ||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | sub search { | ||||||||||||||||||||
| 50 | 10 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||
|
Subroutine
search: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 51 | 10 | ▶ | my $iname = $params{in} || 'docs'; | ||||||||||||||||||
|
Condition100%2/2
$params{'in'} || 'docs'
MC/DC100%1/1
$params{'in'} || 'docs'
$params{'in'}
Truth table100%2/2
A
A= $params{'in'}
| |||||||||||||||||||||
| 52 | 10 | my $query = $self->{parsers}{$iname}->parse($params{query}); | |||||||||||||||||||
| 53 | 10 | ▶ | my $limit = ($params{limit} ||= 50) < 1024 ? $params{limit} : 50; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$params{"limit"} ||= 50
MC/DC100%1/1
$params{"limit"} ||= 50
$params{"limit"}
Truth table100%2/2
A
A= $params{"limit"}
| |||||||||||||||||||||
| 54 | 10 | my $searcher = Lucy::Search::IndexSearcher->new( | |||||||||||||||||||
| 55 | index => File::Spec->catdir($self->doc_root, '_index', $iname) | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | my $hits = $searcher->hits( | ||||||||||||||||||||
| 59 | query => $query, | ||||||||||||||||||||
| 60 | offset => $params{offset}, | ||||||||||||||||||||
| 61 | 10 | num_wanted => $limit, | |||||||||||||||||||
| 62 | ); | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Arrange for highlighted excerpts to be created. | ||||||||||||||||||||
| 65 | 10 | my $highlighter; | |||||||||||||||||||
| 66 | 10 | ▶ | if (my $field = $highlightable{$iname}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 10 | my $h = Lucy::Highlight::Highlighter->new( | |||||||||||||||||||
| 68 | searcher => $searcher, | ||||||||||||||||||||
| 69 | query => $query, | ||||||||||||||||||||
| 70 | field => $field, | ||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||
| 72 | $highlighter = sub { | ||||||||||||||||||||
| 73 | 13 | ▶ | return excerpt => $h->create_excerpt(shift); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 74 | 10 | }; | |||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||
| 76 | 0 | ▶ | $highlighter = sub { }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | my %ret = ( | ||||||||||||||||||||
| 80 | query => $params{query}, | ||||||||||||||||||||
| 81 | 10 | ▶ | offset => $params{offset} || 0, | ||||||||||||||||||
|
Condition100%2/2
$params{'offset'} || 0
MC/DC100%1/1
$params{'offset'} || 0
$params{'offset'}
Truth table100%2/2
A
A= $params{'offset'}
| |||||||||||||||||||||
| 82 | limit => $limit, | ||||||||||||||||||||
| 83 | count => $hits->total_hits, | ||||||||||||||||||||
| 84 | hits => my $res = [], | ||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # Create result list. | ||||||||||||||||||||
| 88 | 10 | ▶ | while ( my $hit = $hits->next ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 13 | push @{ $res } => { | |||||||||||||||||||
| 90 | score => sprintf( "%0.3f", $hit->get_score ), | ||||||||||||||||||||
| 91 | $highlighter->($hit), | ||||||||||||||||||||
| 92 | 13 | map { $_ => $hit->{$_} } @{ $fields{$iname} } | |||||||||||||||||||
| 93 | }; | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 10 | return \%ret; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | 1; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||