@@ -2,8 +2,6 @@ package MetaCPAN::Model::Search;
22
33use Moose;
44
5- use v5.10;
6-
75use Log::Contextual qw( :log :dlog ) ;
86use MooseX::StrictConstructor;
97
@@ -53,13 +51,34 @@ sub search_for_first_result {
5351 return $data -> {fields };
5452}
5553
54+ =head2 search_web
55+
56+ search_web( $search_term, $from, $page_size, $collapsed );
57+
58+ - search_term:
59+ - can be unqualified string e.g. 'paging'
60+ - can be author e.g: 'author:LLAP'
61+ - can be module e.g.: 'module:Data::Pageset'
62+ - can be distribution e.g.: 'dist:Data-Pageset'
63+
64+ - from: where in result set to start, int
65+
66+ - page_size: number of results per page, int
67+
68+ - collapsed: whether to merge results by dist or not
69+
70+ =cut
71+
5672sub search_web {
5773 my ( $self , $search_term , $from , $page_size , $collapsed ) = @_ ;
5874 $page_size //= 20;
5975 $from //= 0;
6076
61- # munge the query
77+ # munge the search_term
6278 # these would be nicer if we had variable-length lookbehinds...
79+ # Allow q = 'author:LLAP' or 'module:Data::Page' or 'dist:'
80+ # We are mapping to correct ES fields here - wonder if ANYONE
81+ # uses these?!?!?!
6382 $search_term #
6483 =~ s { (^|\s )author:([a-zA-Z]+)(?=\s |$)} { $1author:\U $2 \E } g ;
6584 $search_term
@@ -78,8 +97,9 @@ sub search_web {
7897sub _search_expanded {
7998 my ( $self , $search_term , $from , $page_size ) = @_ ;
8099
81- # When used for a distribution or module search, the limit is included in
82- # thl query and ES does the right thing.
100+ # Used for distribution and module searches, the limit is included in
101+ # the query and ES does the right thing (because we are not collapsing
102+ # results by distribution).
83103 my $es_query = $self -> build_query(
84104 $search_term ,
85105 {
@@ -89,27 +109,32 @@ sub _search_expanded {
89109 );
90110
91111 # return $es_query;
92- my $data = $self -> run_query( file => $es_query );
112+ my $es_results = $self -> run_query( file => $es_query );
93113
94114 my @distributions = uniq
95115 map {
96116 single_valued_arrayref_to_scalar( $_ -> {fields } );
97117 $_ -> {fields }-> {distribution }
98- } @{ $data -> {hits }-> {hits } };
118+ } @{ $es_results -> {hits }-> {hits } };
99119
100120 # Everything after this will fail (slowly and silently) without results.
101121 return {} unless @distributions ;
102122
103- my @ids = map { $_ -> {fields }-> {id } } @{ $data -> {hits }-> {hits } };
123+ # Lookup favs and extract results from es (adding in favs)
124+ my $favorites = $self -> search_favorites(@distributions );
125+ my $results = $self -> _extract_results_add_favs( $es_results , $favorites );
126+
127+ # Add descriptions
128+ my @ids = map { $_ -> {id } } @{$results };
104129 my $descriptions = $self -> search_descriptions(@ids );
105- my $favorites = $self -> search_favorites(@distributions );
106- my $results = $self -> _extract_results( $data , $favorites );
130+
107131 map { $_ -> {description } = $descriptions -> {results }-> { $_ -> {id } } }
108132 @{$results };
133+
109134 my $return = {
110135 results => [ map { [$_ ] } @$results ],
111- total => $data -> {hits }-> {total },
112- took => sum( grep {defined } $data -> {took }, $favorites -> {took } ),
136+ total => $es_results -> {hits }-> {total },
137+ took => sum( grep {defined } $es_results -> {took }, $favorites -> {took } ),
113138 collapsed => \0,
114139 };
115140 return $return ;
@@ -198,7 +223,7 @@ sub _search_collapsed {
198223 my $results = $self -> run_query( file => $es_query );
199224
200225 $took += sum( grep {defined } $results -> {took }, $favorites -> {took } );
201- $results = $self -> _extract_results ( $results , $favorites );
226+ $results = $self -> _extract_results_add_favs ( $results , $favorites );
202227 $results = $self -> _collapse_results($results );
203228 my @ids = map { $_ -> [0]{id } } @$results ;
204229 $data = {
@@ -473,20 +498,19 @@ sub search_favorites {
473498 return $results ;
474499}
475500
476- sub _extract_results {
501+ sub _extract_results_add_favs {
477502 my ( $self , $results , $favorites ) = @_ ;
503+
478504 return [
479505 map {
480506 my $res = $_ ;
481507 single_valued_arrayref_to_scalar( $res -> {fields } );
482- my $dist = $res -> {fields }{distribution };
483508 +{
484509 %{ $res -> {fields } },
485510 %{ $res -> {_source } },
486- abstract => $res -> {fields }{' abstract.analyzed' },
487- score => $res -> {_score },
488- favorites => $favorites -> {favorites }{$dist },
489- myfavorite => $favorites -> {myfavorites }{$dist },
511+ abstract => delete $res -> {fields }-> {' abstract.analyzed' },
512+ score => $res -> {_score },
513+ favorites => $favorites -> { $res -> {fields }-> {distribution } },
490514 }
491515 } @{ $results -> {hits }{hits } }
492516 ];
0 commit comments