@@ -271,74 +271,33 @@ sub history {
271
271
return $search -> sort ( [ { date => ' desc' } ] );
272
272
}
273
273
274
- sub autocomplete {
275
- my ( $self , @terms ) = @_ ;
276
-
277
- my $query = {
278
- bool => {
279
- must => [
280
- {
281
- multi_match => {
282
- query => join ( q{ } , @terms ),
283
- type => ' most_fields' ,
284
- fields => [ ' documentation' , ' documentation.*' ],
285
- analyzer => ' camelcase' ,
286
- minimum_should_match => ' 80%'
287
- }
288
- },
289
- { exists => { field => ' documentation' } },
290
- { term => { status => ' latest' } },
291
- { term => { indexed => true } },
292
- { term => { authorized => true } }
293
- ],
294
- must_not =>
295
- [ { terms => { distribution => \@ROGUE_DISTRIBUTIONS } }, ],
296
- },
297
- };
298
-
299
- my $data = $self -> es-> search(
300
- search_type => ' dfs_query_then_fetch' ,
301
- es_doc_path(' file' ),
302
- body => {
303
- query => $query ,
304
- sort => [ ' _score' , ' documentation' ],
305
- _source => [qw( documentation release author distribution ) ],
306
- },
307
- );
308
-
309
- # this is backcompat. we don't use this end point.
310
- $_ -> {fields } = delete $_ -> {_source } for @{ $data -> {hits }{hits } };
311
-
312
- return $data ;
313
- }
314
-
315
- sub autocomplete_suggester {
274
+ sub _autocomplete {
316
275
my ( $self , $query ) = @_ ;
317
- return $self unless $query ;
318
276
319
277
my $search_size = 100;
320
278
321
- my $suggestions = $self -> es-> suggest( {
279
+ my $sugg_res = $self -> es-> search(
322
280
es_doc_path(' file' ),
323
281
body => {
324
- documentation => {
325
- text => $query ,
326
- completion => {
327
- field => " suggest" ,
328
- size => $search_size ,
329
- }
282
+ suggest => {
283
+ documentation => {
284
+ text => $query ,
285
+ completion => {
286
+ field => " suggest" ,
287
+ size => $search_size ,
288
+ },
289
+ },
330
290
}
331
291
},
332
- } );
292
+ );
333
293
334
294
my %docs ;
335
-
336
- for my $suggest ( @{ $suggestions -> {documentation }[0]{options } } ) {
295
+ for my $suggest ( @{ $sugg_res -> {suggest }{documentation }[0]{options } } ) {
337
296
$docs { $suggest -> {text } } = max grep {defined }
338
297
( $docs { $suggest -> {text } }, $suggest -> {score } );
339
298
}
340
299
341
- my $data = $self -> es-> search( {
300
+ my $res = $self -> es-> search(
342
301
es_doc_path(' file' ),
343
302
body => {
344
303
query => {
@@ -366,34 +325,91 @@ sub autocomplete_suggester {
366
325
) ],
367
326
size => $search_size ,
368
327
},
369
- } );
328
+ );
329
+
330
+ my $hits = $res -> {hits }{hits };
331
+
332
+ my $fav_res
333
+ = $self -> agg_by_distributions(
334
+ [ map $_ -> {_source }{distribution }, @$hits ] );
335
+
336
+ my $favs = $fav_res -> {favorites };
370
337
371
338
my %valid = map {
372
- my %record = %{ $_ -> {_source } };
373
- $record {name } = delete $record {documentation }; # rename
374
- ( $record {name } => \%record );
375
- } @{ $data -> {hits }{hits } };
339
+ my $source = $_ -> {_source };
340
+ (
341
+ $source -> {documentation } => {
342
+ %$source , favorites => $favs -> { $source -> {distribution } },
343
+ }
344
+ );
345
+ } @{ $res -> {hits }{hits } };
376
346
377
347
# remove any exact match, it will be added later
378
348
my $exact = delete $valid {$query };
379
349
380
- my $favorites
381
- = $self -> agg_by_distributions(
382
- [ map { $_ -> {distribution } } values %valid ] )-> {favorites };
383
-
384
350
no warnings ' uninitialized' ;
385
351
my @sorted = map { $valid {$_ } }
386
352
sort {
387
- $valid {$a }-> {deprecated } <=> $valid {$b }-> {deprecated }
388
- || $favorites -> { $valid {$b }-> {distribution } }
389
- <=> $favorites -> { $valid {$a }-> {distribution } }
390
- || $docs {$b } <=> $docs {$a }
391
- || length ($a ) <=> length ($b )
353
+ my $a_data = $valid {$a };
354
+ my $b_data = $valid {$b };
355
+ $a_data -> {deprecated } <=> $b_data -> {deprecated }
356
+ || $b_data -> {favorites } <=> $a_data -> {favorites }
357
+ || $docs {$b } <=> $docs {$a }
358
+ || length ($a ) <=> length ($b )
392
359
|| $a cmp $b
393
360
}
394
361
keys %valid ;
395
362
396
- return +{ suggestions => [ grep {defined } ( $exact , @sorted ) ] };
363
+ return {
364
+ took => $sugg_res -> {took } + $res -> {took } + $fav_res -> {took },
365
+ suggestions => \@sorted ,
366
+ };
367
+ }
368
+
369
+ sub autocomplete {
370
+ my ( $self , @terms ) = @_ ;
371
+ my $data = $self -> _autocomplete( join ' ' , @terms );
372
+
373
+ return {
374
+ took => $data -> {took },
375
+ hits => {
376
+ hits => [
377
+ map {
378
+ my $source = $_ ;
379
+ +{
380
+ fields => {
381
+ map +( $_ => $source -> {$_ } ), qw(
382
+ documentation
383
+ release
384
+ author
385
+ distribution
386
+ ) ,
387
+ },
388
+ };
389
+ } @{ $data -> {suggestions } }
390
+ ],
391
+ },
392
+ };
393
+ }
394
+
395
+ sub autocomplete_suggester {
396
+ my ( $self , @terms ) = @_ ;
397
+ my $data = $self -> _autocomplete( join ' ' , @terms );
398
+
399
+ return {
400
+ took => $data -> {took },
401
+ suggestions => [
402
+ map +{
403
+ author => $_ -> {author },
404
+ date => $_ -> {date },
405
+ deprecated => $_ -> {deprecated },
406
+ distribution => $_ -> {distribution },
407
+ name => $_ -> {documentation },
408
+ release => $_ -> {release },
409
+ },
410
+ @{ $data -> {suggestions } }
411
+ ],
412
+ };
397
413
}
398
414
399
415
sub find_changes_files {
0 commit comments