Skip to content

Commit

Permalink
author by id: move ES query to new API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed Nov 20, 2016
1 parent a16cdef commit 24e13e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -97,7 +97,14 @@ sub search : Path('search') : Args(0) {
};
};

$self->es_by_filter( $c, $filter, $cb );
$self->es_by_filter( c => $c, filter => $filter, cb => $cb );
}

# endpoint: /author/by_id?id=<csv_author_ids>[&fields=<csv_fields>][&sort=<csv_sort>][&size=N]
sub by_id : Path('by_id') : Args(0) {
my ( $self, $c ) = @_;
my @ids = map {uc} split /,/ => $c->req->parameters->{id};
$self->es_by_key_vals( c => $c, key => 'pauseid', vals => \@ids );
}

# endpoint: /author/by_user?user=<csv_user_ids>[&fields=<csv_fields>][&sort=<csv_sort>][&size=N]
Expand Down
16 changes: 10 additions & 6 deletions lib/MetaCPAN/Server/Role/ES/Query.pm
Expand Up @@ -9,26 +9,30 @@ use Ref::Util qw( is_arrayref is_coderef );

# queries by given key and values
sub es_by_key_vals {
my ( $self, $c, $key, $vals, $cb ) = @_;
my ( $self, %args ) = @_;
my ( $c, $cb, $key, $vals ) = @args{qw< c cb key vals >};
my @vals = is_arrayref $vals ? @{$vals} : $vals;
my $filter
= +{
bool => { should => [ map +{ term => { $key => $_ } }, @vals ] }
};
$self->es_query_by_filter( $c, $filter, $cb );
$self->es_by_filter( c => $c, cb => $cb, filter => $filter );
}

# queries by given filter
sub es_by_filter {
my ( $self, $c, $filter, $cb ) = @_;
my $res = $self->model($c)->raw->filter($filter);
return $self->es_query_res( $c, $res, $cb );
my ( $self, %args ) = @_;
my ( $c, $cb, $filter, $_model ) = @args{qw< c cb filter model >};
my $model = $_model ? $c->model($_model) : $self->model($c);
my $res = $model->raw->filter($filter);
return $self->es_query_res( c => $c, cb => $cb, res => $res );
}

# applies generic 'size', 'sort' & 'fields' to
# query result
sub es_query_res {
my ( $self, $c, $res, $cb ) = @_;
my ( $self, %args ) = @_;
my ( $c, $cb, $res ) = @args{qw< c cb res >};
my $params = $c->req->parameters;

my $size = $params->{size} || 5000;
Expand Down

0 comments on commit 24e13e6

Please sign in to comment.