Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Autosearch for author, by name.
  • Loading branch information
Talina06 committed Jul 6, 2014
1 parent acfa27a commit 492f636
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/MetaCPAN/Document/Author.pm
Expand Up @@ -157,6 +157,24 @@ sub validate {
return @result;
}

sub authorsearch {
my ( $self, @terms ) = @_;
my $query = join( " ", @terms );
return $self unless $query;
return $self->query(
{
custom_score => {
query => { bool => { should => $query } },
script => "_score - doc['name'].value.length()/100",
}
}
)->filter(
{
and => [ $self->_not_rogue, { exists => { field => 'name' } } ]
}
)->sort( [ '_score', 'name' ] );
}

__PACKAGE__->meta->make_immutable;

1;
Expand Down
23 changes: 23 additions & 0 deletions lib/MetaCPAN/Server/Controller/Search/AuthorSearch.pm
@@ -0,0 +1,23 @@
package MetaCPAN::Server::Controller::Search::AuthorSearch;

use strict;
use warnings;

use Moose;

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

with 'MetaCPAN::Server::Role::JSONP';

has '+type' => ( default => 'author' );

sub get : Local : Path('') : Args(0) {
my ( $self, $c ) = @_;
my $model = $self->model($c);
$model = $model->fields( [qw(name pauseid gravatar_url)] )
unless $model->fields;
my $data = $model->authorsearch( $c->req->param("q") )->raw;
$c->stash( $data->all );
}

1;

0 comments on commit 492f636

Please sign in to comment.