Solr/JSON.pm

filter: Statementstmt 21.7% 27 / 124 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 33.3% 8 / 24 Podpod 85.7% 6 / 7 totaltotal 15.4% 41 / 265 CC 44 SCAR 41.0
CC
44
Cov
13%
_add
156.0
_doc2json
132.0
simpleUpdate
42.0
CRAP 60.1
↑ Summary Solr.pm »
1 # This code is part of Perl distribution Apache-Solr version 1.12.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2012-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Apache::Solr::JSON;{
13 2 our $VERSION = '1.12';
14 }
15  
16 2 ▶use base 'Apache::Solr';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use Log::Report qw/solr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Apache::Solr::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #--------------------
29  
30 sub init($)
31 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=3 SCAR=24.8
Pod undocumented
32 0 ▶ $args->{format} ||= 'JSON';
Condition0%0/2
$args->{"format"} ||= "JSON"
l!l
00
MC/DC0%0/1
$args->{"format"} ||= "JSON"
$args->{"format"}
Truth table0%0/2
A
A= $args->{"format"}
Aresult
11
00
33 0 $self->SUPER::init($args);
34  
35 0 ▶ $self->{ASJ_json} = $args->{json} || JSON->new->utf8;
Condition0%0/3
$args->{'json'} || 'JSON'->new->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'json'} || 'JSON'->new->utf8
$args->{'json'} 'JSON'->new->utf8
Truth table0%0/3
A || B
A= $args->{'json'}B= 'JSON'->new->utf8
ABresult
1X1
011
000
36 0 $self;
37 }
38  
39 #--------------------
40  
41 0 ▶sub json() { $_[0]->{ASJ_json} }
Subroutine json: not called CC=1 SCAR=6.9
Pod documented
42  
43 #--------------------
44  
45 sub _select($$)
46 0 ▶{ my ($self, $args, $params) = @_;
Subroutine _select: not called CC=2 SCAR=17.9
47  
48 # select may be called more than once, but do not add wt each time
49 # again.
50 0 my $endpoint = $self->endpoint('select', params => $params);
51 0 my $result = Apache::Solr::Result->new(%$args, params => $params, endpoint => $endpoint, core => $self);
52 0 $self->request($endpoint, $result);
53  
54 0 ▶ if(my $dec = $result->decoded)
Branch: 1 branch
BranchTrueFalseTotal
if (my $dec = $result->decoded) 0 0 0
55 { # JSON uses different names!
56 0 my $r = $dec->{result} = delete $dec->{response};
57 0 $r->{doc} = delete $r->{docs};
58 }
59 0 $result;
60 }
61  
62 sub _extract($$$)
63 0 ▶{ my ($self, $params, $data, $ct) = @_;
Subroutine _extract: not called CC=1 SCAR=6.9
64 0 my $endpoint = $self->endpoint('update/extract', params => $params);
65 0 my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
66 0 $self->request($endpoint, $result, $data, $ct);
67 0 $result;
68 }
69  
70  
71 sub _add($$$)
72 0 ▶{ my ($self, $docs, $attrs, $params) = @_;
Subroutine _add: not called CC=12 SCAR=50.5
73 0 ▶ $attrs ||= {};
Condition0%0/2
$attrs ||= {}
l!l
00
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table0%0/2
A
A= $attrs
Aresult
11
00
74 0 ▶ $params ||= [];
Condition0%0/2
$params ||= []
l!l
00
MC/DC0%0/1
$params ||= []
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
75  
76 0 my $sv = $self->serverVersion;
77 0 ▶ $sv ge '3.1' or error __x"Solr version too old for updates in JSON syntax.";
Branch: 1 branch
BranchTrueFalseTotal
$sv ge "3.1" or &error(&__x("Solr version too old for updates in JSON syntax.")) 0 0 0
78  
79 # We cannot create HASHes with twice the same key in Perl, so cannot
80 # produce the syntax for adding multiple documents. Try to save it.
81 delete $attrs->{boost}
82 0 ▶ if $attrs->{boost} && $attrs->{boost}==1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"boost"} and $attrs->{"boost"} == 1 0 0 0
Condition0%0/3
$attrs->{"boost"} and $attrs->{"boost"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$attrs->{"boost"} and $attrs->{"boost"} == 1
$attrs->{"boost"} $attrs->{"boost"} == 1
Truth table0%0/3
A and B
A= $attrs->{"boost"}B= $attrs->{"boost"} == 1
ABresult
0X0
100
111
83  
84 0 ▶ $params = +{ @$params } if ref $params eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $params eq "ARRAY" 0 0 0
85 exists $attrs->{$_} && ($params->{$_} = delete $attrs->{$_})
86 0 ▶ for qw/commit commitWithin overwrite boost/;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{$_} and $params->{$_} = delete $attrs->{$_} 0 0 0
87  
88 0 ▶ my $endpoint = $self->endpoint(($sv lt '4.0' ? 'update/json' : 'update'), params => $params);
Branch: 1 branch
BranchTrueFalseTotal
$sv lt '4.0' ? : 0 0 0
89 0 my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
90  
91 0 my $add;
92 0 ▶ if(@$docs==1)
Branch: 2 branches
BranchTrueFalseTotal
if (@$docs == 1) { } 0 0 0
elsif (keys %$attrs) { } 0 0 0
93 0 { $add = +{ add => +{ %$attrs, doc => $self->_doc2json($docs->[0]) } }
94 }
95 elsif(keys %$attrs)
96 { # in combination with attributes only
97 0 error __x"unable to add more than one doc with JSON interface.";
98 }
99 else
100 0 { $add = [ map $self->_doc2json($_), @$docs ];
101 }
102  
103 0 $self->request($endpoint, $result, $add);
104 0 $result;
105 }
106  
107 sub _doc2json($)
108 0 ▶{ my ($self, $this) = @_;
Subroutine _doc2json: not called CC=11 SCAR=48.8
109 0 my %doc;
110 0 foreach my $fieldname ($this->fieldNames)
111 0 { my @f;
112 0 foreach my $field ($this->fields($fieldname))
113 0 ▶ { my $update = $field->{update} || 'value';
Condition0%0/2
$field->{'update'} || 'value'
l!l
00
MC/DC0%0/1
$field->{'update'} || 'value'
$field->{'update'}
Truth table0%0/2
A
A= $field->{'update'}
Aresult
11
00
114 0 ▶ my $boost = $field->{boost} || 1.0;
Condition0%0/2
$field->{'boost'} || 1.0
l!l
00
MC/DC0%0/1
$field->{'boost'} || 1.0
$field->{'boost'}
Truth table0%0/2
A
A= $field->{'boost'}
Aresult
11
00
115  
116 0 ▶ undef $boost
Branch: 1 branch
BranchTrueFalseTotal
if $boost > "0.9999" and $boost < "1.0001" 0 0 0
Condition0%0/3
$boost > "0.9999" and $boost < "1.0001"
!ll&&!rl&&r
000
MC/DC0%0/2
$boost > "0.9999" and $boost < "1.0001"
$boost > "0.9999" $boost < "1.0001"
Truth table0%0/3
A and B
A= $boost > "0.9999"B= $boost < "1.0001"
ABresult
0X0
100
111
117 if $boost > 0.9999 && $boost < 1.0001;
118  
119 push @f,
120 ! defined $boost && $update eq 'value'
121 ? $field->{content}
122 : defined $boost
123 ? +{ boost => $boost, $update => $field->{content} }
124 0 ▶ : +{ $update => $field->{content} };
Branch: 2 branches
BranchTrueFalseTotal
!defined($boost) && $update eq 'value' ? : 0 0 0
defined $boost ? : 0 0 0
Condition0%0/3
not defined $boost and $update eq "value"
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $boost and $update eq "value"
not defined $boost $update eq "value"
Truth table0%0/3
not(A) and B
A= not defined $boostB= $update eq "value"
ABresult
0X0
100
111
125 }
126 # we have to combine multi-fields into ARRAYS
127 0 ▶ $doc{$fieldname} = @f > 1 ? \@f : $f[0];
Branch: 1 branch
BranchTrueFalseTotal
@f > 1 ? : 0 0 0
128 }
129  
130 0 \%doc;
131 }
132  
133 0 ▶sub _commit($) { my ($s, $attr) = @_; $s->simpleUpdate(commit => $attr) }
Subroutine _commit: not called CC=1 SCAR=6.9
134 0 ▶sub _optimize($) { my ($s, $attr) = @_; $s->simpleUpdate(optimize => $attr) }
Subroutine _optimize: not called CC=1 SCAR=6.9
135 0 ▶sub _delete($$) { my $self = shift; $self->simpleUpdate(delete => @_) }
Subroutine _delete: not called CC=1 SCAR=6.9
136 0 ▶sub _rollback() { $_[0]->simpleUpdate('rollback') }
Subroutine _rollback: not called CC=1 SCAR=6.9
137  
138 sub _terms($)
139 0 ▶{ my ($self, $terms) = @_;
Subroutine _terms: not called CC=5 SCAR=34.0
140 0 my $endpoint = $self->endpoint('terms', params => $terms);
141 0 my $result = Apache::Solr::Result->new(params => $terms, endpoint => $endpoint, core => $self);
142 0 $self->request($endpoint, $result);
143  
144 0 ▶ my $table = $result->decoded->{terms} || {};
Condition0%0/2
$result->decoded->{'terms'} || {}
l!l
00
MC/DC0%0/1
$result->decoded->{'terms'} || {}
$result->decoded->{'terms'}
Truth table0%0/2
A
A= $result->decoded->{'terms'}
Aresult
11
00
145 0 ▶ $table = {@$table} if ref $table eq 'ARRAY'; # bug in Solr 1.4
Branch: 1 branch
BranchTrueFalseTotal
if ref $table eq "ARRAY" 0 0 0
146  
147 0 ▶ while(my ($field, $terms) = each %$table)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$table) 0 0 0
148 { # repack array-of-pairs into array-of-arrays-of-pair
149 0 my @pairs = @$terms;
150 0 my @terms;
151 0 ▶ push @terms, [shift @pairs, shift @pairs] while @pairs;
Branch: 1 branch
BranchTrueFalseTotal
@pairs and do { push @terms, [shift @pairs, shift @pairs] } 0 0 0
152 0 $result->terms($field => \@terms);
153 }
154  
155 0 $result;
156 }
157  
158 #--------------------
159  
160 sub request($$;$$)
161 0 ▶{ my ($self, $url, $result, $body, $body_ct) = @_;
Subroutine request: not called CC=4 SCAR=30.0
Pod documented
162  
163 0 ▶ if(ref $body && ref $body ne 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body and ref $body ne "SCALAR") 0 0 0
Condition0%0/3
ref $body and ref $body ne "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $body and ref $body ne "SCALAR"
ref $body ref $body ne "SCALAR"
Truth table0%0/3
A and B
A= ref $bodyB= ref $body ne "SCALAR"
ABresult
0X0
100
111
164 0 ▶ { $body_ct ||= 'application/json; charset=utf-8';
Condition0%0/2
$body_ct ||= "application/json; charset=utf-8"
l!l
00
MC/DC0%0/1
$body_ct ||= "application/json; charset=utf-8"
$body_ct
Truth table0%0/2
A
A= $body_ct
Aresult
11
00
165 0 $body = \$self->json->encode($body);
166 }
167  
168 # Solr server 3.6.2 seems not to detect the JSON input from the
169 # body content, so requires this work-around
170 # https://solr.apache.org/guide/6_6/uploading-data-with-index-handlers.html#UploadingDatawithIndexHandlers-JSONUpdateConveniencePaths
171 0 $url =~ s!/update\?!/update/json?!;
172  
173 0 $self->SUPER::request($url, $result, $body, $body_ct);
174 }
175  
176  
177 sub decodeResponse($)
178 0 ▶{ my ($self, $resp) = @_;
Subroutine decodeResponse: not called CC=3 SCAR=24.8
Pod documented
179  
180 # At least until Solr 4.0 response ct=text/plain while producing JSON
181 0 my $ct = $resp->content_type;
182 0 ▶ $ct =~ m/json/i
Branch: 1 branch
BranchTrueFalseTotal
$ct =~ /json/i or &error(&__x("answer from solr server is not json but {type}.", "type", $ct)) 0 0 0
183 or error __x"answer from solr server is not json but {type}.", type => $ct;
184  
185 0 ▶ $self->json->decode($resp->decoded_content || $resp->content);
Condition0%0/3
$resp->decoded_content || $resp->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$resp->decoded_content || $resp->content
$resp->decoded_content $resp->content
Truth table0%0/3
A || B
A= $resp->decoded_contentB= $resp->content
ABresult
1X1
011
000
186 }
187  
188  
189 sub simpleUpdate($$;$)
190 0 ▶{ my ($self, $command, $attrs, $content) = @_;
Subroutine simpleUpdate: not called CC=6 SCAR=37.4
Pod documented
191 0 my $sv = $self->serverVersion;
192 0 ▶ $sv ge '3.1' or error __x"Solr version too old for updates in JSON syntax.";
Branch: 1 branch
BranchTrueFalseTotal
$sv ge "3.1" or &error(&__x("Solr version too old for updates in JSON syntax.")) 0 0 0
193  
194 0 ▶ $attrs ||= {};
Condition0%0/2
$attrs ||= {}
l!l
00
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table0%0/2
A
A= $attrs
Aresult
11
00
195 0 my $params = [ commit => delete $attrs->{commit} ];
196 0 ▶ my $endpoint = $self->endpoint(($sv lt '4.0' ? 'update/json' : 'update'), params => $params);
Branch: 1 branch
BranchTrueFalseTotal
$sv lt '4.0' ? : 0 0 0
197 0 my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
198 0 ▶ my %params = (%$attrs, (!$content ? () : ref $content eq 'HASH' ? %$content : @$content));
Branch: 2 branches
BranchTrueFalseTotal
!$content ? : 0 0 0
ref $content eq 'HASH' ? : 0 0 0
199 0 my $doc = $self->simpleDocument($command, \%params);
200 0 $self->request($endpoint, $result, $doc);
201 0 $result;
202 }
203  
204  
205 sub simpleDocument($;$$)
206 0 ▶{ my ($self, $command, $attrs, $content) = @_;
Subroutine simpleDocument: not called CC=3 SCAR=24.8
Pod documented
207 0 ▶ $attrs ||= {};
Condition0%0/2
$attrs ||= {}
l!l
00
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table0%0/2
A
A= $attrs
Aresult
11
00
208 0 ▶ $content ||= {};
Condition0%0/2
$content ||= {}
l!l
00
MC/DC0%0/1
$content ||= {}
$content
Truth table0%0/2
A
A= $content
Aresult
11
00
209 0 +{ $command => { %$attrs, %$content } }
210 }
211  
212 sub endpoint($@)
213 0 ▶{ my ($self, $action, %args) = @_;
Subroutine endpoint: not called CC=4 SCAR=30.0
Pod documented
214 0 ▶ my $params = $args{params} ||= [];
Condition0%0/2
$args{"params"} ||= []
l!l
00
MC/DC0%0/1
$args{"params"} ||= []
$args{"params"}
Truth table0%0/2
A
A= $args{"params"}
Aresult
11
00
215  
216 0 ▶ if(ref $params eq 'HASH') { $params->{wt} ||= 'json' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq 'HASH') { } 0 0 0
Condition0%0/2
$params->{"wt"} ||= "json"
l!l
00
MC/DC0%0/1
$params->{"wt"} ||= "json"
$params->{"wt"}
Truth table0%0/2
A
A= $params->{"wt"}
Aresult
11
00
217 0 else { $args{params} = [ wt => 'json', @$params ] }
218  
219 0 $self->SUPER::endpoint($action, %args);
220 }
221  
222 2 1;
↑ Summary Solr.pm »