Skip to content

Commit 2465f5c

Browse files
authored
Merge pull request #764 from metacpan/autocomplete_suggester
Improve autocomplete when using suggester
2 parents e6eca45 + 2b6b5c9 commit 2465f5c

File tree

1 file changed

+51
-29
lines changed
  • lib/MetaCPAN/Document/File

1 file changed

+51
-29
lines changed

lib/MetaCPAN/Document/File/Set.pm

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@ use Moose;
44

55
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );
66
use Ref::Util qw( is_hashref );
7+
use List::Util qw( max );
8+
9+
use MetaCPAN::Query::Favorite;
710

811
extends 'ElasticSearchX::Model::Document::Set';
912

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+
1029
my @ROGUE_DISTRIBUTIONS = qw(
1130
Bundle-Everything
1231
kurila
@@ -479,10 +498,11 @@ sub autocomplete {
479498
# mapping + data is fully deployed.
480499
# -- Mickey
481500
sub autocomplete_using_suggester {
482-
my ( $self, @terms ) = @_;
483-
my $query = join( q{ }, @terms );
501+
my ( $self, $query ) = @_;
484502
return $self unless $query;
485503

504+
my $search_size = 50;
505+
486506
my $suggestions
487507
= $self->search_type('dfs_query_then_fetch')->es->suggest(
488508
{
@@ -492,7 +512,7 @@ sub autocomplete_using_suggester {
492512
text => $query,
493513
completion => {
494514
field => "suggest",
495-
size => 50,
515+
size => $search_size,
496516
}
497517
}
498518
},
@@ -502,8 +522,8 @@ sub autocomplete_using_suggester {
502522
my %docs;
503523

504524
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} );
507527
}
508528

509529
my $data = $self->es->search(
@@ -512,21 +532,6 @@ sub autocomplete_using_suggester {
512532
type => 'file',
513533
body => {
514534
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 => {
530535
bool => {
531536
must => [
532537
{ term => { indexed => 1 } },
@@ -536,21 +541,38 @@ sub autocomplete_using_suggester {
536541
terms => { 'documentation' => [ keys %docs ] }
537542
},
538543
],
544+
must_not => [
545+
{
546+
terms =>
547+
{ distribution => \@ROGUE_DISTRIBUTIONS }
548+
},
549+
],
539550
}
540551
},
541552
},
542-
fields => ['documentation'],
543-
size => 10,
553+
fields => [ 'documentation', 'distribution' ],
554+
size => $search_size,
544555
}
545556
);
546557

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 ) ] };
554576
}
555577

556578
sub dir {

0 commit comments

Comments
 (0)