Skip to content

Commit

Permalink
split out desc/fav adding
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed May 13, 2017
1 parent a9d14ca commit 39f09b1
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/MetaCPAN/Model/Search.pm
Expand Up @@ -112,8 +112,7 @@ sub _search_expanded {
return {} unless @distributions;

my $results = $self->_extract_results($es_results);

$results = $self->_inflate_results($results);
$results = $self->_add_descriptions_and_faves_to_flat_results($results);

return {

Expand Down Expand Up @@ -214,7 +213,8 @@ sub _search_collapsed {

my $results = $self->_extract_results($es_dist_results);
$results = $self->_collapse_results($results);
$results = $self->_inflate_results($results);
$results
= $self->_add_descriptions_and_faves_to_collapsed_results($results);

return {
results => $results,
Expand Down Expand Up @@ -417,14 +417,14 @@ sub _extract_results {
+{
%{ $res->{fields} },
%{ $res->{_source} },
abstract => $res->{fields}->{'abstract.analyzed'},
abstract => delete $res->{fields}->{'abstract.analyzed'},
score => $res->{_score},
}
} @{ $results->{hits}{hits} }
];
}

sub _inflate_results {
sub _add_descriptions_and_faves_to_flat_results {
my ( $self, $results ) = @_;

my @distributions = uniq map { $_->{distribution} } @{$results};
Expand All @@ -441,6 +441,24 @@ sub _inflate_results {
return $results;
}

# we only add the description and favs to the top level dist
sub _add_descriptions_and_faves_to_collapsed_results {
my ( $self, $results ) = @_;

my @distributions = uniq map { $_->[0]->{distribution} } @{$results};
my $favorites = $self->search_favorites( \@distributions );

my @ids = uniq map { $_->[0]->{id} } @{$results};
my $descriptions = $self->search_descriptions( \@ids );

map {
$_->[0]->{description} = $descriptions->{results}->{ $_->{id} };
$_->[0]->{favorites} = $favorites->{ $_->{distribution} };
} @{$results};

return $results;
}

1;

## THIS IS THE QUERY and we fill in the gaps
Expand Down

0 comments on commit 39f09b1

Please sign in to comment.