Skip to content

Commit ff7fdb2

Browse files
committed
minor refactor to make 1st run stand out more + comments
1 parent f7298d5 commit ff7fdb2

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

lib/MetaCPAN/Model/Search.pm

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,39 @@ sub _search_collapsed {
160160
fields => [qw(distribution)],
161161
};
162162

163-
# On the first request also fetch the number of total distributions
164-
# that match the query so that can be reported to the user. There is
165-
# no need to do it on each iteration though, once is enough.
166-
$es_query_opts->{aggregations}
167-
= {
168-
count => { terms => { size => 999, field => 'distribution' } }
169-
}
170-
if $run == 1;
171-
my $es_query = $self->build_query( $search_term, $es_query_opts );
163+
if ( $run == 1 ) {
164+
165+
# On the first request also fetch the number of total distributions
166+
# that match the query so that can be reported to the user. There is
167+
# no need to do it on each iteration though, once is enough.
168+
169+
$es_query_opts->{aggregations}
170+
= {
171+
count => { terms => { size => 999, field => 'distribution' } }
172+
};
173+
}
172174

175+
my $es_query = $self->build_query( $search_term, $es_query_opts );
173176
$es_results = $self->run_query( file => $es_query );
174177
$took += $es_results->{took} || 0;
175-
$total = @{ $es_results->{aggregations}->{count}->{buckets} || [] }
176-
if $run == 1;
178+
179+
if ( $run == 1 ) {
180+
$total
181+
= @{ $es_results->{aggregations}->{count}->{buckets} || [] };
182+
}
183+
177184
$hits = @{ $es_results->{hits}->{hits} || [] };
185+
186+
# Flatten results down to unique dists
178187
@distributions = uniq(
179188
@distributions,
180189
map {
181190
single_valued_arrayref_to_scalar( $_->{fields} );
182191
$_->{fields}->{distribution}
183192
} @{ $es_results->{hits}->{hits} }
184193
);
194+
195+
# Keep track
185196
$run++;
186197
} while ( @distributions < $page_size + $from
187198
&& $es_results->{hits}->{total}
@@ -199,8 +210,7 @@ sub _search_collapsed {
199210

200211
# Now that we know which distributions are going to be displayed on the
201212
# results page, fetch the details about those distributions
202-
my $favorites = $self->search_favorites(@distributions);
203-
my $es_query = $self->build_query(
213+
my $es_query = $self->build_query(
204214
$search_term,
205215
{
206216
# we will probably never hit that limit, since we are searching in $page_size=20 distributions max
@@ -224,7 +234,9 @@ sub _search_collapsed {
224234
);
225235
my $results = $self->run_query( file => $es_query );
226236

237+
my $favorites = $self->search_favorites(@distributions);
227238
$took += sum( grep {defined} $results->{took}, $favorites->{took} );
239+
228240
$results = $self->_extract_results_add_favs( $results, $favorites );
229241
$results = $self->_collapse_results($results);
230242
my @ids = map { $_->[0]{id} } @$results;
@@ -234,10 +246,12 @@ sub _search_collapsed {
234246
took => $took,
235247
collapsed => \1,
236248
};
249+
237250
my $descriptions = $self->search_descriptions(@ids);
238251
$data->{took} += $descriptions->{took} || 0;
239252
map { $_->[0]{description} = $descriptions->{results}{ $_->[0]{id} } }
240253
@{ $data->{results} };
254+
241255
return $data;
242256
}
243257

0 commit comments

Comments
 (0)