Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 23 additions & 53 deletions lib/Padre/Task/CPAN.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,54 +88,22 @@ sub metacpan_autocomplete {
# Create an array of query keywords that are separated by spaces
my @query = split( /\s+/, $query );

# The documentation Module-Name that should be analyzed
my $should = [
map {
( { field => { 'documentation.analyzed' => "$_*" } },
{ field => { 'documentation.camelcase' => "$_*" } }
)
}
grep {
$_
} @query
];
push @{$should}, map { ( { field => { 'author' => "$_" } }, ) } map { uc $_ } grep {$_} @query;

# The distribution we do not want in our search

my @ROGUE_DISTRIBUTIONS = qw(kurila perl_debug perl-5.005_02+apache1.3.3+modperl pod2texi perlbench spodcxx);

# Create String Search
my $criteria = '('.join ' AND ', @query.' )' ;
my $exclude = 'AND NOT ('.join ' OR ', @ROGUE_DISTRIBUTIONS.' )' ;

# The ElasticSearch query in Perl

my %payload = (
track_scores => 1,
query => {
filtered => {
query => {
bool => { should => $should }

# ToDo see #1488 comment:7 itcharlie++
# custom_score => {
# query => { bool => { should => $should } },
# script => "_score - doc['documentation'].stringValue.length()/100"
# },
},
filter => {
and => [
{ not => {
filter => {
or => [
map {
{ term => { 'file.distribution' => $_ } }
} @ROGUE_DISTRIBUTIONS
]
}
}
},
{ exists => { field => 'documentation' } },
{ term => { 'file.indexed' => \1 } },
{ term => { 'file.status' => 'latest' } },
{ not => { filter => { term => { 'file.authorized' => \0 } } } }
]
}
query => {
query_string => {
query => "$criteria AND (status:latest) AND (indexed:true) $exclude",
fields => ["distribution","documentation.camelcase", "documentation.analyzed","author" ],
analyze_wildcard => 'true',
}
},
sort => [
Expand All @@ -145,6 +113,7 @@ sub metacpan_autocomplete {
documentation => { order => "asc" }
}
],
track_scores => 1,
fields => [qw(documentation release author distribution)],
size => MAX_RESULTS,
);
Expand All @@ -162,7 +131,7 @@ sub metacpan_autocomplete {
$ua->env_proxy unless Padre::Constant::WIN32;

my $response = $ua->post(
'http://api.metacpan.org/v0/file/_search',
'https://fastapi.metacpan.org/v1/file/_search',
Content => $json_request,
);

Expand Down Expand Up @@ -192,7 +161,7 @@ sub metacpan_pod {
my $ua = LWP::UserAgent->new( agent => "Padre/$VERSION" );
$ua->timeout(10);
$ua->env_proxy unless Padre::Constant::WIN32;
my $url = "http://api.metacpan.org/v0/pod/$module?content-type=text/x-pod";
my $url = "https://fastapi.metacpan.org/v1/pod/$module?content-type=text/x-pod";
my $response = $ua->get($url);
unless ( $response->is_success ) {
TRACE( sprintf( "Got '%s for %s", $response->status_line, $url ) )
Expand Down Expand Up @@ -244,7 +213,7 @@ sub metacpan_recent {
$ua->timeout(10);
$ua->env_proxy unless Padre::Constant::WIN32;
my $url =
"http://api.metacpan.org/v0/release/?sort=date:desc"
"https://fastapi.metacpan.org/v1/release/?sort=date:desc"
. "&size="
. MAX_RESULTS
. "&fields=name,distribution,abstract,download_url";
Expand Down Expand Up @@ -272,18 +241,17 @@ sub metacpan_recent {
sub metacpan_favorite {
my $self = shift;

my %payload = (
my %payload = (
"query" => { "match_all" => {} },
"facets" => {
"leaderboard" => {
"aggs" => {
"dist" => {
"terms" => {
"field" => "distribution",
"size" => MAX_RESULTS,
},
},
},
size => 0,

);

# Convert ElasticSearch Perl query to a JSON request
Expand All @@ -296,18 +264,20 @@ sub metacpan_favorite {
$ua->timeout(10);
$ua->env_proxy unless Padre::Constant::WIN32;
my $response = $ua->post(
'http://api.metacpan.org/v0/favorite/_search',
'https://fastapi.metacpan.org/v1/favorite/_search',
Content => $json_request,
);



unless ( $response->is_success ) {
TRACE( sprintf( "Got '%s' from metacpan.org", $response->status_line ) ) if DEBUG;
return [];
}

# Decode json response then cleverly map it for the average joe :)
my $data = JSON::XS::decode_json( $response->decoded_content );
my @results = map {$_} @{ $data->{facets}->{leaderboard}->{terms} || [] };
my @results = map {{ term => $_->{key}, count => $_->{doc_count} }}
@{$data->{aggregations}->{dist}->{buckets} || [] };

return \@results;
}
Expand Down