line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SolarBeam; |
2
|
2
|
|
|
2
|
|
54145
|
use Mojo::Base -base; |
|
2
|
|
|
|
|
112729
|
|
|
2
|
|
|
|
|
17
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
914
|
use Mojo::UserAgent; |
|
2
|
|
|
|
|
193765
|
|
|
2
|
|
|
|
|
21
|
|
5
|
2
|
|
|
2
|
|
54
|
use Mojo::Parameters; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
21
|
|
6
|
2
|
|
|
2
|
|
39
|
use Mojo::URL; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
9
|
|
7
|
2
|
|
|
2
|
|
804
|
use SolarBeam::Query; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
10
|
|
8
|
2
|
|
|
2
|
|
755
|
use SolarBeam::Response; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
10
|
|
9
|
2
|
|
|
2
|
|
61
|
use SolarBeam::Util 'escape'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2606
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has ua => sub { Mojo::UserAgent->new }; |
14
|
|
|
|
|
|
|
has default_query => sub { {} }; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub autocomplete { |
17
|
2
|
|
|
2
|
1
|
490
|
my $cb = pop; |
18
|
2
|
|
|
|
|
8
|
my ($self, $prefix, %options) = @_; |
19
|
2
|
|
100
|
|
|
10
|
my $postfix = delete $options{'-postfix'} || '\w+'; |
20
|
|
|
|
|
|
|
|
21
|
2
|
|
|
|
|
26
|
$options{'regex.flag'} = 'case_insensitive'; |
22
|
2
|
|
|
|
|
8
|
$options{'regex'} = quotemeta($prefix) . $postfix; |
23
|
2
|
|
|
|
|
8
|
my $options = {terms => \%options, -endpoint => 'terms'}; |
24
|
2
|
|
|
|
|
10
|
my $url = $self->_build_url($options); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Mojo::IOLoop->delay( |
27
|
2
|
|
|
2
|
|
263
|
sub { $self->ua->get($url, shift->begin) }, |
28
|
|
|
|
|
|
|
sub { |
29
|
2
|
|
|
2
|
|
82685
|
my ($delay, $tx) = @_; |
30
|
2
|
|
|
|
|
16
|
$self->$cb(SolarBeam::Response->new->parse($tx)); |
31
|
|
|
|
|
|
|
} |
32
|
2
|
|
|
|
|
17
|
); |
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
|
|
222
|
return $self; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub new { |
38
|
3
|
|
|
3
|
1
|
591
|
my $self = shift->SUPER::new(@_); |
39
|
3
|
100
|
|
|
|
33
|
$self->url($self->{url}) if $self->{url}; |
40
|
3
|
|
|
|
|
6
|
$self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub search { |
44
|
1
|
|
|
1
|
1
|
12
|
my $cb = pop; |
45
|
1
|
|
|
|
|
3
|
my ($self, $query, %options) = @_; |
46
|
1
|
|
|
|
|
2
|
my $options = \%options; |
47
|
1
|
|
|
|
|
2
|
my $page = $options->{page}; |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
2
|
$options->{-query} = $query; |
50
|
1
|
|
|
|
|
7
|
my $url = $self->_build_url($options); |
51
|
1
|
|
|
|
|
3
|
my $q = $url->query; |
52
|
1
|
|
|
|
|
9
|
$url->query(Mojo::Parameters->new); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Mojo::IOLoop->delay( |
55
|
1
|
|
|
1
|
|
181
|
sub { $self->ua->post($url, form => $q->to_hash, shift->begin) }, |
56
|
|
|
|
|
|
|
sub { |
57
|
1
|
|
|
1
|
|
13581
|
my ($delay, $tx) = @_; |
58
|
1
|
|
|
|
|
12
|
my $res = SolarBeam::Response->new->parse($tx); |
59
|
|
|
|
|
|
|
|
60
|
1
|
50
|
33
|
|
|
5
|
if ($page && !$res->error) { |
61
|
0
|
|
|
|
|
0
|
$res->pager->current_page($page); |
62
|
0
|
|
|
|
|
0
|
$res->pager->entries_per_page($options->{rows}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
3
|
$self->$cb($res); |
66
|
|
|
|
|
|
|
} |
67
|
1
|
|
|
|
|
35
|
); |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
151
|
return $self; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub url { |
73
|
19
|
|
|
19
|
1
|
17720
|
my $self = shift; |
74
|
19
|
100
|
66
|
|
|
80
|
return $self->{url} ||= Mojo::URL->new('http://localhost:8983/solr') unless @_; |
75
|
3
|
|
|
|
|
12
|
$self->{url} = Mojo::URL->new(shift); |
76
|
3
|
|
|
|
|
760
|
return $self; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _build_hash { |
80
|
3
|
|
|
3
|
|
7
|
my ($self, %fields) = @_; |
81
|
3
|
|
|
|
|
4
|
my @query; |
82
|
|
|
|
|
|
|
|
83
|
3
|
|
|
|
|
8
|
for my $field (keys %fields) { |
84
|
3
|
|
|
|
|
5
|
my $val = $fields{$field}; |
85
|
3
|
100
|
|
|
|
21
|
my @vals = ref($val) eq 'ARRAY' ? @{$val} : $val; |
|
1
|
|
|
|
|
3
|
|
86
|
3
|
|
|
|
|
6
|
push @query, join(' OR ', map { $field . ':(' . escape($_) . ')' } @vals); |
|
4
|
|
|
|
|
11
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
3
|
|
|
|
|
16
|
return '(' . join(' AND ', @query) . ')'; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _build_query { |
93
|
10
|
|
|
10
|
|
21
|
my ($self, $query) = @_; |
94
|
|
|
|
|
|
|
|
95
|
10
|
|
|
|
|
14
|
my $type = ref($query); |
96
|
10
|
100
|
|
|
|
30
|
if ($type eq 'HASH') { |
|
|
100
|
|
|
|
|
|
97
|
3
|
|
|
|
|
5
|
$self->_build_hash(%{$query}); |
|
3
|
|
|
|
|
8
|
|
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
elsif ($type eq 'ARRAY') { |
100
|
3
|
|
|
|
|
8
|
my ($raw, @params) = @$query; |
101
|
3
|
|
|
|
|
9
|
$raw =~ s|%@|escape(shift @params)|ge; |
|
2
|
|
|
|
|
5
|
|
102
|
3
|
|
|
|
|
9
|
my %params = @params; |
103
|
3
|
|
|
|
|
12
|
$raw =~ s|%([a-z]+)|escape($params{$1})|ge; |
|
3
|
|
|
|
|
10
|
|
104
|
3
|
|
|
|
|
16
|
$raw; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
4
|
|
|
|
|
12
|
$query; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _build_url { |
112
|
12
|
|
|
12
|
|
27
|
my ($self, $options) = @_; |
113
|
12
|
|
|
|
|
32
|
my $endpoint = delete $options->{-endpoint}; |
114
|
12
|
|
|
|
|
16
|
my $query = delete $options->{-query}; |
115
|
12
|
|
|
|
|
25
|
my $url = $self->url->clone; |
116
|
|
|
|
|
|
|
|
117
|
12
|
|
100
|
|
|
730
|
$url->path($endpoint || 'select'); |
118
|
12
|
100
|
|
|
|
1534
|
$url->query(q => $self->_build_query($query)) if $query; |
119
|
12
|
|
|
|
|
104
|
$url->query($self->default_query); |
120
|
12
|
|
|
|
|
382
|
$url->query({wt => 'json'}); |
121
|
|
|
|
|
|
|
|
122
|
12
|
100
|
|
|
|
323
|
if ($options->{page}) { |
123
|
1
|
|
|
|
|
5
|
$self->_handle_page($options->{page}, $options); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
12
|
100
|
|
|
|
26
|
if ($options->{fq}) { |
127
|
3
|
|
|
|
|
9
|
$self->_handle_fq($options->{fq}, $options); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
12
|
100
|
|
|
|
26
|
if ($options->{facet}) { |
131
|
2
|
|
|
|
|
4
|
$self->_handle_facet($options->{facet}, $options); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
12
|
100
|
|
|
|
35
|
if ($options->{terms}) { |
135
|
3
|
|
|
|
|
12
|
$self->_handle_nested_hash('terms', $options->{terms}, $options); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
12
|
|
|
|
|
44
|
$url->query($options); |
139
|
|
|
|
|
|
|
|
140
|
12
|
|
|
|
|
381
|
return $url; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _handle_fq { |
144
|
3
|
|
|
3
|
|
6
|
my ($self, $fq, $options) = @_; |
145
|
|
|
|
|
|
|
|
146
|
3
|
100
|
|
|
|
9
|
if (ref($fq) eq 'ARRAY') { |
147
|
2
|
|
|
|
|
2
|
my @queries = map { $self->_build_query($_) } @{$fq}; |
|
4
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
4
|
|
148
|
2
|
|
|
|
|
6
|
$options->{fq} = \@queries; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
else { |
151
|
1
|
|
|
|
|
3
|
$options->{fq} = $self->_build_query($fq); |
152
|
|
|
|
|
|
|
} |
153
|
3
|
|
|
|
|
5
|
return; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _handle_facet { |
157
|
2
|
|
|
2
|
|
5
|
my ($self, $facet, $options) = @_; |
158
|
2
|
|
|
|
|
5
|
$self->_handle_nested_hash('facet', $facet, $options); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _handle_nested_hash { |
162
|
25
|
|
|
25
|
|
39
|
my ($self, $prefix, $content, $options) = @_; |
163
|
25
|
|
|
|
|
44
|
my $type = ref $content; |
164
|
|
|
|
|
|
|
|
165
|
25
|
100
|
|
|
|
38
|
if ($type eq 'HASH') { |
166
|
6
|
100
|
|
|
|
16
|
$content->{-value} or $content->{-value} = 'true'; |
167
|
|
|
|
|
|
|
|
168
|
6
|
|
|
|
|
9
|
for my $key (keys %{$content}) { |
|
6
|
|
|
|
|
15
|
|
169
|
20
|
|
|
|
|
22
|
my $name = $prefix; |
170
|
20
|
100
|
|
|
|
38
|
$name .= '.' . $key if $key ne '-value'; |
171
|
20
|
|
|
|
|
35
|
$self->_handle_nested_hash($name, $content->{$key}, $options); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
else { |
175
|
19
|
|
|
|
|
36
|
$options->{$prefix} = $content; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub _handle_page { |
180
|
1
|
|
|
1
|
|
3
|
my ($self, $page, $options) = @_; |
181
|
1
|
50
|
|
|
|
4
|
die "You must provide both page and rows" unless $options->{rows}; |
182
|
1
|
|
|
|
|
4
|
$options->{start} = ($page - 1) * $options->{rows}; |
183
|
1
|
|
|
|
|
3
|
return delete $options->{page}; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=encoding utf8 |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 NAME |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
SolarBeam - Async Solr search driver |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 VERSION |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
0.04 |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head1 SYNOPSIS |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
use SolarBeam; |
201
|
|
|
|
|
|
|
my $solr = SolarBeam->new; |
202
|
|
|
|
|
|
|
$solr->search(...); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 DESCRIPTION |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Interface to acquire Solr index engine connections. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
L is currently EXPERIMENTAL. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
L implements the the following attributes. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 ua |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
$ua = $self->ua |
217
|
|
|
|
|
|
|
$self = $self->ua(Mojo::UserAgent->new); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
A L compatible object. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 url |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
$url = $self->url; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Solr endpoint as a L object. Note that passing in L as a |
226
|
|
|
|
|
|
|
string to L also works. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head2 default_query |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
A hashref with default parameters used for every query. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 METHODS |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 new |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
$self = SolarBeam->new(%attributes); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Object constructor. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head2 search |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
$self = $self->search($query, [%options], sub { my ($self, $res) = @_; }); |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
Used to search for data in Solr. C<$res> is a L object. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Example C<$query>: |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=over 2 |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item * Hash |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
$self->search({surname => q("Thorsen"), age => [33, 34]}); |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
The query above will result in this Solr query: |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
(surname:("Thorsen") AND age:(33) OR age:(34)) |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=item * String |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
$self->search("active:1"); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
The query above will result in this Solr query: |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
active:1 |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=back |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
C<%options> can hold Solr query parameters and some special instuctions |
269
|
|
|
|
|
|
|
to this module, such a "page" and "rows". |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=over 2 |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item * page |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Used to calculate the offset together with L. Will also be used to set |
276
|
|
|
|
|
|
|
L attributes in L: |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
$res->pager->current_page($page); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=item * rows |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Used to calculate the offset together with L. Will also be used to set |
283
|
|
|
|
|
|
|
L attributes in L: |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
$res->pager->entries_per_page($rows); |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=back |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head2 autocomplete |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
$self = $self->autocomplete($prefix, [%options], sub { my ($self, $res) = @_; }); |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
TODO. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
C<$res> is a L object. |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
C<%options> can be: |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=over 2 |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=item * -postfix - defaults to \w+ |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=item * regex.flag - |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=item * regex - |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=back |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Copyright (C) 2011-2016, Magnus Holm |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
314
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 AUTHOR |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Magnus Holm - C |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
Nicolas Mendoza - C |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=cut |