| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package SolarBeam; |
|
2
|
2
|
|
|
2
|
|
35142
|
use Mojo::Base -base; |
|
|
2
|
|
|
|
|
14375
|
|
|
|
2
|
|
|
|
|
25
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
1086
|
use Mojo::UserAgent; |
|
|
2
|
|
|
|
|
366117
|
|
|
|
2
|
|
|
|
|
20
|
|
|
5
|
2
|
|
|
2
|
|
63
|
use Mojo::Parameters; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
17
|
|
|
6
|
2
|
|
|
2
|
|
76
|
use Mojo::URL; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
11
|
|
|
7
|
2
|
|
|
2
|
|
1133
|
use SolarBeam::Query; |
|
|
2
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
14
|
|
|
8
|
2
|
|
|
2
|
|
1166
|
use SolarBeam::Response; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
26
|
|
|
9
|
2
|
|
|
2
|
|
95
|
use SolarBeam::Util 'escape'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
3801
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has ua => sub { Mojo::UserAgent->new }; |
|
14
|
|
|
|
|
|
|
has default_query => sub { {} }; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub autocomplete { |
|
17
|
2
|
|
|
2
|
1
|
895
|
my $cb = pop; |
|
18
|
2
|
|
|
|
|
10
|
my ($self, $prefix, %options) = @_; |
|
19
|
2
|
|
100
|
|
|
13
|
my $postfix = delete $options{'-postfix'} || '\w+'; |
|
20
|
|
|
|
|
|
|
|
|
21
|
2
|
|
|
|
|
6
|
$options{'regex.flag'} = 'case_insensitive'; |
|
22
|
2
|
|
|
|
|
9
|
$options{'regex'} = quotemeta($prefix) . $postfix; |
|
23
|
2
|
|
|
|
|
10
|
my $options = {terms => \%options, -endpoint => 'terms'}; |
|
24
|
2
|
|
|
|
|
11
|
my $url = $self->_build_url($options); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Mojo::IOLoop->delay( |
|
27
|
2
|
|
|
2
|
|
307
|
sub { $self->ua->get($url, shift->begin) }, |
|
28
|
|
|
|
|
|
|
sub { |
|
29
|
2
|
|
|
2
|
|
92834
|
my ($delay, $tx) = @_; |
|
30
|
2
|
|
|
|
|
17
|
$self->$cb(SolarBeam::Response->new->parse($tx)); |
|
31
|
|
|
|
|
|
|
} |
|
32
|
2
|
|
|
|
|
26
|
); |
|
33
|
|
|
|
|
|
|
|
|
34
|
2
|
|
|
|
|
344
|
return $self; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub new { |
|
38
|
3
|
|
|
3
|
1
|
435
|
my $self = shift->SUPER::new(@_); |
|
39
|
3
|
100
|
|
|
|
43
|
$self->url($self->{url}) if $self->{url}; |
|
40
|
3
|
|
|
|
|
8
|
$self; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub search { |
|
44
|
1
|
|
|
1
|
1
|
19
|
my $cb = pop; |
|
45
|
1
|
|
|
|
|
5
|
my ($self, $query, %options) = @_; |
|
46
|
1
|
|
|
|
|
2
|
my $options = \%options; |
|
47
|
1
|
|
|
|
|
5
|
my $page = $options->{page}; |
|
48
|
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
5
|
$options->{-query} = $query; |
|
50
|
1
|
|
|
|
|
5
|
my $url = $self->_build_url($options); |
|
51
|
1
|
|
|
|
|
5
|
my $q = $url->query; |
|
52
|
1
|
|
|
|
|
14
|
$url->query(Mojo::Parameters->new); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Mojo::IOLoop->delay( |
|
55
|
1
|
|
|
1
|
|
234
|
sub { $self->ua->post($url, form => $q->to_hash, shift->begin) }, |
|
56
|
|
|
|
|
|
|
sub { |
|
57
|
1
|
|
|
1
|
|
11728
|
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
|
|
|
|
|
5
|
$self->$cb($res); |
|
66
|
|
|
|
|
|
|
} |
|
67
|
1
|
|
|
|
|
44
|
); |
|
68
|
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
213
|
return $self; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub url { |
|
73
|
19
|
|
|
19
|
1
|
22085
|
my $self = shift; |
|
74
|
19
|
100
|
66
|
|
|
125
|
return $self->{url} ||= Mojo::URL->new('http://localhost:8983/solr') unless @_; |
|
75
|
3
|
|
|
|
|
18
|
$self->{url} = Mojo::URL->new(shift); |
|
76
|
3
|
|
|
|
|
529
|
return $self; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _build_hash { |
|
80
|
3
|
|
|
3
|
|
7
|
my ($self, %fields) = @_; |
|
81
|
3
|
|
|
|
|
4
|
my @query; |
|
82
|
|
|
|
|
|
|
|
|
83
|
3
|
|
|
|
|
7
|
for my $field (keys %fields) { |
|
84
|
3
|
|
|
|
|
4
|
my $val = $fields{$field}; |
|
85
|
3
|
100
|
|
|
|
9
|
my @vals = ref($val) eq 'ARRAY' ? @{$val} : $val; |
|
|
1
|
|
|
|
|
3
|
|
|
86
|
3
|
|
|
|
|
6
|
push @query, join(' OR ', map { $field . ':(' . escape($_) . ')' } @vals); |
|
|
4
|
|
|
|
|
12
|
|
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
3
|
|
|
|
|
17
|
return '(' . join(' AND ', @query) . ')'; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _build_query { |
|
93
|
10
|
|
|
10
|
|
19
|
my ($self, $query) = @_; |
|
94
|
|
|
|
|
|
|
|
|
95
|
10
|
|
|
|
|
19
|
my $type = ref($query); |
|
96
|
10
|
100
|
|
|
|
31
|
if ($type eq 'HASH') { |
|
|
|
100
|
|
|
|
|
|
|
97
|
3
|
|
|
|
|
5
|
$self->_build_hash(%{$query}); |
|
|
3
|
|
|
|
|
11
|
|
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
elsif ($type eq 'ARRAY') { |
|
100
|
3
|
|
|
|
|
10
|
my ($raw, @params) = @$query; |
|
101
|
3
|
|
|
|
|
12
|
$raw =~ s|%@|escape(shift @params)|ge; |
|
|
2
|
|
|
|
|
9
|
|
|
102
|
3
|
|
|
|
|
12
|
my %params = @params; |
|
103
|
3
|
|
|
|
|
15
|
$raw =~ s|%([a-z]+)|escape($params{$1})|ge; |
|
|
3
|
|
|
|
|
13
|
|
|
104
|
3
|
|
|
|
|
15
|
$raw; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
else { |
|
107
|
4
|
|
|
|
|
16
|
$query; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _build_url { |
|
112
|
12
|
|
|
12
|
|
31
|
my ($self, $options) = @_; |
|
113
|
12
|
|
|
|
|
35
|
my $endpoint = delete $options->{-endpoint}; |
|
114
|
12
|
|
|
|
|
28
|
my $query = delete $options->{-query}; |
|
115
|
12
|
|
|
|
|
29
|
my $url = $self->url->clone; |
|
116
|
|
|
|
|
|
|
|
|
117
|
12
|
|
100
|
|
|
846
|
$url->path($endpoint || 'select'); |
|
118
|
12
|
100
|
|
|
|
1870
|
$url->query(q => $self->_build_query($query)) if $query; |
|
119
|
12
|
|
|
|
|
114
|
$url->query($self->default_query); |
|
120
|
12
|
|
|
|
|
443
|
$url->query({wt => 'json'}); |
|
121
|
|
|
|
|
|
|
|
|
122
|
12
|
100
|
|
|
|
346
|
if ($options->{page}) { |
|
123
|
1
|
|
|
|
|
4
|
$self->_handle_page($options->{page}, $options); |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
|
|
126
|
12
|
100
|
|
|
|
56
|
if ($options->{fq}) { |
|
127
|
3
|
|
|
|
|
10
|
$self->_handle_fq($options->{fq}, $options); |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
12
|
100
|
|
|
|
33
|
if ($options->{facet}) { |
|
131
|
2
|
|
|
|
|
9
|
$self->_handle_facet($options->{facet}, $options); |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
12
|
100
|
|
|
|
29
|
if ($options->{terms}) { |
|
135
|
3
|
|
|
|
|
14
|
$self->_handle_nested_hash('terms', $options->{terms}, $options); |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
12
|
|
|
|
|
39
|
$url->query($options); |
|
139
|
|
|
|
|
|
|
|
|
140
|
12
|
|
|
|
|
439
|
return $url; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _handle_fq { |
|
144
|
3
|
|
|
3
|
|
5
|
my ($self, $fq, $options) = @_; |
|
145
|
|
|
|
|
|
|
|
|
146
|
3
|
100
|
|
|
|
9
|
if (ref($fq) eq 'ARRAY') { |
|
147
|
2
|
|
|
|
|
4
|
my @queries = map { $self->_build_query($_) } @{$fq}; |
|
|
4
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
5
|
|
|
148
|
2
|
|
|
|
|
5
|
$options->{fq} = \@queries; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
else { |
|
151
|
1
|
|
|
|
|
2
|
$options->{fq} = $self->_build_query($fq); |
|
152
|
|
|
|
|
|
|
} |
|
153
|
3
|
|
|
|
|
5
|
return; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _handle_facet { |
|
157
|
2
|
|
|
2
|
|
3
|
my ($self, $facet, $options) = @_; |
|
158
|
2
|
|
|
|
|
8
|
$self->_handle_nested_hash('facet', $facet, $options); |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _handle_nested_hash { |
|
162
|
25
|
|
|
25
|
|
38
|
my ($self, $prefix, $content, $options) = @_; |
|
163
|
25
|
|
|
|
|
33
|
my $type = ref $content; |
|
164
|
|
|
|
|
|
|
|
|
165
|
25
|
100
|
|
|
|
42
|
if ($type eq 'HASH') { |
|
166
|
6
|
100
|
|
|
|
31
|
$content->{-value} or $content->{-value} = 'true'; |
|
167
|
|
|
|
|
|
|
|
|
168
|
6
|
|
|
|
|
8
|
for my $key (keys %{$content}) { |
|
|
6
|
|
|
|
|
21
|
|
|
169
|
20
|
|
|
|
|
22
|
my $name = $prefix; |
|
170
|
20
|
100
|
|
|
|
50
|
$name .= '.' . $key if $key ne '-value'; |
|
171
|
20
|
|
|
|
|
59
|
$self->_handle_nested_hash($name, $content->{$key}, $options); |
|
172
|
|
|
|
|
|
|
} |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
else { |
|
175
|
19
|
|
|
|
|
53
|
$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
|
|
|
|
|
2
|
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.03 |
|
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 |