@@ -4,9 +4,28 @@ use Moose;
4
4
5
5
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar ) ;
6
6
use Ref::Util qw( is_hashref ) ;
7
+ use List::Util qw( max ) ;
8
+
9
+ use MetaCPAN::Query::Favorite;
7
10
8
11
extends ' ElasticSearchX::Model::Document::Set' ;
9
12
13
+ has query_favorite => (
14
+ is => ' ro' ,
15
+ isa => ' MetaCPAN::Query::Favorite' ,
16
+ lazy => 1,
17
+ builder => ' _build_query_favorite' ,
18
+ handles => [qw< agg_by_distributions > ],
19
+ );
20
+
21
+ sub _build_query_favorite {
22
+ my $self = shift ;
23
+ return MetaCPAN::Query::Favorite-> new(
24
+ es => $self -> es,
25
+ index_name => $self -> index -> name,
26
+ );
27
+ }
28
+
10
29
my @ROGUE_DISTRIBUTIONS = qw(
11
30
Bundle-Everything
12
31
kurila
@@ -479,10 +498,11 @@ sub autocomplete {
479
498
# mapping + data is fully deployed.
480
499
# -- Mickey
481
500
sub autocomplete_using_suggester {
482
- my ( $self , @terms ) = @_ ;
483
- my $query = join ( q{ } , @terms );
501
+ my ( $self , $query ) = @_ ;
484
502
return $self unless $query ;
485
503
504
+ my $search_size = 50;
505
+
486
506
my $suggestions
487
507
= $self -> search_type(' dfs_query_then_fetch' )-> es-> suggest(
488
508
{
@@ -492,7 +512,7 @@ sub autocomplete_using_suggester {
492
512
text => $query ,
493
513
completion => {
494
514
field => " suggest" ,
495
- size => 50 ,
515
+ size => $search_size ,
496
516
}
497
517
}
498
518
},
@@ -502,8 +522,8 @@ sub autocomplete_using_suggester {
502
522
my %docs ;
503
523
504
524
for my $suggest ( @{ $suggestions -> {documentation }[0]{options } } ) {
505
- next if exists $docs { $suggest -> {text } };
506
- $docs { $suggest -> {text } } = $suggest -> {score };
525
+ $docs { $suggest -> {text } } = max grep { defined }
526
+ ( $docs { $suggest -> {text } }, $suggest -> {score } ) ;
507
527
}
508
528
509
529
my $data = $self -> es-> search(
@@ -512,21 +532,6 @@ sub autocomplete_using_suggester {
512
532
type => ' file' ,
513
533
body => {
514
534
query => {
515
- filtered => {
516
- query => {
517
- function_score => {
518
- script_score => {
519
- script => {
520
- lang => ' groovy' ,
521
- file =>
522
- ' prefer_shorter_module_names_400' ,
523
- },
524
- },
525
- },
526
- },
527
- },
528
- },
529
- filter => {
530
535
bool => {
531
536
must => [
532
537
{ term => { indexed => 1 } },
@@ -536,21 +541,38 @@ sub autocomplete_using_suggester {
536
541
terms => { ' documentation' => [ keys %docs ] }
537
542
},
538
543
],
544
+ must_not => [
545
+ {
546
+ terms =>
547
+ { distribution => \@ROGUE_DISTRIBUTIONS }
548
+ },
549
+ ],
539
550
}
540
551
},
541
552
},
542
- fields => [' documentation' ],
543
- size => 10 ,
553
+ fields => [ ' documentation' , ' distribution ' ],
554
+ size => $search_size ,
544
555
}
545
556
);
546
557
547
- return +{
548
- suggestions => [
549
- sort { length ($a ) <=> length ($b ) || $a cmp $b }
550
- map { $_ -> {fields }{documentation }[0] }
551
- @{ $data -> {hits }{hits } }
552
- ]
553
- };
558
+ my %valid = map {
559
+ ( $_ -> {fields }{documentation }[0] => $_ -> {fields }{distribution }[0] )
560
+ } @{ $data -> {hits }{hits } };
561
+
562
+ my $exact = delete $valid {$query };
563
+
564
+ my $favorites
565
+ = $self -> agg_by_distributions( [ values %valid ] )-> {favorites };
566
+
567
+ my @sorted = sort {
568
+ $favorites -> { $valid {$b } } <=> $favorites -> { $valid {$a } }
569
+ || $docs {$b } <=> $docs {$a }
570
+ || length ($a ) <=> length ($b )
571
+ || $a cmp $b
572
+ }
573
+ keys %valid ;
574
+
575
+ return +{ suggestions => [ grep {defined } ( $exact , @sorted ) ] };
554
576
}
555
577
556
578
sub dir {
0 commit comments