Skip to content

Commit

Permalink
Added /mirror/search API endpoint
Browse files Browse the repository at this point in the history
This endpoint will replace query sending from WEB
  • Loading branch information
mickeyn committed Jun 28, 2017
1 parent fd47fe0 commit 2435ece
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
77 changes: 77 additions & 0 deletions lib/MetaCPAN/Document/Mirror.pm
Expand Up @@ -42,5 +42,82 @@ has [qw(inceptdate reitredate)] => (
coerce => 1,
);

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Document::Mirror::Set;

use strict;
use warnings;

use Moose;

extends 'ElasticSearchX::Model::Document::Set';

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

my @protocols = grep /^ (?: http | ftp | rsync ) $/x, split /\s+/, $q;

my $query
= @protocols
? {
bool => {
must_not => {
bool => {
should => [
map +{ filter => { missing => { field => $_ } } },
@protocols
]
}
}
}
}
: { match_all => {} };

my @sort = ( sort => [qw( continent country )] );

my $location;

if ( $q =~ /loc\:([^\s]+)/ ) {
$location = [ split( /,/, $1 ) ];
if ($location) {
@sort = (
sort => {
_geo_distance => {
location => [ $location->[1], $location->[0] ],
order => 'asc',
unit => 'km'
}
}
);
}
}

my $ret = $self->es->search(
index => $self->index->name,
type => 'mirror',
body => {
size => 999,
query => $query,
@sort,
},
);
return unless $ret->{hits}{total};

my $data = [
map +{
%{ $_->{_source} },
distance => ( $location ? $_->{sort}[0] : undef )
},
@{ $ret->{hits}{hits} }
];

return {
mirrors => $data,
total => $ret->{hits}{total},
took => $ret->{took}
};
}

__PACKAGE__->meta->make_immutable;
1;
7 changes: 7 additions & 0 deletions lib/MetaCPAN/Server/Controller/Mirror.pm
Expand Up @@ -9,4 +9,11 @@ BEGIN { extends 'MetaCPAN::Server::Controller' }

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

sub search : Path('') : Args(0) {
my ( $self, $c ) = @_;
my $data = $self->model($c)->raw->search( $c->req->param('q') );
return unless $data;
$c->stash($data);
}

1;

0 comments on commit 2435ece

Please sign in to comment.