Skip to content

Commit

Permalink
Merge pull request #687 from metacpan/mickey/author_search_endpoint
Browse files Browse the repository at this point in the history
Added /author/search?q=QUERY API endpoint
  • Loading branch information
oalders committed Jun 14, 2017
2 parents b22b404 + f977841 commit 544c64b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/MetaCPAN/Document/Author.pm
Expand Up @@ -181,6 +181,53 @@ sub by_user {
return { authors => \@authors };
}

sub search {
my ( $self, $query, $from ) = @_;

my $body = {
query => {
bool => {
should => [
{
match => {
'name.analyzed' =>
{ query => $query, operator => 'and' }
}
},
{
match => {
'asciiname.analyzed' =>
{ query => $query, operator => 'and' }
}
},
{ match => { 'pauseid' => uc($query) } },
{ match => { 'profile.id' => lc($query) } },
]
}
},
size => 10,
from => $from || 0,
};

my $ret = $self->es->search(
index => $self->index->name,
type => 'author',
body => $body,
);
return {} unless $ret->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
+{ %{ $_->{_source} }, id => $_->{_id} }
} @{ $ret->{hits}{hits} };

return +{
authors => \@authors,
took => $ret->{took},
total => $ret->{hits}{total},
};
}

__PACKAGE__->meta->make_immutable;
1;

Expand Down
9 changes: 9 additions & 0 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -61,6 +61,15 @@ sub get : Path('') : Args(1) {
['The requested field(s) could not be found'] );
}

# /author/search?q=QUERY
sub qsearch : Path('search') : Args(0) {
my ( $self, $c ) = @_;
my ( $query, $from ) = @{ $c->req->params }{qw( q from )};
my $data = $self->model($c)->raw->search( $query, $from );
$data or return;
$c->stash($data);
}

# /author/by_user/USER_ID
sub by_user : Path('by_user') : Args(1) {
my ( $self, $c, $user ) = @_;
Expand Down

0 comments on commit 544c64b

Please sign in to comment.