Skip to content

Commit

Permalink
/search/autocomplete: Collapse single-valued arrays in fields
Browse files Browse the repository at this point in the history
To be consistent with API v0 and other v1 endpoints.  No need to leak
more crazy from Elasticsearch than necessary.
  • Loading branch information
tsibley committed Nov 20, 2016
1 parent 1321275 commit b94affe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/MetaCPAN/Server/Controller/Search/Autocomplete.pm
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand All @@ -16,8 +17,12 @@ sub get : Local : Path('') : Args(0) {
my $model = $self->model($c);
$model = $model->fields( [qw(documentation release author distribution)] )
unless $model->fields;
my $data = $model->autocomplete( $c->req->param("q") )->raw;
$c->stash( $data->all );
my $data = $model->autocomplete( $c->req->param("q") )->raw->all;

single_valued_arrayref_to_scalar( $_->{fields} )
for @{ $data->{hits}{hits} };

$c->stash($data);
}

1;
2 changes: 1 addition & 1 deletion t/server/controller/search/autocomplete.t
Expand Up @@ -14,7 +14,7 @@ test_psgi app, sub {
'GET' );
my $json = decode_json_ok($res);

my $got = [ map { @{ $_->{fields}{documentation} } }
my $got = [ map { $_->{fields}{documentation} }
@{ $json->{hits}{hits} } ];

is_deeply $got, [
Expand Down

0 comments on commit b94affe

Please sign in to comment.