Skip to content

Commit ec7c8f5

Browse files
committed
favorite: ES syntax update for newer version
1 parent b1d3bc8 commit ec7c8f5

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

bin/favorite.pl

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use MetaCPAN::Logger qw< :log :dlog >;
77

88
use MetaCPAN::ES;
9-
use MetaCPAN::Ingest qw< minion >;
9+
use MetaCPAN::Ingest qw< false minion >;
1010

1111
# args
1212
my ( $age, $check_missing, $count, $distribution, $limit, $queue );
@@ -39,7 +39,7 @@
3939
###
4040

4141
sub index_favorites () {
42-
my $body;
42+
my $query = { match_all => {} };
4343
my $age_filter;
4444

4545
if ($age) {
@@ -48,37 +48,30 @@ ()
4848
}
4949

5050
if ($distribution) {
51-
$body = {
52-
query => {
53-
term => { distribution => $distribution }
54-
}
55-
};
51+
$query = { term => { distribution => $distribution } };
5652
}
5753
elsif ($age) {
5854
my $es = MetaCPAN::ES->new( type => "favorite" );
5955
my $favs = $es->scroll(
6056
scroll => '5m',
61-
fields => [qw< distribution >],
6257
body => {
6358
query => $age_filter,
64-
( $limit ? ( size => $limit ) : () )
59+
_source => [qw< distribution >],
60+
size => $limit || 500,
61+
sort => '_doc',
6562
}
6663
);
6764

6865
my %recent_dists;
6966

7067
while ( my $fav = $favs->next ) {
71-
my $dist = $fav->{fields}{distribution}[0];
68+
my $dist = $fav->{_source}{distribution};
7269
$recent_dists{$dist}++ if $dist;
7370
}
7471

7572
my @keys = keys %recent_dists;
7673
if (@keys) {
77-
$body = {
78-
query => {
79-
terms => { distribution => \@keys }
80-
}
81-
};
74+
$query = { terms => { distribution => \@keys } };
8275
}
8376
$es->index_refresh;
8477
}
@@ -94,12 +87,16 @@ ()
9487
my $es = MetaCPAN::ES->new( type => "favorite" );
9588
my $favs = $es->scroll(
9689
scroll => '30s',
97-
fields => [qw< distribution >],
98-
( $body ? ( body => $body ) : () ),
90+
body => {
91+
query => $query,
92+
_source => [qw< distribution >],
93+
size => 500,
94+
sort => '_doc',
95+
},
9996
);
10097

10198
while ( my $fav = $favs->next ) {
102-
my $dist = $fav->{fields}{distribution}[0];
99+
my $dist = $fav->{_source}{distribution};
103100
$dist_fav_count{$dist}++ if $dist;
104101
}
105102

@@ -119,22 +116,24 @@ ()
119116
my $es = MetaCPAN::ES->new( type => "file" );
120117
my $files = $es->scroll(
121118
scroll => '15m',
122-
fields => [qw< id distribution >],
123-
size => 500,
124119
body => {
125120
query => {
126121
bool => {
127122
must_not => [
128123
{ range => { dist_fav_count => { gte => 1 } } }
129124
],
130125
@age_filter,
131-
}
132-
}
126+
},
127+
},
128+
_source => [qw< id distribution >],
129+
size => 500,
130+
sort => '_doc',
131+
133132
},
134133
);
135134

136135
while ( my $file = $files->next ) {
137-
my $dist = $file->{fields}{distribution}[0];
136+
my $dist = $file->{_source}{distribution};
138137
next unless $dist;
139138
next if exists $missing{$dist} or exists $dist_fav_count{$dist};
140139

@@ -189,20 +188,22 @@ ()
189188
my $bulk = $es->bulk( timeout => '120m' );
190189
my $files = $es->scroll(
191190
scroll => '15s',
192-
fields => [qw< id >],
193191
body => {
194192
query => { term => { distribution => $dist } }
193+
_source => false,
194+
size => 500,
195+
sort => '_doc',
195196
},
196197
);
197198

198199
while ( my $file = $files->next ) {
199-
my $id = $file->{fields}{id}[0];
200+
my $id = $file->{_id};
200201
my $cnt = $dist_fav_count{$dist};
201202

202203
log_debug {"Updating file id $id with fav_count $cnt"};
203204

204205
$bulk->update( {
205-
id => $file->{fields}{id}[0],
206+
id => $file->{_id};
206207
doc => { dist_fav_count => $cnt },
207208
} );
208209
}

0 commit comments

Comments
 (0)