Solr.pm

filter: Statementstmt 19.5% 99 / 507 Branchbran 4.0% 8 / 196 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 22.7% 18 / 79 Podpod 100% 20 / 20 totaltotal 17.0% 145 / 850 CC 127 SCAR 37.9
CC
127
Cov
15%
_parse_url
182.0
paginator
156.0
execute_query
110.0
CRAP 44.1
↑ Summary Solr/UpdateIndex.pm »
1 package Interchange::Search::Solr;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use WebService::Solr;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use WebService::Solr::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use POSIX qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Encode qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Interchange::Search::Solr::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Interchange::Search::Solr::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Lingua::StopWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Types::Standard qw/ArrayRef HashRef Int Bool/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use constant { DEBUG => 0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 Interchange::Search::Solr -- Solr query encapsulation
25  
26 =head1 VERSION
27  
28 Version 0.14
29  
30 =cut
31  
32 13 our $VERSION = '0.14';
33  
34 =head1 DESCRIPTION
35  
36 Exposes Solr search API in a programmer-friendly way.
37  
38 =head1 SYNOPSIS
39  
40 Quick summary of what the module does.
41  
42 Perhaps a little code snippet.
43  
44 use Interchange::Search::Solr;
45 my $solr = Interchange::Search::Solr->new(solr_url => $url);
46 $solr->rows(10);
47 $solr->start(0);
48 $solr->search('shirts');
49 $results = $solr->results;
50  
51 =head1 ACCESSORS
52  
53 =head2 solr_url
54  
55 Url of the solr instance. Read-only.
56  
57 =head2 input_encoding [DEPRECATED]
58  
59 Assume the urls to be in this encoding, so decode it before parsing
60 it. This is basically a (probably bugged) workaround when you have all
61 the shop in latin1. If the search keep crashing on non-ascii
62 characters, try to set this to iso-8859-1.
63  
64 =head2 rows
65  
66 Number of results to return. Read-write (so you can reuse the object).
67  
68 =head2 page_scope
69  
70 The number of paging items for the paginator
71  
72 =head2 search_fields
73  
74 An arrayref with the indexed fields to search. Defaults to:
75  
76 [qw/sku name description/]
77  
78 You can add boost to fields appending a float with a caret.
79  
80 [qw/ sku^5.0 name^1.0 description^0.4 /]
81  
82  
83 =head2 return_fields
84  
85 An arrayref of indexed fields to return. All by default.
86  
87 =head2 facets
88  
89 A string or an arrayref with the fields which will generate a facet.
90 Defaults to
91  
92 [qw/suchbegriffe manufacturer/]
93  
94 =head2 start
95  
96 Start of pagination. Read-write.
97  
98 =head2 page
99  
100 Current page. Read-write.
101  
102 =head2 filters
103  
104 An hashref with the filters. E.g.
105  
106 {
107 suchbegriffe => [qw/xxxxx yyyy/],
108 manufacturer => [qw/pikeur/],
109 }
110  
111 The keys of the hashref, to have any effect, must be one of the facets.
112  
113 =head2 response
114  
115 Read-only accessor to the response object of the current search.
116  
117 =head2 facets_found
118  
119 After a search, the structure with the facets can be retrieved with
120 this accessor, with this structure:
121  
122 {
123 field => [ { name => "name", count => 11 }, { name => "name", count => 9 }, ... ],
124 field2 => [ { name => "name", count => 7 }, { name => "name", count => 6 }, ... ],
125 ...
126 }
127  
128 Each hashref in each field's arrayref has the following keys:
129  
130 =over 4
131  
132 =item name
133  
134 The name to display
135  
136 =item count
137  
138 The count of item
139  
140 =item query_url
141  
142 The url fragment to toggle this filter.
143  
144 =item active
145  
146 True if currently in use (to be used for, e.g., checkboxes)
147  
148 =back
149  
150 =head2 search_string
151  
152 Debug only. The search string produced by the query.
153  
154 =head2 search_terms
155  
156 The terms used for the current search.
157  
158 =head2 search_structure
159  
160 The perl data structure used for the current search. It's passed to
161 L<Webservice::Solr::Query> for stringification.
162  
163 =head2 sorting
164  
165 The field used to sort the result (optional and defaults to score, as
166 per Solr doc).
167  
168 You can set it to a scalar with a field name or instead you can use
169 the L<SQL::Abstract> syntax (all the cases documented there are
170 supported). E.g.
171  
172 $solr->sorting([{ -asc => 'created_date' }, {-desc => [qw/updated_date sku/] }]);
173  
174 If you pass a reference, the C<sorting_direction> setting is ignored.
175  
176 =head2 sorting_direction
177  
178 The direction used by the sorting, when C<sorting> is specified and is a plain scalar.
179 Default to 'desc'.
180  
181 =cut
182  
183 13 has solr_url => (is => 'ro',
184 required => 1);
185  
186 13 has input_encoding => (is => 'ro');
187  
188  
189 =head2 wild_matching
190  
191 By default, a search term produce a query with a wildcard appended. So
192 searching for 1234 will query 1234*. With this option set to true, a
193 wildcard is prepended as well, querying for *1234* instead).
194  
195 =cut
196  
197  
198 has wild_matching => (is => 'ro',
199 0 ▶ default => sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
200  
201 =head2 stop_words_langs
202  
203 The languages for which we should build the stop word list. It
204 defaults to:
205  
206 [ 'en' ]
207  
208 New in 0.10. To revert to the old behaviour (no filtering of
209 stopwords), pass an empty arrayref.
210  
211 =cut
212  
213 13 has stop_words => (is => 'lazy', isa => HashRef);
214  
215 0 ▶has stop_words_langs => (is => 'ro', default => sub { [qw/en/ ] }, isa => ArrayRef);
Subroutine __ANON__: not called CC=1 SCAR=1.2
216  
217 sub _build_stop_words {
218 0 ▶ my $self = shift;
Subroutine _build_stop_words: not called CC=3 SCAR=24.8
219 0 my @stopwords;
220 0 foreach my $lang (@{ $self->stop_words_langs }) {
221 0 ▶ if (my $stops = Lingua::StopWords::getStopWords($lang, 'UTF-8')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stops = Lingua::StopWords::getStopWords($lang, "UTF-8")) 0 0 0
222 0 push @stopwords, keys %$stops;
223 }
224 }
225 0 my %out = map { $_ => 1 } @stopwords;
226 0 return \%out;
227 }
228  
229 =head2 min_chars
230  
231 Minimum characters for filtering the search terms. Default to 3.
232  
233 New in 0.10. To revert to the old behaviour, set it to 0.
234  
235 =head2 permit_empty_search
236  
237 By default, empty searches are not executed. You can permit them
238 setting this accessor to 1. The module will reset it to 0 when the
239 search is executed.
240  
241 =cut
242  
243 0 ▶has min_chars => (is => 'ro', isa => Int, default => sub { 3 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
244  
245 0 ▶has permit_empty_search => (is => 'rw', isa => Bool, default => sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
246  
247 has search_fields => (is => 'ro',
248 default => sub {
249 return [
250 0 ▶ qw/sku
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 name
252 description/
253 ]
254 },
255 0 ▶ isa => sub { die unless ref($_[0]) eq 'ARRAY' });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 0 0
256  
257 has facets => (is => 'rw',
258 0 ▶ isa => sub { die "not an arrayref" unless ref($_[0]) eq 'ARRAY' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 0 0
259 default => sub {
260 0 ▶ return [qw/suchbegriffe manufacturer/];
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 13 });
262  
263 has rows => (is => 'rw',
264 0 ▶ default => sub { 10 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
265  
266 has page_scope => (is => 'rw',
267 0 ▶ default => sub { 5 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
268  
269 has start => (is => 'rw',
270 0 ▶ default => sub { 0 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
271  
272 has page => (is => 'rw',
273 0 ▶ default => sub { 1 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
274  
275 has filters => (is => 'rw',
276 isa => HashRef,
277 0 ▶ default => sub { return {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 13 );
279  
280 13 has response => (is => 'rwp');
281  
282 13 has search_string => (is => 'rwp');
283  
284 has search_terms => (is => 'rw',
285 0 ▶ isa => sub { die unless ref($_[0]) eq 'ARRAY' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 0 0
286 0 ▶ default => sub { return [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
287 13 );
288  
289 13 has search_structure => (is => 'rw');
290  
291 13 has sorting => (is => 'rw');
292 has sorting_direction => (is => 'rw',
293 0 ▶ isa => sub { die unless $_[0] =~ m/\A(asc|desc)\z/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\A(asc|desc)\z/ 0 0 0
294 0 ▶ default => sub { 'desc' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 13 );
296  
297 has return_fields => (is => 'rw',
298 0 ▶ isa => sub { die unless ref($_[0]) eq 'ARRAY' },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 0 0
299 13 );
300  
301 has global_conditions => (is => 'rw',
302 0 ▶ isa => sub { die unless ref($_[0]) eq 'HASH' }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 0 0
303 13 );
304  
305 sub results {
306 0 ▶ my $self = shift;
Subroutine results: not called CC=5 SCAR=34.0
Pod documented
307 0 my @matches;
308 0 ▶ if ($self->response->ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response->ok) 0 0 0
309 0 for my $doc ( $self->response->docs ) {
310 0 my (%record, $name);
311  
312 0 for my $fld ($doc->fields) {
313 0 $name = $fld->name;
314 0 ▶ next if $name =~ /^_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^_/ 0 0 0
315  
316 0 $record{$name} = $fld->value;
317 }
318  
319 0 push @matches, \%record;
320 }
321 }
322 0 return \@matches;
323 }
324  
325 =head1 INTERNAL ACCESSORS
326  
327 =head2 solr_object
328  
329 The L<WebService::Solr> instance.
330  
331 =cut
332  
333 13 has solr_object => (is => 'lazy');
334  
335 sub _build_solr_object {
336 0 ▶ my $self = shift;
Subroutine _build_solr_object: not called CC=1 SCAR=6.9
337 0 my @args = $self->solr_url;
338 # if (my $enc = $self->solr_encoding) {
339 # my %options = (
340 # default_params => {
341 # ie => $enc,
342 # },
343 # );
344 # push @args, \%options;
345 # }
346 0 return WebService::Solr->new(@args);
347 }
348  
349 =head2 builder_object(\@terms, \%filters, $page)
350  
351 Creates Interchange::Search::Solr::Builder instance.
352  
353 =cut
354  
355 sub builder_object {
356 0 ▶ my ($self, $terms, $filters, $page) = @_;
Subroutine builder_object: not called CC=1 SCAR=6.9
Pod documented
357 0 return Interchange::Search::Solr::Builder->new(
358 terms => $terms,
359 filters => $filters,
360 facets => $self->facets,
361 page => $page
362 );
363 }
364  
365 =head1 METHODS
366  
367 =head2 search( [ $string_or$structure ] ])
368  
369 Run a search and return a L<WebService::Solr::Response> object.
370  
371 The method accept zero or one argument.
372  
373 With no arguments, run a full wildcard search.
374  
375 With one argument, if it's a string, run the search against all the
376 indexed fields. If it's a structure, build a query for it. The syntax
377 of the structure is described at L<WebService::Solr::Query>.
378  
379 After calling this method you can inspect the response using the
380 following methods:
381  
382 =head2 results
383  
384 Returns reference to list of results, each result is a hash
385 reference.
386  
387 =head2 skus_found
388  
389 Returns just a plain list of skus.
390  
391 =head2 num_found
392  
393 Return the number of items found
394  
395 =head2 has_more
396  
397 Return true if there are more pages
398  
399 =cut
400  
401 sub search {
402 0 ▶ my ($self, $string) = (shift, shift);
Subroutine search: not called CC=5 SCAR=34.0
Pod documented
403 0 ▶ die "Extra parameter found" if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
404 0 my $structure;
405 # here we just split the terms, set C<search_terms>, and call
406 # _do_search.
407 0 ▶ if ($string && ref($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string and ref $string) 0 0 0
Condition0%0/3
$string and ref $string
!ll&&!rl&&r
000
MC/DC0%0/2
$string and ref $string
$string ref $string
Truth table0%0/3
A and B
A= $stringB= ref $string
ABresult
0X0
100
111
408 0 $structure = $string;
409 0 $string = undef;
410 }
411 0 my @terms;
412 0 ▶ if ($string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string) 0 0 0
413 0 @terms = grep { $self->_term_is_good($_) } split(/\s+/, $string);
414 }
415 0 $self->search_terms(\@terms);
416 0 $self->search_structure($structure);
417 0 return $self->_do_search;
418 }
419  
420 sub _do_search {
421 0 ▶ my $self = shift;
Subroutine _do_search: not called CC=4 SCAR=30.0
422  
423 0 my @terms = grep { $self->_term_is_good($_) } @{ $self->search_terms };
424  
425 0 my $query = '';
426 0 my $wild_match = '';
427 0 ▶ if ($self->wild_matching) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wild_matching) 0 0 0
428 0 $wild_match = '*';
429 }
430  
431 0 ▶ if (@terms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@terms) { } 0 0 0
432 0 my @escaped = map { $wild_match . WebService::Solr::Query->escape($_) . '*' } @terms;
433 0 $query = '(' . join(' AND ', @escaped) . ')';
434 # even if the structure looks correct, the query isn't build properly
435 # print Dumper($query);
436 }
437 else {
438 # catch all
439 0 ▶ if (my $structure = $self->search_structure) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $structure = $self->search_structure) 0 0 0
440 0 $query = WebService::Solr::Query->new($structure);
441 }
442  
443 }
444 0 return $self->execute_query($query);
445 }
446  
447 =head2 execute_query($query)
448  
449 Accept either a raw string with the query or a WebService::Solr::Query
450 object and run the query against the Solr service.
451  
452 If no query is provided, a wildcard search is performed.
453  
454 =cut
455  
456 sub _search_is_empty {
457 0 ▶ my $self = shift;
Subroutine _search_is_empty: not called CC=4 SCAR=30.0
458 0 my @terms = @{ $self->search_terms };
459 0 my %filters = %{ $self->filters };
460 0 my $structure = $self->search_structure;
461 0 ▶ if (@terms || %filters || $structure) {
Branch: 1 branch
BranchTrueFalseTotal
if (@terms or %filters or $structure) { } 0 0 0
Condition0%0/6
@terms or %filters or $structure
l!l&&r!l&&!r
000
@terms or %filters
l!l&&r!l&&!r
000
MC/DC0%0/3
@terms or %filters or $structure
@terms %filters $structure
Truth table0%0/4
A or B or C
A= @termsB= %filtersC= $structure
ABCresult
1XX1
01X1
0011
0000
462 0 return 0;
463 }
464 else {
465 0 return 1;
466 }
467 }
468  
469 sub execute_query {
470 0 ▶ my ($self, $query) = @_;
Subroutine execute_query: not called CC=10 SCAR=47.0
Pod documented
471 0 my $querystring = '*';
472 0 ▶ if (ref($query)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $query) { } 0 0 0
elsif ($query) { } 0 0 0
473 0 $querystring = $query->stringify;
474 }
475 elsif ($query) {
476 0 $querystring = $query;
477 }
478  
479 0 ▶ if (my $global = $self->global_conditions) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $global = $self->global_conditions) 0 0 0
480 0 my @conditions = ($querystring);
481 0 foreach my $condition (keys %$global) {
482 0 my $string;
483 0 ▶ if (ref($global->{$condition}) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $global->{$condition} eq 'SCALAR') { } 0 0 0
484 0 $string = ${$global->{$condition}};
485 }
486 else {
487 $string = '"' .
488 0 WebService::Solr::Query->escape($global->{$condition}) . '"';
489 }
490 0 push @conditions, qq{($condition:$string)};
491 }
492 0 $querystring = '(' . join(' AND ', @conditions) . ')';
493 }
494 # save the debug info
495 0 $self->_set_search_string($querystring);
496  
497 0 my $our_res;
498 0 ▶ unless ($self->permit_empty_search) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permit_empty_search) 0 0 0
499 0 ▶ if ($self->_search_is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_search_is_empty) 0 0 0
500 0 $our_res = Interchange::Search::Solr::Response->new(HTTP::Response->new(404));
501 0 $our_res->error('empty_search');
502 }
503 }
504 0 ▶ unless ($our_res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($our_res) 0 0 0
505 0 my %params = $self->construct_params;
506 # print Dumper(\%params) if DEBUG;
507 0 my $res = $self->solr_object->search($querystring, \%params);
508 0 $our_res = Interchange::Search::Solr::Response->new($res->raw_response);
509  
510 0 ▶ if ($our_res->solr_status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($our_res->solr_status != 0) 0 0 0
511 0 die "Solr failure: ".$our_res->raw_response->message;
512 }
513 }
514 0 $self->_set_response($our_res);
515 0 $self->permit_empty_search(0);
516 0 return $our_res;
517 }
518  
519 =head2 construct_params
520  
521 Constructs parameters for the search.
522  
523 =cut
524  
525 sub construct_params {
526 # set start and rows
527 0 ▶ my $self = shift;
Subroutine construct_params: not called CC=9 SCAR=45.0
Pod documented
528 0 my %params = (
529 start => $self->_start_row,
530 rows => $self->_rows
531 );
532  
533  
534 0 ▶ if (my $facet_field = $self->facets) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $facet_field = $self->facets) 0 0 0
535 0 $params{facet} = 'true';
536 0 $params{'facet.field'} = $facet_field;
537 0 $params{'facet.mincount'} = 1;
538  
539 # see if we have filters set
540 0 ▶ if (my $filters = $self->filters) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filters = $self->filters) 0 0 0
541 0 my @fq;
542 0 foreach my $facet (@{ $self->facets }) {
543 0 ▶ if (my $condition = $filters->{$facet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $condition = $filters->{$facet}) 0 0 0
544 0 push @fq,
545 WebService::Solr::Query->new({
546 $facet => $condition,
547 });
548 }
549 }
550 0 ▶ if (@fq) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fq) 0 0 0
551 0 $params{fq} = \@fq;
552 }
553 }
554 }
555 0 ▶ if (my $sort_by = $self->sorting) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sort_by = $self->sorting) 0 0 0
556 0 my $sort_by_struct;
557 0 ▶ if (ref($sort_by)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sort_by) { } 0 0 0
558 0 $sort_by_struct = $sort_by;
559 }
560 else {
561 0 $sort_by_struct = { '-' . $self->sorting_direction => $sort_by };
562 }
563 0 $params{sort} = join(', ', $self->_build_sort_field($sort_by_struct));
564 }
565 0 ▶ if (my $fl = $self->return_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fl = $self->return_fields) 0 0 0
566 0 $params{fl} = join(',', @$fl);
567 }
568 # if using edifmax
569 0 $params{qf} = join(' ', @{ $self->search_fields });
570 0 $params{defType} = 'edismax';
571 0 return %params;
572 }
573  
574 sub _start_row {
575 0 ▶ my $self = shift;
Subroutine _start_row: not called CC=2 SCAR=17.9
576 0 ▶ return $self->_convert_to_int($self->start) || 0;
Condition0%0/2
$self->_convert_to_int($self->start) || 0
l!l
00
MC/DC0%0/1
$self->_convert_to_int($self->start) || 0
$self->_convert_to_int($self->start)
Truth table0%0/2
A
A= $self->_convert_to_int($self->start)
Aresult
11
00
577 }
578  
579 sub _rows {
580 0 ▶ my $self = shift;
Subroutine _rows: not called CC=2 SCAR=17.9
581 0 ▶ return $self->_convert_to_int($self->rows) || 10;
Condition0%0/2
$self->_convert_to_int($self->rows) || 10
l!l
00
MC/DC0%0/1
$self->_convert_to_int($self->rows) || 10
$self->_convert_to_int($self->rows)
Truth table0%0/2
A
A= $self->_convert_to_int($self->rows)
Aresult
11
00
582 }
583  
584 sub _convert_to_int {
585 0 ▶ my ($self, $maybe_num) = @_;
Subroutine _convert_to_int: not called CC=3 SCAR=24.8
586 0 ▶ return 0 unless $maybe_num;
Branch: 1 branch
BranchTrueFalseTotal
unless $maybe_num 0 0 0
587 0 ▶ if ($maybe_num =~ m/([1-9][0-9]*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_num =~ /([1-9][0-9]*)/) { } 0 0 0
588 0 return $1;
589 }
590 else {
591 0 return 0;
592 }
593 }
594  
595 sub num_found {
596 0 ▶ my $self = shift;
Subroutine num_found: not called CC=3 SCAR=24.8
Pod documented
597 0 ▶ if (my $res = $self->response) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->response) { } 0 0 0
598 0 ▶ return $res->content->{response}->{numFound} || 0;
Condition0%0/2
$res->content->{'response'}{'numFound'} || 0
l!l
00
MC/DC0%0/1
$res->content->{'response'}{'numFound'} || 0
$res->content->{'response'}{'numFound'}
Truth table0%0/2
A
A= $res->content->{'response'}{'numFound'}
Aresult
11
00
599 }
600 else {
601 0 return 0;
602 }
603 }
604  
605 sub skus_found {
606 0 ▶ my $self = shift;
Subroutine skus_found: not called CC=3 SCAR=24.8
Pod documented
607 0 my @skus;
608 0 ▶ if ($self->response->ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response->ok) 0 0 0
609 0 foreach my $item ($self->response->docs) {
610 0 push @skus, $item->value_for('sku');
611 }
612 }
613 0 return @skus;
614 }
615  
616 sub facets_found {
617 0 ▶ my $self = shift;
Subroutine facets_found: not called CC=3 SCAR=24.8
Pod documented
618 0 my $res = $self->response;
619 0 my $facets = $res->content->{facet_counts}->{facet_fields};
620 0 my %out;
621 0 foreach my $field (keys %$facets) {
622 0 my @list = @{$facets->{$field}};
623 0 my @items;
624 0 ▶ while (@list > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list > 1) 0 0 0
625 0 my $name = shift @list;
626 0 my $count = shift @list;
627 0 push @items, {
628 name => $name,
629 count => $count,
630 query_url => $self->_build_facet_url($field, $name),
631 active => $self->_filter_is_active($field, $name),
632 };
633 }
634 0 $out{$field} = \@items;
635 }
636 0 return \%out;
637 }
638  
639  
640 sub has_more {
641 0 ▶ my $self = shift;
Subroutine has_more: not called CC=2 SCAR=17.9
Pod documented
642 0 ▶ if ($self->num_found > ($self->_start_row + $self->_rows)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->num_found > $self->_start_row + $self->_rows) { } 0 0 0
643 0 return 1;
644 }
645 else {
646 0 return 0;
647 }
648 }
649  
650  
651 =head2 maintainer_update($mode)
652  
653 Perform a maintainer update and return a L<WebService::Solr::Response>
654 object.
655  
656 =cut
657  
658 sub maintainer_update {
659 0 ▶ my ($self, $mode, $data) = @_;
Subroutine maintainer_update: not called CC=6 SCAR=37.4
Pod documented
660 0 ▶ die "Missing argument" unless $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode 0 0 0
661 0 my (@query, %params);
662  
663 0 ▶ if ($mode eq 'add') {
Branch: 4 branches
BranchTrueFalseTotal
if ($mode eq 'add') { } 0 0 0
elsif ($mode eq 'clear') { } 0 0 0
elsif ($mode eq 'full') { } 0 0 0
elsif ($mode eq 'delta') { } 0 0 0
664 0 my $xml = $self->_build_xml_add_op($data);
665  
666 0 %params = (
667 'stream.body' => $xml,
668 commit => 'true',
669 );
670  
671 0 @query = ('update', \%params);
672 }
673 elsif ($mode eq 'clear') {
674 0 %params = (
675 'stream.body' => '<delete><query>*:*</query></delete>',
676 commit => 'true',
677 );
678 0 @query = ('update', \%params);
679 }
680 elsif ($mode eq 'full') {
681 0 @query = ('dataimport', { command => 'full-import' });
682 }
683 elsif ($mode eq 'delta') {
684 0 @query = ('dataimport', { command => 'delta-import' });
685 }
686 else {
687 0 die "Unrecognized mode $mode!";
688 }
689 0 return $self->solr_object->generic_solr_request(@query);
690 }
691  
692 # builds XML for add maintainer option
693  
694 sub _build_xml_add_op {
695 3 ▶ my ($self, $input) = @_;
Subroutine _build_xml_add_op: called CC=8 SCAR=20.8
696 3 my $doc = XML::LibXML::Document->new;
697 3 my $el_add = $doc->createElement('add');
698 3 my $list;
699 3 $doc->addChild($el_add);
700  
701 3 ▶ if (ref($input) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input eq 'ARRAY') { } 1 2 3
elsif (ref $input eq 'HASH') { } 2 0 2
702 1 $list = $input;
703 }
704 elsif (ref($input) eq 'HASH') {
705 2 $list = [ $input ];
706 }
707 else {
708 0 die "Bad usage: input should be an arrayref or an hashref";
709 }
710  
711 3 foreach my $data (@$list) {
712 4 my $el_doc = $doc->createElement('doc');
713 4 $el_add->addChild($el_doc);
714 4 ▶ while (my ($name, $value) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 4 4 8
715 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 0 4
716 4 my @values;
717 4 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 2 2 4
718 2 @values = @$value;
719 } else {
720 2 @values = ($value);
721 }
722 4 foreach my $v (@values) {
723 6 my $el_field = $doc->createElement('field');
724 6 $el_field->setAttribute(name => $name);
725 6 $el_field->appendText($v);
726 6 $el_doc->addChild($el_field);
727 }
728 }
729 }
730 }
731 3 return $doc->toString;
732 }
733  
734 =head2 reset_object
735  
736 Reset the leftovers of a possible previous search.
737  
738 =head2 search_from_url($url)
739  
740 Parse the url provided and do the search.
741  
742 =cut
743  
744 sub reset_object {
745 0 ▶ my $self = shift;
Subroutine reset_object: not called CC=1 SCAR=6.9
Pod documented
746 0 $self->start(0);
747 0 $self->page(1);
748 0 $self->_set_response(undef);
749 0 $self->_set_search_string(undef);
750 0 $self->filters({});
751 0 $self->search_terms([]);
752 0 $self->search_structure(undef);
753 }
754  
755 sub search_from_url {
756 0 ▶ my ($self, $url) = @_;
Subroutine search_from_url: not called CC=2 SCAR=17.9
Pod documented
757 0 ▶ if (my $enc = $self->input_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = $self->input_encoding) 0 0 0
758 0 $url = Encode::decode($enc, $url);
759 }
760 0 $self->_parse_url($url);
761 # at this point, all the parameters are set after the url parsing
762 0 return $self->_do_search;
763 }
764  
765 =head2 add_terms_to_url($url, $string)
766  
767 Parse the url, and return a new one with the additional words added.
768 The page is discarded, while the filters are retained.
769  
770 =cut
771  
772 sub add_terms_to_url {
773 0 ▶ my ($self, $url, @other_terms) = @_;
Subroutine add_terms_to_url: not called CC=3 SCAR=24.8
Pod documented
774 0 ▶ die "Bad usage" unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 0 0
775 0 $self->_parse_url($url);
776 0 ▶ return $url unless @other_terms;
Branch: 1 branch
BranchTrueFalseTotal
unless @other_terms 0 0 0
777 0 my @additional_terms = grep { $self->_term_is_good($_) } @other_terms;
778 0 my @terms = @{ $self->search_terms };
779 0 push @terms, @additional_terms;
780 0 $self->search_terms(\@terms);
781 0 my $builder = $self->builder_object(
782 $self->search_terms,
783 $self->filters
784 );
785 0 return $builder->url_builder;
786
787 }
788  
789  
790 sub _parse_url {
791 0 ▶ my ($self, $url) = @_;
Subroutine _parse_url: not called CC=13 SCAR=52.0
792 0 $self->reset_object;
793 0 ▶ return unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
794 0 my @fragments = grep { $_ } split('/', $url);
795  
796 # nothing to do if there are no fragments
797 0 ▶ return unless @fragments;
Branch: 1 branch
BranchTrueFalseTotal
unless @fragments 0 0 0
798  
799 0 my (@terms, %filters);
800 # the first keyword we need is the optional "words"
801 0 ▶ if ($fragments[0] eq 'words') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragments[0] eq "words") 0 0 0
802 # just discards and check if we have something. This could
803 # also mean that the next word is not a keyword.
804 0 shift @fragments;
805 0 ▶ if (@fragments) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fragments) 0 0 0
806 0 push @terms, shift @fragments;
807 }
808 }
809  
810 # the page is the last fragment, so check that
811 0 ▶ if (@fragments > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fragments > 1) 0 0 0
812 0 my $page = $#fragments;
813 0 ▶ if ($fragments[$page - 1] eq 'page') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fragments[$page - 1] eq "page") 0 0 0
814 0 $page = pop @fragments;
815 # and remove the page
816 0 pop @fragments;
817 # but assert it is a number, 1 otherwise
818 0 ▶ if ($page =~ s/^([1-9][0-9]*)$/)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ s/^([1-9][0-9]*)$/)/) { } 0 0 0
819 0 $self->page($1);
820 }
821 else {
822 0 $self->page(1);
823 }
824 0 $self->_set_start_from_page;
825 }
826 }
827 0 my $current_filter;
828 0 ▶ while (@fragments) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fragments) 0 0 0
829  
830 0 my $chunk = shift @fragments;
831  
832 # we lookup until the first keyword, but only if there is
833 # non-keywords after that.
834 0 ▶ if ($self->_fragment_is_keyword($chunk) and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fragment_is_keyword($chunk) and @fragments and not $self->_fragment_is_keyword($fragments[0])) 0 0 0
Condition0%0/6
$self->_fragment_is_keyword($chunk) and @fragments and not $self->_fragment_is_keyword($fragments[0])
!ll&&!rl&&r
000
$self->_fragment_is_keyword($chunk) and @fragments
!ll&&!rl&&r
000
MC/DC0%0/3
$self->_fragment_is_keyword($chunk) and @fragments and not $self->_fragment_is_keyword($fragments[0])
$self->_fragment_is_keyword($chunk) @fragments not $self->_fragment_is_keyword($fragments[0])
Truth table0%0/4
A and B and not(C)
A= $self->_fragment_is_keyword($chunk)B= @fragmentsC= not $self->_fragment_is_keyword($fragments[0])
ABCresult
0XX0
10X0
1100
1111
835 @fragments and
836 !$self->_fragment_is_keyword($fragments[0])) {
837 # chunk is actually a keyword. Set the flag, prepare the
838 # array and move on.
839 0 $current_filter = $chunk;
840 0 $filters{$current_filter} = [];
841 0 next;
842 }
843  
844 # are we inside a filter?
845 0 ▶ if ($current_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_filter) { } 0 0 0
846 0 push @{ $filters{$current_filter} }, $chunk;
847 }
848 # if not, it's a term
849 else {
850 0 push @terms, $chunk;
851 }
852 }
853 # filter the terms
854 0 $self->search_terms([ grep { $self->_term_is_good($_) } @terms ]);
855 0 $self->filters(\%filters);
856 }
857  
858 sub _fragment_is_keyword {
859 0 ▶ my ($self, $fragment) = @_;
Subroutine _fragment_is_keyword: not called CC=2 SCAR=17.9
860 0 ▶ return unless defined $fragment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fragment 0 0 0
861 0 return grep { $_ eq $fragment } @{ $self->facets };
862 }
863  
864  
865 sub _set_start_from_page {
866 0 ▶ my $self = shift;
Subroutine _set_start_from_page: not called CC=1 SCAR=6.9
867 0 $self->start($self->rows * ($self->page - 1));
868 }
869  
870  
871 =head2 current_search_to_url
872  
873 Return the url for the current search.
874  
875 =cut
876  
877 sub current_search_to_url {
878 0 ▶ my ($self, %args) = @_;
Subroutine current_search_to_url: not called CC=2 SCAR=17.9
Pod documented
879 0 my $page;
880  
881 0 ▶ if (! $args{hide_page}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"hide_page"}) 0 0 0
882 0 $page = $self->page;
883 }
884  
885 0 my $builder = $self->builder_object($self->search_terms,
886 $self->filters,
887 $page);
888  
889 0 return $builder->url_builder;
890 }
891  
892 sub _build_facet_url {
893 0 ▶ my ($self, $field, $name) = @_;
Subroutine _build_facet_url: not called CC=6 SCAR=37.4
894 # get the current filters
895 # print "Building $field $name\n";
896 0 my @terms = @{ $self->search_terms };
897 # page is not needed
898  
899 # the hash for the url builder
900 0 my %toggled_filters;
901  
902 # the current filters
903 0 my $filters = $self->filters;
904  
905 # loop over the facets we defined
906 0 foreach my $facet (@{ $self->facets }) {
907  
908 # copy of the active filters
909 0 ▶ my @active = @{ $filters->{$facet} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$filters->{$facet} or [] 0 0 0
910  
911 # filter is active: remove
912 0 ▶ if ($self->_filter_is_active($facet, $name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_filter_is_active($facet, $name)) { } 0 0 0
elsif ($facet eq $field) { } 0 0 0
913 0 @active = grep { $_ ne $name } @active;
914 }
915 # it's not active, but we're building an url for this facet
916 elsif ($facet eq $field) {
917 0 push @active, $name;
918 }
919 # and store
920 0 ▶ $toggled_filters{$facet} = \@active if @active;
Branch: 1 branch
BranchTrueFalseTotal
if @active 0 0 0
921 }
922 # print Dumper(\@terms, \%toggled_filters);
923 0 my $builder = $self->builder_object(\@terms, \%toggled_filters);
924 0 return $builder->url_builder;
925 }
926  
927 sub _filter_is_active {
928 0 ▶ my ($self, $field, $name) = @_;
Subroutine _filter_is_active: not called CC=4 SCAR=30.0
929 0 my $filters = $self->filters;
930 0 ▶ if (my $list = $self->filters->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list = $self->filters->{$field}) 0 0 0
931 0 ▶ if (my @active = @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@active) = @$list) 0 0 0
932 0 ▶ if (grep { $_ eq $name } @active) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $name;} @active) 0 0 0
933 0 return 1;
934 }
935 }
936 }
937 0 return 0 ;
938 }
939  
940 =head2 paginator
941  
942 Return an hashref suitable to be turned into a paginator, undef if
943 there is no need for a paginator.
944  
945 Be careful that a defined empty string in the first/last/next/previous
946 keys is perfectly legit and points to an unfiltered search which will
947 return all the items, so concatenating it to the prefix is perfectly
948 fine (e.g. "products/" . ''). When rendering this structure to HTML,
949 just check if the value is defined, not if it's true.
950  
951 The structure looks like this:
952  
953 {
954 first => 'words/bla' || undef,
955 first_page => 1 || undef,
956 last => 'words/bla/page/5' || undef,
957 last_page => 5 || undef,
958 next => 'words/bla/page/5' || undef,
959 next_page => 5 || undef
960 previous => 'words/bla/page/3' || undef,
961 previous_page => 3 || undef,
962 pages => [
963 {
964 name => 1,
965 url => 'words/bla/page/1',
966 },
967 {
968 name => 2,
969 url => 'words/bla/page/2',
970 },
971 {
972 name => 3,
973 url => 'words/bla/page/3',
974 },
975 {
976 name => 4,
977 url => 'words/bla/page/4',
978 current => 1,
979 },
980 {
981 name => 5,
982 url => 'words/bla/page/5',
983 },
984 ]
985 total_pages => 5
986 }
987  
988 =cut
989  
990 sub paginator {
991 0 ▶ my $self = shift;
Subroutine paginator: not called CC=12 SCAR=50.5
Pod documented
992 0 ▶ my $page = $self->page || 1;
Condition0%0/2
$self->page || 1
l!l
00
MC/DC0%0/1
$self->page || 1
$self->page
Truth table0%0/2
A
A= $self->page
Aresult
11
00
993 0 my $page_size = $self->rows;
994 0 my $page_scope = $self->page_scope;
995 0 my $total = $self->num_found;
996 0 ▶ return undef unless $total;
Branch: 1 branch
BranchTrueFalseTotal
unless $total 0 0 0
997 0 my $total_pages = POSIX::ceil($total / $page_size);
998 0 ▶ return undef if $total_pages < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $total_pages < 2 0 0 0
999  
1000 # compute the scope
1001 0 ▶ my $start = ($page - $page_scope > 0) ? ($page - $page_scope) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$page - $page_scope > 0 ? : 0 0 0
1002 0 ▶ my $end = ($page + $page_scope < $total_pages) ? ($page + $page_scope) : $total_pages;
Branch: 1 branch
BranchTrueFalseTotal
$page + $page_scope < $total_pages ? : 0 0 0
1003  
1004 0 my %pager = (items => []);
1005 0 my $builder = $self->builder_object($self->search_terms, $self->filters);
1006  
1007 0 ▶ for (my $count = $start; $count <= $end ; $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count <= $end) 0 0 0
1008 # create the link
1009 0 $builder->page($count);
1010 0 my $url = $builder->url_builder;
1011 0 my $item = {
1012 url => $url,
1013 name => $count,
1014 };
1015 0 my $position = $count - $page;
1016 0 ▶ if ($position == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($position == 0) { } 0 0 0
elsif ($position == 1) { } 0 0 0
elsif ($position == -1) { } 0 0 0
1017 0 $item->{current} = 1;
1018 }
1019 elsif ($position == 1) {
1020 0 $pager{next} = $url;
1021 0 $pager{next_page} = $count;
1022 }
1023 elsif ($position == -1) {
1024 0 $pager{previous} = $url;
1025 0 $pager{previous_page} = $count;
1026 }
1027 0 push @{$pager{items}}, $item;
1028 }
1029 0 ▶ if ($page != $total_pages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page != $total_pages) 0 0 0
1030 0 $builder->page($total_pages);
1031 0 $pager{last} = $builder->url_builder;
1032 0 $pager{last_page} = $total_pages;
1033 }
1034 0 ▶ if ($page != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page != 1) 0 0 0
1035 0 $builder->page(1);
1036 0 $pager{first} = $builder->url_builder;
1037 0 $pager{first_page} = 1;
1038 }
1039 0 $pager{total_pages} = $total_pages;
1040 0 return \%pager;
1041 }
1042  
1043 =head2 terms_found
1044  
1045 Returns an hashref suitable to build a widget with the terms used and
1046 the links to toggle them. Return undef if no terms were used in the search.
1047  
1048 The structure looks like this:
1049  
1050 {
1051 reset => '',
1052 terms => [
1053 { term => 'first', url => 'words/second' },
1054 { term => 'second', url => 'words/first' },
1055 ],
1056 }
1057  
1058 See also:
1059  
1060 =over 4
1061  
1062 =item clear_words_link
1063  
1064 Which return the reset link
1065  
1066 =item remove_word_links
1067  
1068 Which returns a list of hashrefs with C<uri> and C<label> for each
1069 word to remove.
1070  
1071 =back
1072  
1073 =cut
1074  
1075 sub terms_found {
1076 0 ▶ my $self = shift;
Subroutine terms_found: not called CC=3 SCAR=24.8
Pod documented
1077 0 my @terms = @{ $self->search_terms };
1078 0 ▶ return unless @terms;
Branch: 1 branch
BranchTrueFalseTotal
unless @terms 0 0 0
1079 0 my %out = (
1080 reset => $self->builder_object([], $self->filters)->url_builder,
1081 terms => [],
1082 );
1083 0 my @toggled;
1084 0 my $builder = $self->builder_object(\@toggled, $self->filters);
1085 0 foreach my $term (@terms) {
1086 0 @toggled = grep { $_ ne $term } @terms;
1087 0 $builder->terms(\@toggled);
1088 0 push @{ $out{terms} }, {
1089 term => $term,
1090 url => $builder->url_builder,
1091 };
1092 }
1093 0 return \%out;
1094
1095 }
1096  
1097 =head2 version
1098  
1099 Return the version of this module.
1100  
1101 =cut
1102  
1103 sub version {
1104 0 ▶ return $VERSION;
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
1105 }
1106  
1107  
1108 =head2 breadcrumbs
1109  
1110 Return a list of hashrefs with C<uri> and C<label> suitable to compose
1111 a breadcrumb for the current search.
1112  
1113 If the breadcrumb points to a facet, the facet name is stored in the
1114 C<facet> key.
1115  
1116 =cut
1117  
1118 sub breadcrumbs {
1119 0 ▶ my $self = shift;
Subroutine breadcrumbs: not called CC=6 SCAR=37.4
Pod documented
1120 0 my $words = $self->search_terms;
1121 0 my $filters = $self->filters;
1122 # always add the root
1123 0 my @pieces;
1124 0 my $current_uri = 'words';
1125 0 foreach my $word (@$words) {
1126 0 $current_uri .= "/$word";
1127 0 push @pieces, {
1128 uri => $current_uri,
1129 label => $word,
1130 };
1131 }
1132 0 ▶ if (%$filters) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$filters) 0 0 0
1133 0 foreach my $facet (@{ $self->facets }) {
1134 0 ▶ if (my $terms = $filters->{$facet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $terms = $filters->{$facet}) 0 0 0
1135 0 $current_uri .= "/$facet";
1136 0 foreach my $term (@$terms) {
1137 0 $current_uri .= "/$term";
1138 0 push @pieces, {
1139 uri => $current_uri,
1140 label => $term,
1141 facet => $facet,
1142 };
1143 }
1144 }
1145 }
1146 }
1147 0 return @pieces;
1148 }
1149  
1150 sub clear_words_link {
1151 0 ▶ my $self = shift;
Subroutine clear_words_link: not called CC=2 SCAR=17.9
Pod documented
1152 0 ▶ if (my $struct = $self->terms_found) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $struct = $self->terms_found) { } 0 0 0
1153 0 return $struct->{reset};
1154 }
1155 else {
1156 0 return;
1157 }
1158 }
1159  
1160 sub remove_word_links {
1161 0 ▶ my $self = shift;
Subroutine remove_word_links: not called CC=4 SCAR=30.0
Pod documented
1162 0 my @out;
1163 0 ▶ if (my $struct = $self->terms_found) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $struct = $self->terms_found) 0 0 0
1164 0 ▶ if (my $terms = $struct->{terms}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $terms = $struct->{"terms"}) 0 0 0
1165 0 foreach my $term (@$terms) {
1166 push @out, {
1167 uri => $term->{url},
1168 label => $term->{term},
1169 0 };
1170 }
1171 }
1172 }
1173 0 return @out;
1174 }
1175  
1176 sub _term_is_good {
1177 0 ▶ my ($self, $term) = @_;
Subroutine _term_is_good: not called CC=6 SCAR=37.4
1178 0 ▶ if ($term && $term =~ /\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term and $term =~ /\w/) 0 0 0
Condition0%0/3
$term and $term =~ /\w/
!ll&&!rl&&r
000
MC/DC0%0/2
$term and $term =~ /\w/
$term $term =~ /\w/
Truth table0%0/3
A and B
A= $termB= $term =~ /\w/
ABresult
0X0
100
111
1179 0 ▶ if ($self->stop_words->{lc($term)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stop_words->{lc $term}) 0 0 0
1180 0 return 0;
1181 }
1182 0 ▶ if ($self->min_chars > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->min_chars > 1) 0 0 0
1183 0 my $re = "\\w.*" x $self->min_chars;
1184 0 ▶ if ($term =~ m/$re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term =~ /$re/) { } 0 0 0
1185 0 return 1;
1186 }
1187 else {
1188 0 return 0;
1189 }
1190 }
1191 0 return 1;
1192 }
1193 0 return 0;
1194 }
1195  
1196 # stolen from SQL::Abstract
1197  
1198 sub _build_sort_field {
1199 0 ▶ my ($self, $arg) = @_;
Subroutine _build_sort_field: not called CC=1 SCAR=6.9
1200 return $self->_SWITCH_refkind($arg,
1201 {
1202 ARRAYREF => sub {
1203 0 ▶ map { $self->_build_sort_field($_) } @$arg;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1204 },
1205 SCALAR => sub {
1206 0 ▶ return "$arg";
Subroutine __ANON__: not called CC=1 SCAR=6.9
1207 },
1208 UNDEF => sub {
1209 0 ▶ return;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1210 },
1211 SCALARREF => sub {
1212 0 ▶ $$arg;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1213 },
1214 HASHREF => sub {
1215 0 ▶ my ($key, $val, @rest) = %$arg;
Subroutine __ANON__: not called CC=5 SCAR=34.0
1216 0 ▶ return () unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
1217 0 ▶ if ( @rest or not $key =~ /^-(desc|asc)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest or not $key =~ /^-(desc|asc)/i) 0 0 0
Condition0%0/3
@rest or not $key =~ /^-(desc|asc)/i
l!l&&r!l&&!r
000
MC/DC0%0/2
@rest or not $key =~ /^-(desc|asc)/i
@rest not $key =~ /^-(desc|asc)/i
Truth table0%0/3
A or not(B)
A= @restB= not $key =~ /^-(desc|asc)/i
ABresult
1X1
011
000
1218 0 die "hash passed to sorting must have exactly ".
1219 "one key (-desc or -asc)";
1220 }
1221 0 my $direction = $1;
1222 0 my @ret;
1223 0 for my $c ($self->_build_sort_field($val)) {
1224 0 my $query;
1225 $self->_SWITCH_refkind ($c,
1226 {
1227 SCALAR => sub {
1228 0 ▶ $query = $c;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1229 },
1230 0 });
1231 0 $query = $query . ' ' . lc($direction);
1232 0 push @ret, $query;
1233 }
1234 0 return @ret;
1235 },
1236 0 });
1237 }
1238  
1239  
1240 sub _refkind {
1241 0 ▶ my ($self, $data) = @_;
Subroutine _refkind: not called CC=8 SCAR=42.8
1242 0 ▶ return 'UNDEF' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
1243 # blessed objects are treated like scalars
1244 0 ▶ my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 0 0 0
1245 0 ▶ return 'SCALAR' unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 0 0
1246 0 my $n_steps = 1;
1247 0 ▶ while ($ref eq 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "REF") 0 0 0
1248 0 $data = $$data;
1249 0 ▶ $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 0 0 0
1250 0 ▶ $n_steps++ if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
1251 }
1252 0 ▶ return ($ref||'SCALAR') . ('REF' x $n_steps);
Condition0%0/2
$ref || 'SCALAR'
l!l
00
MC/DC0%0/1
$ref || 'SCALAR'
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
1253 }
1254  
1255 sub _SWITCH_refkind {
1256 0 ▶ my ($self, $data, $dispatch_table) = @_;
Subroutine _SWITCH_refkind: not called CC=2 SCAR=17.9
1257 0 my $type = $self->_refkind($data);
1258 0 my $coderef = $dispatch_table->{$self->_refkind($data)};
1259 0 ▶ die "Unsupported structure $type" unless $coderef;
Branch: 1 branch
BranchTrueFalseTotal
unless $coderef 0 0 0
1260 0 $coderef->();
1261 }
1262  
1263  
1264 =head1 AUTHOR
1265  
1266 Marco Pessotto, C<< <melmothx at gmail.com> >>
1267  
1268 =head1 BUGS
1269  
1270 Please report any bugs or feature requests to
1271 L<https://github.com/interchange/Interchange-Search-Solr/issues>.
1272  
1273 =head1 SUPPORT
1274  
1275 You can find documentation for this module with the perldoc command.
1276  
1277 perldoc Interchange::Search::Solr
1278  
1279  
1280 You can also look for information at:
1281  
1282 =over 4
1283  
1284 =item * Github
1285  
1286 L<https://github.com/interchange/Interchange-Search-Solr>
1287  
1288 =item * AnnoCPAN: Annotated CPAN documentation
1289  
1290 L<http://annocpan.org/dist/Interchange-Search-Solr>
1291  
1292 =item * CPAN Ratings
1293  
1294 L<http://cpanratings.perl.org/d/Interchange-Search-Solr>
1295  
1296 =item * META CPAN
1297  
1298 L<https://metacpan.org/pod/Interchange::Search::Solr>
1299  
1300 =back
1301  
1302  
1303 =head1 ACKNOWLEDGEMENTS
1304  
1305 Mohammad S Anwar (GH #14).
1306  
1307 =head1 LICENSE AND COPYRIGHT
1308  
1309 Copyright 2014-2016 Marco Pessotto.
1310  
1311 This program is free software; you can redistribute it and/or modify it
1312 under the terms of the the Artistic License (2.0). You may obtain a
1313 copy of the full license at:
1314  
1315 L<http://www.perlfoundation.org/artistic_license_2_0>
1316  
1317 Any use, modification, and distribution of the Standard or Modified
1318 Versions is governed by this Artistic License. By using, modifying or
1319 distributing the Package, you accept this license. Do not use, modify,
1320 or distribute the Package, if you do not accept this license.
1321  
1322 If your Modified Version has been derived from a Modified Version made
1323 by someone other than you, you are nevertheless required to ensure that
1324 your Modified Version complies with the requirements of this license.
1325  
1326 This license does not grant you the right to use any trademark, service
1327 mark, tradename, or logo of the Copyright Holder.
1328  
1329 This license includes the non-exclusive, worldwide, free-of-charge
1330 patent license to make, have made, use, offer to sell, sell, import and
1331 otherwise transfer the Package with respect to any patent claims
1332 licensable by the Copyright Holder that are necessarily infringed by the
1333 Package. If you institute patent litigation (including a cross-claim or
1334 counterclaim) against any party alleging that the Package constitutes
1335 direct or contributory patent infringement, then this Artistic License
1336 to you shall terminate on the date that such litigation is filed.
1337  
1338 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1339 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1340 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1341 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1342 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1343 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1344 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1345 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1346  
1347  
1348 =cut
1349  
1350 13 1; # End of Interchange::Search::Solr
↑ Summary Solr/UpdateIndex.pm »