line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Solr; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
647140
|
use Moo; |
|
9
|
|
|
|
|
84136
|
|
|
9
|
|
|
|
|
38
|
|
4
|
|
|
|
|
|
|
|
5
|
9
|
|
|
9
|
|
15113
|
use Types::Standard qw(InstanceOf Object Bool HashRef Maybe); |
|
9
|
|
|
|
|
559686
|
|
|
9
|
|
|
|
|
81
|
|
6
|
9
|
|
|
9
|
|
9986
|
use Scalar::Util qw(blessed); |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
379
|
|
7
|
9
|
|
|
9
|
|
4184
|
use Encode qw(encode); |
|
9
|
|
|
|
|
77128
|
|
|
9
|
|
|
|
|
547
|
|
8
|
9
|
|
|
9
|
|
3885
|
use URI; |
|
9
|
|
|
|
|
34548
|
|
|
9
|
|
|
|
|
261
|
|
9
|
9
|
|
|
9
|
|
1141
|
use LWP::UserAgent; |
|
9
|
|
|
|
|
65743
|
|
|
9
|
|
|
|
|
210
|
|
10
|
9
|
|
|
9
|
|
3631
|
use WebService::Solr::Response; |
|
9
|
|
|
|
|
31
|
|
|
9
|
|
|
|
|
302
|
|
11
|
9
|
|
|
9
|
|
62
|
use HTTP::Request; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
196
|
|
12
|
9
|
|
|
9
|
|
943
|
use HTTP::Headers; |
|
9
|
|
|
|
|
12866
|
|
|
9
|
|
|
|
|
181
|
|
13
|
9
|
|
|
9
|
|
43
|
use XML::Easy::Element; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
156
|
|
14
|
9
|
|
|
9
|
|
147
|
use XML::Easy::Content; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
143
|
|
15
|
9
|
|
|
9
|
|
36
|
use XML::Easy::Text (); |
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
174
|
|
16
|
9
|
|
|
9
|
|
39
|
use Carp qw(confess); |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
10880
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has 'url' => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
isa => InstanceOf['URI'], |
21
|
|
|
|
|
|
|
default => sub { URI->new( 'http://localhost:8983/solr' ) } |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'agent' => |
25
|
|
|
|
|
|
|
( is => 'ro', isa => Object, default => sub { LWP::UserAgent->new } ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has 'autocommit' => ( is => 'ro', isa => Bool, default => 1 ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has 'default_params' => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
isa => HashRef, |
32
|
|
|
|
|
|
|
default => sub { { wt => 'json' } } |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
around default_params => sub { |
36
|
|
|
|
|
|
|
my ($orig, $self, @args) = @_; |
37
|
|
|
|
|
|
|
my $ret = $self->$orig(@args); |
38
|
|
|
|
|
|
|
return wantarray ? %$ret : $ret; |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has 'last_response' => ( |
42
|
|
|
|
|
|
|
is => 'rw', |
43
|
|
|
|
|
|
|
isa => Maybe[InstanceOf['WebService::Solr::Response']], |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
our $VERSION = '0.44'; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub BUILDARGS { |
49
|
7
|
|
|
7
|
1
|
25026
|
my ( $self, $url, $options ) = @_; |
50
|
7
|
|
100
|
|
|
46
|
$options ||= {}; |
51
|
|
|
|
|
|
|
|
52
|
7
|
50
|
|
|
|
20
|
if ( $url ) { |
53
|
0
|
0
|
|
|
|
0
|
$options->{ url } = ref $url ? $url : URI->new( $url ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
7
|
50
|
|
|
|
24
|
if ( exists $options->{ default_params } ) { |
57
|
|
|
|
|
|
|
$options->{ default_params } |
58
|
0
|
|
|
|
|
0
|
= { %{ $options->{ default_params } }, wt => 'json', }; |
|
0
|
|
|
|
|
0
|
|
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
7
|
|
|
|
|
103
|
return $options; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub add { |
65
|
2
|
|
|
2
|
1
|
6780
|
my ( $self, $doc, $params ) = @_; |
66
|
2
|
50
|
|
|
|
9
|
my @docs = ref $doc eq 'ARRAY' ? @$doc : ( $doc ); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my @elements = map { |
69
|
2
|
50
|
|
|
|
4
|
( '', |
|
2
|
50
|
|
|
|
25
|
|
70
|
|
|
|
|
|
|
blessed $_ |
71
|
|
|
|
|
|
|
? $_->to_element |
72
|
|
|
|
|
|
|
: WebService::Solr::Document->new( |
73
|
|
|
|
|
|
|
ref $_ eq 'HASH' ? %$_ : @$_ |
74
|
|
|
|
|
|
|
)->to_element |
75
|
|
|
|
|
|
|
) |
76
|
|
|
|
|
|
|
} @docs; |
77
|
|
|
|
|
|
|
|
78
|
2
|
|
50
|
|
|
12
|
$params ||= {}; |
79
|
2
|
|
|
|
|
10
|
my $e |
80
|
|
|
|
|
|
|
= XML::Easy::Element->new( 'add', $params, |
81
|
|
|
|
|
|
|
XML::Easy::Content->new( [ @elements, '' ] ), |
82
|
|
|
|
|
|
|
); |
83
|
2
|
|
|
|
|
35
|
my $xml = XML::Easy::Text::xml10_write_element( $e ); |
84
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
7
|
my $response = $self->_send_update( $xml ); |
86
|
2
|
|
|
|
|
32
|
return $response->ok; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub update { |
90
|
1
|
|
|
1
|
1
|
348
|
return shift->add( @_ ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub commit { |
94
|
5
|
|
|
5
|
1
|
6312
|
my ( $self, $params ) = @_; |
95
|
5
|
|
50
|
|
|
11
|
$params ||= {}; |
96
|
5
|
|
|
|
|
65
|
my $e = XML::Easy::Element->new( 'commit', $params, [ '' ] ); |
97
|
5
|
|
|
|
|
35
|
my $xml = XML::Easy::Text::xml10_write_element( $e ); |
98
|
5
|
|
|
|
|
17
|
my $response = $self->_send_update( $xml, {}, 0 ); |
99
|
5
|
|
|
|
|
50
|
return $response->ok; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub rollback { |
103
|
1
|
|
|
1
|
1
|
8371
|
my ( $self ) = @_; |
104
|
1
|
|
|
|
|
6
|
my $response = $self->_send_update( '', {}, 0 ); |
105
|
1
|
|
|
|
|
27
|
return $response->ok; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub optimize { |
109
|
5
|
|
|
5
|
1
|
7135
|
my ( $self, $params ) = @_; |
110
|
5
|
|
50
|
|
|
14
|
$params ||= {}; |
111
|
5
|
|
|
|
|
79
|
my $e = XML::Easy::Element->new( 'optimize', $params, [ '' ] ); |
112
|
5
|
|
|
|
|
39
|
my $xml = XML::Easy::Text::xml10_write_element( $e ); |
113
|
5
|
|
|
|
|
20
|
my $response = $self->_send_update( $xml, {}, 0 ); |
114
|
5
|
|
|
|
|
55
|
return $response->ok; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub delete { |
118
|
4
|
|
|
4
|
1
|
24
|
my ( $self, $options ) = @_; |
119
|
|
|
|
|
|
|
|
120
|
4
|
|
|
|
|
6
|
my $xml = ''; |
121
|
4
|
|
|
|
|
14
|
for my $k ( keys %$options ) { |
122
|
6
|
|
|
|
|
10
|
my $v = $options->{ $k }; |
123
|
|
|
|
|
|
|
$xml .= join( |
124
|
|
|
|
|
|
|
'', |
125
|
|
|
|
|
|
|
map { |
126
|
6
|
100
|
|
|
|
18
|
XML::Easy::Text::xml10_write_element( |
|
8
|
|
|
|
|
105
|
|
127
|
|
|
|
|
|
|
XML::Easy::Element->new( $k, {}, [ $_ ] ) ) |
128
|
|
|
|
|
|
|
} ref $v ? @$v : $v |
129
|
|
|
|
|
|
|
); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
4
|
|
|
|
|
17
|
my $response = $self->_send_update( "${xml}" ); |
133
|
4
|
|
|
|
|
47
|
return $response->ok; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub delete_by_id { |
137
|
1
|
|
|
1
|
1
|
6596
|
my ( $self, $id ) = @_; |
138
|
1
|
|
|
|
|
5
|
return $self->delete( { id => $id } ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub delete_by_query { |
142
|
1
|
|
|
1
|
1
|
12
|
my ( $self, $query ) = @_; |
143
|
1
|
|
|
|
|
4
|
return $self->delete( { query => $query } ); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub ping { |
147
|
1
|
|
|
1
|
1
|
6833
|
my ( $self ) = @_; |
148
|
1
|
|
|
|
|
6
|
$self->last_response( WebService::Solr::Response->new( |
149
|
|
|
|
|
|
|
$self->agent->get( $self->_gen_url( 'admin/ping' ) ) ) ); |
150
|
1
|
|
|
|
|
85
|
return $self->last_response->is_success; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub search { |
154
|
1
|
|
|
1
|
1
|
6920
|
my ( $self, $query, $params ) = @_; |
155
|
1
|
|
50
|
|
|
7
|
$params ||= {}; |
156
|
1
|
|
|
|
|
2
|
$params->{ 'q' } = $query; |
157
|
1
|
|
|
|
|
3
|
return $self->generic_solr_request( 'select', $params ); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub auto_suggest { |
161
|
0
|
|
|
0
|
1
|
0
|
shift->generic_solr_request( 'autoSuggest', @_ ); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub generic_solr_request { |
165
|
1
|
|
|
1
|
1
|
3
|
my ( $self, $path, $params ) = @_; |
166
|
1
|
|
50
|
|
|
3
|
$params ||= {}; |
167
|
1
|
|
|
|
|
5
|
return $self->last_response( |
168
|
|
|
|
|
|
|
WebService::Solr::Response->new( |
169
|
|
|
|
|
|
|
$self->agent->post( |
170
|
|
|
|
|
|
|
$self->_gen_url( $path ), |
171
|
|
|
|
|
|
|
Content_Type => 'application/x-www-form-urlencoded; charset=utf-8', |
172
|
|
|
|
|
|
|
Content => { $self->default_params, %$params } ) ) ); |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub _gen_url { |
176
|
19
|
|
|
19
|
|
47
|
my ( $self, $handler ) = @_; |
177
|
|
|
|
|
|
|
|
178
|
19
|
|
|
|
|
110
|
my $url = $self->url->clone; |
179
|
19
|
|
|
|
|
551
|
$url->path( $url->path . "/$handler" ); |
180
|
19
|
|
|
|
|
930
|
return $url; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub _send_update { |
184
|
17
|
|
|
17
|
|
48
|
my ( $self, $xml, $params, $autocommit ) = @_; |
185
|
17
|
100
|
|
|
|
65
|
$autocommit = $self->autocommit unless defined $autocommit; |
186
|
|
|
|
|
|
|
|
187
|
17
|
|
100
|
|
|
56
|
$params ||= {}; |
188
|
17
|
|
|
|
|
47
|
my $url = $self->_gen_url( 'update' ); |
189
|
17
|
|
|
|
|
365
|
$url->query_form( { $self->default_params, %$params } ); |
190
|
17
|
|
|
|
|
1393
|
my $req = HTTP::Request->new( |
191
|
|
|
|
|
|
|
POST => $url, |
192
|
|
|
|
|
|
|
HTTP::Headers->new( Content_Type => 'text/xml; charset=utf-8' ), |
193
|
|
|
|
|
|
|
'' . encode( 'utf8', "$xml" ) |
194
|
|
|
|
|
|
|
); |
195
|
|
|
|
|
|
|
|
196
|
17
|
|
|
|
|
1952
|
my $http_response = $self->agent->request( $req ); |
197
|
17
|
50
|
|
|
|
371732
|
if ( $http_response->is_error ) { |
198
|
0
|
|
|
|
|
0
|
confess($http_response->status_line . ': ' . $http_response->content); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
17
|
|
|
|
|
1482
|
$self->last_response( WebService::Solr::Response->new( $http_response ) ); |
202
|
|
|
|
|
|
|
|
203
|
17
|
50
|
|
|
|
1232
|
$self->commit if $autocommit; |
204
|
|
|
|
|
|
|
|
205
|
17
|
|
|
|
|
253
|
return $self->last_response; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
9
|
|
|
9
|
|
62
|
no Moo; |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
42
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
__END__ |