From 0145221bf2702fcbe0137948a2073d2af53ab055 Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Tue, 23 May 2017 12:40:31 +0100 Subject: [PATCH 1/2] convert deprecated 'filtered' with recommended 'bool' queries --- lib/MetaCPAN/Web/Model/API/Favorite.pm | 17 +- lib/MetaCPAN/Web/Model/API/File.pm | 27 +-- lib/MetaCPAN/Web/Model/API/Lab.pm | 9 +- lib/MetaCPAN/Web/Model/API/Module.pm | 21 +- lib/MetaCPAN/Web/Model/API/Rating.pm | 10 +- lib/MetaCPAN/Web/Model/API/Release.pm | 293 +++++++++++-------------- 6 files changed, 161 insertions(+), 216 deletions(-) diff --git a/lib/MetaCPAN/Web/Model/API/Favorite.pm b/lib/MetaCPAN/Web/Model/API/Favorite.pm index 122ad19047..e51aa20fec 100644 --- a/lib/MetaCPAN/Web/Model/API/Favorite.pm +++ b/lib/MetaCPAN/Web/Model/API/Favorite.pm @@ -26,15 +26,7 @@ sub get { { size => 0, query => { - filtered => { - query => { match_all => {} }, - filter => { - or => [ - map { { term => { 'distribution' => $_ } } } - @distributions - ] - } - } + terms => { 'distribution' => \@distributions } }, aggregations => { favorites => { @@ -177,12 +169,7 @@ sub by_dist { return $self->request( '/favorite/_search', { - query => { - filtered => { - query => { match_all => {} }, - filter => { term => { distribution => $distribution }, }, - } - }, + query => { term => { distribution => $distribution } }, _source => "user", size => 1000, } diff --git a/lib/MetaCPAN/Web/Model/API/File.pm b/lib/MetaCPAN/Web/Model/API/File.pm index de3c3a6c79..f3a9d435f0 100644 --- a/lib/MetaCPAN/Web/Model/API/File.pm +++ b/lib/MetaCPAN/Web/Model/API/File.pm @@ -18,21 +18,18 @@ sub dir { '/file/_search', { query => { - filtered => { - query => { match_all => {}, }, - filter => { - and => [ - { term => { 'level' => scalar @path } }, - { term => { 'author' => $author } }, - { term => { 'release' => $release } }, - { - prefix => { - 'path' => join( q{/}, @path, q{} ) - } - }, - ] - }, - } + bool => { + must => [ + { term => { 'level' => scalar @path } }, + { term => { 'author' => $author } }, + { term => { 'release' => $release } }, + { + prefix => { + 'path' => join( q{/}, @path, q{} ) + } + }, + ] + }, }, size => 999, fields => [ diff --git a/lib/MetaCPAN/Web/Model/API/Lab.pm b/lib/MetaCPAN/Web/Model/API/Lab.pm index f7e4d9564a..3634030206 100644 --- a/lib/MetaCPAN/Web/Model/API/Lab.pm +++ b/lib/MetaCPAN/Web/Model/API/Lab.pm @@ -87,12 +87,9 @@ sub fetch_latest_distros { '/release/_search', { query => { - filtered => { - query => { match_all => {} }, - filter => { - and => \@filter, - }, - }, + bool => { + must => \@filter, + } }, sort => [ 'distribution', { 'version_numified' => { reverse => 1 } } diff --git a/lib/MetaCPAN/Web/Model/API/Module.pm b/lib/MetaCPAN/Web/Model/API/Module.pm index d1f49db080..a17a837149 100644 --- a/lib/MetaCPAN/Web/Model/API/Module.pm +++ b/lib/MetaCPAN/Web/Model/API/Module.pm @@ -85,19 +85,16 @@ sub requires { '/release/_search', { query => { - filtered => { - query => { 'match_all' => {} }, - filter => { - and => [ - { term => { 'status' => 'latest' } }, - { term => { 'authorized' => 1 } }, - { - term => { - 'dependency.module' => $module - } + bool => { + must => [ + { term => { 'status' => 'latest' } }, + { term => { 'authorized' => 1 } }, + { + term => { + 'dependency.module' => $module } - ] - } + } + ] } }, size => $page_size, diff --git a/lib/MetaCPAN/Web/Model/API/Rating.pm b/lib/MetaCPAN/Web/Model/API/Rating.pm index 2889aea36f..84acbea2fa 100644 --- a/lib/MetaCPAN/Web/Model/API/Rating.pm +++ b/lib/MetaCPAN/Web/Model/API/Rating.pm @@ -42,15 +42,7 @@ sub get { { size => 0, query => { - filtered => { - query => { match_all => {} }, - filter => { - or => [ - map { { term => { 'distribution' => $_ } } } - @distributions - ] - } - } + terms => { distribution => \@distributions } }, aggregations => { ratings => { diff --git a/lib/MetaCPAN/Web/Model/API/Release.pm b/lib/MetaCPAN/Web/Model/API/Release.pm index 54ee71ad6a..adef3d69e1 100644 --- a/lib/MetaCPAN/Web/Model/API/Release.pm +++ b/lib/MetaCPAN/Web/Model/API/Release.pm @@ -32,14 +32,11 @@ sub get { '/release/_search', { query => { - filtered => { - query => { match_all => {} }, - filter => { - and => [ - { term => { 'name' => $release } }, - { term => { author => uc($author) } } - ] - } + bool => { + must => [ + { term => { 'name' => $release } }, + { term => { author => uc($author) } } + ] } } } @@ -73,14 +70,11 @@ sub latest_by_author { '/release/_search', { query => { - filtered => { - query => { match_all => {} }, - filter => { - and => [ - { term => { author => uc($author) } }, - { term => { status => 'latest' } } - ] - }, + bool => { + must => [ + { term => { author => uc($author) } }, + { term => { status => 'latest' } } + ] } }, sort => [ @@ -100,15 +94,8 @@ sub all_by_author { return $self->request( '/release/_search', { - query => { - filtered => { - query => { match_all => {} }, - filter => { - term => { author => uc($author) } - }, - } - }, - sort => [ { date => 'desc' } ], + query => { term => { author => uc($author) } }, + sort => [ { date => 'desc' } ], fields => [qw(author distribution name status abstract date)], size => $size, from => ( $page - 1 ) * $size, @@ -152,49 +139,54 @@ sub modules { '/file/_search', { query => { - filtered => { - query => { match_all => {} }, - filter => { - and => [ - { term => { release => $release } }, - { term => { author => $author } }, - { term => { directory => 0 } }, - { - or => [ + bool => { + must => [ + { term => { release => $release } }, + { term => { author => $author } }, + { term => { directory => 0 } }, + { + bool => { + should => [ { - and => [ - { - exists => { - field => 'module.name' - } - }, - { - term => { - 'module.indexed' => 1 + bool => { + must => [ + { + exists => { + field => 'module.name' + } + }, + { + term => { + 'module.indexed' => 1 + } } - } - ] + ] + } }, { - and => [ - { - range => - { slop => { gt => 0 } } - }, - { - exists => { - field => 'pod.analyzed' - } - }, - { - term => { 'indexed' => 1 } - }, - ] + bool => { + must => [ + { + range => { + slop => { gt => 0 } + } + }, + { + exists => { + field => + 'pod.analyzed' + } + }, + { + term => { 'indexed' => 1 } + }, + ] + } } ] } - ] - } + } + ] } }, size => 999, @@ -227,18 +219,15 @@ sub find { '/release/_search', { query => { - filtered => { - query => { match_all => {} }, - filter => { - and => [ - { - term => { - 'distribution' => $distribution - } - }, - { term => { status => 'latest' } } - ] - } + bool => { + must => [ + { + term => { + 'distribution' => $distribution + } + }, + { term => { status => 'latest' } } + ] } }, sort => [ { date => 'desc' } ], @@ -259,14 +248,11 @@ sub reverse_dependencies { "/search/reverse_dependencies/$distribution", { query => { - filtered => { - query => { 'match_all' => {} }, - filter => { - and => [ - { term => { 'status' => 'latest' } }, - { term => { 'authorized' => 1 } }, - ] - } + bool => { + must => [ + { term => { 'status' => 'latest' } }, + { term => { 'authorized' => 1 } }, + ] } }, size => $page_size, @@ -295,67 +281,64 @@ sub interesting_files { '/file/_search', { query => { - filtered => { - query => { match_all => {} }, - filter => { - and => [ - { term => { release => $release } }, - { term => { author => $author } }, - { term => { directory => \0 } }, - { not => { prefix => { 'path' => 'xt/' } } }, - { not => { prefix => { 'path' => 't/' } } }, - { - or => [ + bool => { + must => [ + { term => { release => $release } }, + { term => { author => $author } }, + { term => { directory => \0 } }, + { not => { prefix => { 'path' => 'xt/' } } }, + { not => { prefix => { 'path' => 't/' } } }, + { + bool => { + should => [ { - and => [ - { term => { level => 0 } }, - { - or => [ - map { - { - term => { - 'name' => $_ - } - } - } qw( - AUTHORS - Build.PL - CHANGELOG - CHANGES - CONTRIBUTING - CONTRIBUTING.md - COPYRIGHT - CREDITS - ChangeLog - Changelog - Changes - Copying - FAQ - INSTALL - INSTALL.md - LICENCE - LICENSE - MANIFEST - META.json - META.yml - Makefile.PL - NEWS - README - README.markdown - README.md - README.mdown - README.mkdn - THANKS - TODO - ToDo - Todo - cpanfile - dist.ini - minil.toml - ) - ] - } - ] + bool => { + must => [ + { term => { level => 0 } }, + { + terms => { + name => [ + qw( + AUTHORS + Build.PL + CHANGELOG + CHANGES + CONTRIBUTING + CONTRIBUTING.md + COPYRIGHT + CREDITS + ChangeLog + Changelog + Changes + Copying + FAQ + INSTALL + INSTALL.md + LICENCE + LICENSE + MANIFEST + META.json + META.yml + Makefile.PL + NEWS + README + README.markdown + README.md + README.mdown + README.mkdn + THANKS + TODO + ToDo + Todo + cpanfile + dist.ini + minil.toml + ) + ] + } + } + ] + } }, map { { prefix => { 'name' => $_ } }, @@ -369,8 +352,8 @@ sub interesting_files { ) ] } - ] - } + } + ] } }, @@ -392,14 +375,9 @@ sub versions { $self->request( '/release/_search', { - query => { - filtered => { - query => { match_all => {} }, - filter => { term => { distribution => $dist } } - } - }, - size => 250, - sort => [ { date => 'desc' } ], + query => { term => { distribution => $dist } }, + size => 250, + sort => [ { date => 'desc' } ], fields => [qw( name date author version status maturity authorized )], } @@ -457,14 +435,11 @@ sub no_latest { { size => scalar @distributions, query => { - filtered => { - query => { match_all => {} }, - filter => { - and => [ - { terms => { distribution => \@distributions } }, - { term => { status => 'latest' } } - ] - } + bool => { + must => [ + { terms => { distribution => \@distributions } }, + { term => { status => 'latest' } } + ] } }, fields => [qw(distribution status)] From 1da3decd9f0d9368d0360bc2a354853bf7d56446 Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Tue, 23 May 2017 13:49:03 +0100 Subject: [PATCH 2/2] convert deprecated use of and/or/not in queries --- lib/MetaCPAN/Web/Controller/Activity.pm | 19 +++++++++++-------- lib/MetaCPAN/Web/Controller/Mirrors.pm | 17 +++++++++++------ lib/MetaCPAN/Web/Model/API/Author.pm | 4 +--- lib/MetaCPAN/Web/Model/API/Favorite.pm | 4 +--- lib/MetaCPAN/Web/Model/API/Lab.pm | 14 ++++++-------- lib/MetaCPAN/Web/Model/API/Release.pm | 15 +++++++-------- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/MetaCPAN/Web/Controller/Activity.pm b/lib/MetaCPAN/Web/Controller/Activity.pm index e4a4d9d906..5910f0b4ec 100644 --- a/lib/MetaCPAN/Web/Controller/Activity.pm +++ b/lib/MetaCPAN/Web/Controller/Activity.pm @@ -43,14 +43,17 @@ sub index : Path : Args(0) { aggregations => { histo => { filter => { - and => [ - { - range => { - date => { from => $start->epoch . '000' } - } - }, - @$q - ] + bool => { + must => [ + { + range => { + date => + { from => $start->epoch . '000' } + } + }, + @$q + ] + } }, aggregations => { entries => { diff --git a/lib/MetaCPAN/Web/Controller/Mirrors.pm b/lib/MetaCPAN/Web/Controller/Mirrors.pm index ff5165b7e6..dec1dcc4a9 100644 --- a/lib/MetaCPAN/Web/Controller/Mirrors.pm +++ b/lib/MetaCPAN/Web/Controller/Mirrors.pm @@ -14,7 +14,7 @@ sub index : Path : Args(0) { my $location; my @protocols; - if ( my $q = $c->req->parameters->{q} ) { + if ( my $q = $c->req->parameters->{'q'} ) { my @parts = split( /\s+/, $q ); foreach my $part (@parts) { push( @protocols, $part ) @@ -25,16 +25,21 @@ sub index : Path : Args(0) { } } - my @or; - push( @or, { not => { filter => { missing => { field => $_ } } } } ) - for (@protocols); + my @filters + = map +{ filter => { missing => { field => $_ } } }, + @protocols; my $data = $c->model('API')->request( '/mirror/_search', { size => 999, - query => { match_all => {} }, - @or ? ( filter => { and => \@or } ) : (), + query => ( + @filters + ? { + bool => { must_not => { bool => should => \@filters } } + } + : { match_all => {} } + ), $location ? ( sort => { diff --git a/lib/MetaCPAN/Web/Model/API/Author.pm b/lib/MetaCPAN/Web/Model/API/Author.pm index c5b3c8d823..76e976ec52 100644 --- a/lib/MetaCPAN/Web/Model/API/Author.pm +++ b/lib/MetaCPAN/Web/Model/API/Author.pm @@ -98,9 +98,7 @@ sub by_user { my $query = return $self->request( '/author/_search', { - query => { match_all => {} }, - filter => - { or => [ map { { term => { user => $_ } } } @{$users} ] }, + query => { terms => { user => $users } }, fields => [qw(user pauseid)], size => 100 } diff --git a/lib/MetaCPAN/Web/Model/API/Favorite.pm b/lib/MetaCPAN/Web/Model/API/Favorite.pm index e51aa20fec..a2256622df 100644 --- a/lib/MetaCPAN/Web/Model/API/Favorite.pm +++ b/lib/MetaCPAN/Web/Model/API/Favorite.pm @@ -182,9 +182,7 @@ sub plusser_by_id { return $self->request( '/author/_search', { - query => { match_all => {} }, - filter => - { or => [ map { { term => { user => $_ } } } @{$users} ] }, + query => { terms => { user => $users } }, _source => { includes => [qw(pauseid gravatar_url)] }, size => 1000, sort => ['pauseid'] diff --git a/lib/MetaCPAN/Web/Model/API/Lab.pm b/lib/MetaCPAN/Web/Model/API/Lab.pm index 3634030206..70967d5b2e 100644 --- a/lib/MetaCPAN/Web/Model/API/Lab.pm +++ b/lib/MetaCPAN/Web/Model/API/Lab.pm @@ -75,20 +75,18 @@ sub _handle_module { sub fetch_latest_distros { my ( $self, $size, $pauseid ) = @_; -# status can have all kinds of values, cpan is an attempt to find the ones that are on cpan but -# are not authorized. Maybe it also includes ones that were superseeded by releases of other people - my @filter = ( { not => { term => { status => 'backpan' } } } ); - if ($pauseid) { - push @filter, { term => { author => $pauseid } }; - } - my $cv = $self->cv; my $r = $self->request( '/release/_search', { query => { bool => { - must => \@filter, + must => [ + { terms => { status => [qw< cpan latest >] } }, + ( + $pauseid ? { term => { author => $pauseid } } : () + ), + ], } }, sort => [ diff --git a/lib/MetaCPAN/Web/Model/API/Release.pm b/lib/MetaCPAN/Web/Model/API/Release.pm index adef3d69e1..65cdeac354 100644 --- a/lib/MetaCPAN/Web/Model/API/Release.pm +++ b/lib/MetaCPAN/Web/Model/API/Release.pm @@ -52,13 +52,12 @@ sub _new_distributions_query { return { constant_score => { filter => { - and => [ - { term => { first => 1 } }, - { - not => - { filter => { term => { status => 'backpan' } } } - }, - ] + bool => { + must => [ + { term => { first => 1 } }, + { terms => { status => [qw< cpan latest >] } }, + ] + } } } }; @@ -116,7 +115,7 @@ sub recent { $query = { constant_score => { filter => { - not => { filter => { term => { status => 'backpan' } } } + terms => { status => [qw< cpan latest >] } } } };