@@ -2,39 +2,11 @@ package MetaCPAN::Document::File::Set;
22
33use Moose;
44
5- use List::Util qw( max ) ;
6- use MetaCPAN::ESConfig qw( es_doc_path ) ;
7- use MetaCPAN::Query::Favorite ();
8- use MetaCPAN::Query::File ();
9- use MetaCPAN::Query::Release ();
10- use MetaCPAN::Util qw( true false ) ;
5+ use MetaCPAN::ESConfig qw( es_doc_path ) ;
6+ use MetaCPAN::Util qw( true false ) ;
117
128extends ' ElasticSearchX::Model::Document::Set' ;
139
14- has query_favorite => (
15- is => ' ro' ,
16- isa => ' MetaCPAN::Query::Favorite' ,
17- lazy => 1,
18- builder => ' _build_query_favorite' ,
19- handles => [qw< agg_by_distributions > ],
20- );
21-
22- sub _build_query_favorite {
23- my $self = shift ;
24- return MetaCPAN::Query::Favorite-> new( es => $self -> es );
25- }
26-
27- my @ROGUE_DISTRIBUTIONS = qw(
28- Acme-DependOnEverything
29- Bundle-Everything
30- kurila
31- perl-5.005_02+apache1.3.3+modperl
32- perlbench
33- perl_debug
34- pod2texi
35- spodcxx
36- ) ;
37-
3810sub find {
3911 my ( $self , $module ) = @_ ;
4012
@@ -241,146 +213,5 @@ sub history {
241213 return $search -> sort ( [ { date => ' desc' } ] );
242214}
243215
244- sub _autocomplete {
245- my ( $self , $query ) = @_ ;
246-
247- my $search_size = 100;
248-
249- my $sugg_res = $self -> es-> search(
250- es_doc_path(' file' ),
251- body => {
252- suggest => {
253- documentation => {
254- text => $query ,
255- completion => {
256- field => " suggest" ,
257- size => $search_size ,
258- },
259- },
260- }
261- },
262- );
263-
264- my %docs ;
265- for my $suggest ( @{ $sugg_res -> {suggest }{documentation }[0]{options } } ) {
266- $docs { $suggest -> {text } } = max grep {defined }
267- ( $docs { $suggest -> {text } }, $suggest -> {score } );
268- }
269-
270- my $res = $self -> es-> search(
271- es_doc_path(' file' ),
272- body => {
273- query => {
274- bool => {
275- must => [
276- { term => { indexed => true } },
277- { term => { authorized => true } },
278- { term => { status => ' latest' } },
279- { terms => { documentation => [ keys %docs ] } },
280- ],
281- must_not => [
282- {
283- terms => { distribution => \@ROGUE_DISTRIBUTIONS }
284- },
285- ],
286- }
287- },
288- _source => [ qw(
289- author
290- date
291- deprecated
292- distribution
293- documentation
294- release
295- ) ],
296- size => $search_size ,
297- },
298- );
299-
300- my $hits = $res -> {hits }{hits };
301-
302- my $fav_res
303- = $self -> agg_by_distributions(
304- [ map $_ -> {_source }{distribution }, @$hits ] );
305-
306- my $favs = $fav_res -> {favorites };
307-
308- my %valid = map {
309- my $source = $_ -> {_source };
310- (
311- $source -> {documentation } => {
312- %$source , favorites => $favs -> { $source -> {distribution } },
313- }
314- );
315- } @{ $res -> {hits }{hits } };
316-
317- # remove any exact match, it will be added later
318- my $exact = delete $valid {$query };
319-
320- no warnings ' uninitialized' ;
321- my @sorted = map { $valid {$_ } }
322- sort {
323- my $a_data = $valid {$a };
324- my $b_data = $valid {$b };
325- $a_data -> {deprecated } <=> $b_data -> {deprecated }
326- || $b_data -> {favorites } <=> $a_data -> {favorites }
327- || $docs {$b } <=> $docs {$a }
328- || length ($a ) <=> length ($b )
329- || $a cmp $b
330- }
331- keys %valid ;
332-
333- return {
334- took => $sugg_res -> {took } + $res -> {took } + $fav_res -> {took },
335- suggestions => \@sorted ,
336- };
337- }
338-
339- sub autocomplete {
340- my ( $self , @terms ) = @_ ;
341- my $data = $self -> _autocomplete( join ' ' , @terms );
342-
343- return {
344- took => $data -> {took },
345- hits => {
346- hits => [
347- map {
348- my $source = $_ ;
349- +{
350- fields => {
351- map +( $_ => $source -> {$_ } ), qw(
352- documentation
353- release
354- author
355- distribution
356- ) ,
357- },
358- };
359- } @{ $data -> {suggestions } }
360- ],
361- },
362- };
363- }
364-
365- sub autocomplete_suggester {
366- my ( $self , @terms ) = @_ ;
367- my $data = $self -> _autocomplete( join ' ' , @terms );
368-
369- return {
370- took => $data -> {took },
371- suggestions => [
372- map +{
373- author => $_ -> {author },
374- date => $_ -> {date },
375- deprecated => $_ -> {deprecated },
376- distribution => $_ -> {distribution },
377- name => $_ -> {documentation },
378- release => $_ -> {release },
379- },
380- @{ $data -> {suggestions } }
381- ],
382- };
383- }
384-
385216__PACKAGE__ -> meta-> make_immutable;
3862171;
0 commit comments