Skip to content

Commit 58e6348

Browse files
authored
Merge pull request metacpan#768 from metacpan/jberger/new_suggest_format
return structured data from suggest endpoint
2 parents fd72f61 + c29eb94 commit 58e6348

File tree

1 file changed

+15
-8
lines changed
  • lib/MetaCPAN/Document/File

1 file changed

+15
-8
lines changed

lib/MetaCPAN/Document/File/Set.pm

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ sub autocomplete_suggester {
523523
( $docs{ $suggest->{text} }, $suggest->{score} );
524524
}
525525

526-
my $data = $self->es->search(
526+
my @fields = (qw(documentation distribution author release));
527+
my $data = $self->es->search(
527528
{
528529
index => $self->index->name,
529530
type => 'file',
@@ -547,25 +548,31 @@ sub autocomplete_suggester {
547548
}
548549
},
549550
},
550-
fields => [ 'documentation', 'distribution' ],
551+
fields => \@fields,
551552
size => $search_size,
552553
}
553554
);
554555

555556
my %valid = map {
556-
( $_->{fields}{documentation}[0] => $_->{fields}{distribution}[0] )
557+
my $got = $_->{fields};
558+
my %record;
559+
@record{@fields} = map { $got->{$_}[0] } @fields;
560+
$record{name} = delete $record{documentation}; # rename
561+
( $_->{fields}{documentation}[0] => \%record );
557562
} @{ $data->{hits}{hits} };
558563

559564
# remove any exact match, it will be added later
560-
my $exact;
561-
$exact = $query if defined delete $valid{$query};
565+
my $exact = delete $valid{$query};
562566

563567
my $favorites
564-
= $self->agg_by_distributions( [ values %valid ] )->{favorites};
568+
= $self->agg_by_distributions(
569+
[ map { $_->{distribution} } values %valid ] )->{favorites};
565570

566571
no warnings 'uninitialized';
567-
my @sorted = sort {
568-
$favorites->{ $valid{$b} } <=> $favorites->{ $valid{$a} }
572+
my @sorted = map { $valid{$_} }
573+
sort {
574+
$favorites->{ $valid{$b}->{name} }
575+
<=> $favorites->{ $valid{$a}->{name} }
569576
|| $docs{$b} <=> $docs{$a}
570577
|| length($a) <=> length($b)
571578
|| $a cmp $b

0 commit comments

Comments
 (0)