-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathSearch.pm
398 lines (345 loc) · 12.2 KB
/
Search.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package MetaCPAN::Query::Search;
use MetaCPAN::Moose;
use Const::Fast qw( const );
use Hash::Merge qw( merge );
use List::Util qw( min uniq );
use Log::Contextual qw( :log :dlog );
use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Types::TypeTiny qw( Object Str );
use MetaCPAN::Util qw( MAX_RESULT_WINDOW hit_total true false );
use MooseX::StrictConstructor;
with 'MetaCPAN::Query::Role::Common';
const my $RESULTS_PER_RUN => 200;
const my @ROGUE_DISTRIBUTIONS => qw(
Acme-DependOnEverything
Bundle-Everything
kurila
perl-5.005_02+apache1.3.3+modperl
perlbench
perl_debug
perl_mlb
pod2texi
spodcxx
);
sub search_for_first_result {
my ( $self, $search_term ) = @_;
my $es_query = $self->build_query($search_term);
my $es_results = $self->run_query( file => $es_query );
my $data = $es_results->{hits}{hits}[0];
return $data->{_source};
}
=head2 search_web
search_web( $search_term, $from, $page_size, $collapsed );
- search_term:
- can be unqualified string e.g. 'paging'
- can be author e.g: 'author:LLAP'
- can be module e.g.: 'module:Data::Pageset'
- can be distribution e.g.: 'dist:Data-Pageset'
- from: where in result set to start, int
- page_size: number of results per page, int
- collapsed: whether to merge results by dist or not
=cut
sub search_web {
my ( $self, $search_term, $page, $page_size, $collapsed,
$max_collapsed_hits )
= @_;
$page_size //= 20;
$page //= 1;
if ( $page * $page_size >= MAX_RESULT_WINDOW ) {
return {
results => [],
total => 0,
tool => 0,
colapsed => $collapsed ? true : false,
};
}
$search_term =~ s{([+=><!&|\(\)\{\}[\]\^"~*?\\/])}{\\$1}g;
# munge the search_term
# these would be nicer if we had variable-length lookbehinds...
# Allow q = 'author:LLAP' or 'module:Data::Page' or 'dist:'
# We are mapping to correct ES fields here - relied on by metacpan-web
# tests.
#
# The exceptions below are used specifically by the front end search.
$search_term #
=~ s{(^|\s)author:([a-zA-Z]+)(?=\s|$)}{$1author:\U$2\E}g;
$search_term
=~ s/(^|\s)dist(ribution)?:([\w-]+)(?=\s|$)/$1distribution:$3/g;
$search_term
=~ s/(^|\s)module:(\w[\w:]*)(?=\s|$)/$1module.name.analyzed:$2/g;
my $results
= $collapsed // $search_term !~ /(distribution|module\.name\S*):/
? $self->_search_collapsed( $search_term, $page, $page_size,
$max_collapsed_hits )
: $self->_search_expanded( $search_term, $page, $page_size );
return $results;
}
sub _search_expanded {
my ( $self, $search_term, $page, $page_size ) = @_;
# Used for distribution and module searches, the limit is included in
# the query and ES does the right thing (because we are not collapsing
# results by distribution).
my $es_query = $self->build_query(
$search_term,
{
size => $page_size,
from => ( $page - 1 ) * $page_size,
}
);
my $es_results = $self->run_query( file => $es_query );
# Extract results from es
my $results = $self->_extract_results($es_results);
$results = [
map { {
hits => [$_],
distribution => $_->{distribution},
total => 1,
} } @$results
];
my $return = {
results => $results,
total => hit_total($es_results),
took => $es_results->{took},
collapsed => false,
};
return $return;
}
sub _search_collapsed {
my ( $self, $search_term, $page, $page_size, $max_collapsed_hits ) = @_;
$max_collapsed_hits ||= 5;
my $from = ( $page - 1 ) * $page_size;
my $total_size = $page * $page_size;
my $es_query_opts = {
size => 0,
_source => [ qw(
) ],
};
my $es_query = $self->build_query( $search_term, $es_query_opts );
my $source = delete $es_query->{_source};
my $script_key = $self->es->api_version ge '5_0' ? 'source' : 'inline';
$es_query->{aggregations} = {
by_dist => {
terms => {
size => $total_size,
field => 'distribution',
order => {
max_score => 'desc',
},
},
aggregations => {
top_files => {
top_hits => {
_source => $source,
size => $max_collapsed_hits,
},
},
max_score => {
max => {
script => {
lang => "expression",
$script_key => "_score",
},
},
},
},
},
total_dists => {
cardinality => {
field => 'distribution',
},
},
};
my $es_results = $self->run_query( file => $es_query );
my $output = {
results => [],
total => $es_results->{aggregations}{total_dists}{value},
took => $es_results->{took},
collapsed => true,
};
my $last = min( $total_size - 1,
$#{ $es_results->{aggregations}{by_dist}{buckets} } );
my @dists = @{ $es_results->{aggregations}{by_dist}{buckets} }
[ $from .. $last ];
@{ $output->{results} } = map {
+{
hits => $self->_extract_results( $_->{top_files} ),
distribution => $_->{key},
total => $_->{doc_count},
};
} @dists;
return $output;
}
sub build_query {
my ( $self, $search_term, $params ) = @_;
$params //= {};
( my $clean = $search_term ) =~ s/::/ /g;
my $query = {
bool => {
filter => [
{ term => { status => 'latest' } },
{ term => { authorized => true } },
{ term => { indexed => true } },
{
bool => {
should => [
{
bool => {
must => [
{
exists =>
{ field => 'module.name' }
},
{
term =>
{ 'module.indexed' => true }
}
],
}
},
{ exists => { field => 'documentation' } },
],
}
},
],
must_not => [ {
terms => {
distribution => [ $self->query->distribution->rogue_list ]
}
} ],
must => [
{
bool => {
should => [
# exact matches result in a huge boost
{
term => {
'documentation' => {
value => $search_term,
boost => 20,
}
}
},
{
term => {
'module.name' => {
value => $search_term,
boost => 20,
}
}
},
# take the maximum score from the module name and the abstract/pod
{
dis_max => {
queries => [
{
query_string => {
fields => [
qw(documentation.analyzed^2 module.name.analyzed^2 distribution.analyzed),
qw(documentation.camelcase module.name.camelcase distribution.camelcase)
],
query => $clean,
boost => 3,
default_operator => 'AND',
allow_leading_wildcard =>
false,
}
},
{
query_string => {
fields => [
qw(abstract.analyzed pod.analyzed)
],
query => $clean,
default_operator => 'AND',
allow_leading_wildcard =>
false,
},
},
],
}
},
],
}
},
],
},
};
my $script_key = $self->es->api_version ge '5_0' ? 'source' : 'inline';
$query = {
function_score => {
script_score => {
# prefer shorter module names
script => {
lang => 'expression',
$script_key =>
"_score - (doc['documentation_length'].value == 0 ? 26 : doc['documentation_length'].value)/400",
},
},
query => {
boosting => {
negative_boost => 0.5,
positive => $query,
negative => {
bool => {
should => [
{
term => { 'mime' => 'text/x-script.perl' }
},
{ term => { 'deprecated' => true } },
],
},
},
},
},
},
};
my $search = merge(
$params,
{
query => $query,
_source => [ qw(
module
abstract
author
authorized
date
description
dist_fav_count
distribution
documentation
id
indexed
path
pod_lines
release
status
) ],
}
);
# Ensure our requested fields are unique so that Elasticsearch doesn't
# return us the same value multiple times in an unexpected arrayref.
$search->{_source} = [ uniq @{ $search->{_source} || [] } ];
return $search;
}
sub run_query {
my ( $self, $doc, $es_query ) = @_;
return $self->es->search(
es_doc_path($doc),
body => $es_query,
search_type => 'dfs_query_then_fetch',
);
}
sub _extract_results {
my ( $self, $es_results ) = @_;
return [
map {
my $res = $_;
+{
favorites => delete $res->{_source}->{dist_fav_count},
%{ $res->{_source} },
score => $res->{_score},
}
} @{ $es_results->{hits}{hits} }
];
}
1;