Skip to content

Commit 6bd4901

Browse files
committed
remove join parameter
The join feature is something we try to add on top of Elasticsearch. It complicates our interactions with external queries and we don't use them anywhere in our own code.
1 parent f67d282 commit 6bd4901

File tree

2 files changed

+2
-117
lines changed

2 files changed

+2
-117
lines changed

lib/MetaCPAN/Server/Controller.pm

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -128,90 +128,6 @@ sub search : Path('_search') : ActionClass('~Deserialize') {
128128
} or do { $self->internal_error( $c, $@ ) };
129129
}
130130

131-
sub join : ActionClass('~Deserialize') {
132-
my ( $self, $c ) = @_;
133-
my $joins = $self->relationships;
134-
my @req_joins = $c->req->param('join');
135-
my $is_get = ref $c->stash->{hits} ? 0 : 1;
136-
my $query
137-
= $c->req->params->{q}
138-
? { query => { query_string => { query => $c->req->params->{q} } } }
139-
: $c->req->data ? $c->req->data
140-
: { query => { match_all => {} } };
141-
$c->detach(
142-
'/not_allowed',
143-
[
144-
'unknown join type, valid values are '
145-
. Moose::Util::english_list( keys %$joins )
146-
]
147-
) if ( scalar grep { !$joins->{$_} } @req_joins );
148-
149-
while ( my ( $join, $config ) = each %$joins ) {
150-
my $has_many = ref $config->{type};
151-
my ($type) = $has_many ? @{ $config->{type} } : $config->{type};
152-
my $cself = $config->{self} || $join;
153-
next unless ( grep { $_ eq $join } @req_joins );
154-
my $data
155-
= $is_get
156-
? [ $c->stash ]
157-
: [
158-
map {
159-
$_->{_source}
160-
|| single_valued_arrayref_to_scalar( $_->{fields} )
161-
} @{ $c->stash->{hits}->{hits} }
162-
];
163-
my @ids = List::AllUtils::uniq grep {defined}
164-
map { ref $cself eq 'CODE' ? $cself->($_) : $_->{$cself} } @$data;
165-
my $filter = { terms => { $config->{foreign} => [@ids] } };
166-
my $filtered = {%$query}; # don't work on $query
167-
$filtered->{filter}
168-
= $query->{filter}
169-
? { and => [ $filter, $query->{filter} ] }
170-
: $filter;
171-
my $foreign = eval {
172-
$c->model("CPAN::$type")->query( $filtered->{query} )
173-
->filter( $filtered->{filter} )->size(1000)->raw->all;
174-
} or do { $self->internal_error( $c, $@ ) };
175-
$c->detach(
176-
"/not_allowed",
177-
[
178-
'The number of joined documents exceeded the allowed number of 1000 documents by '
179-
. ( $foreign->{hits}->{total} - 1000 )
180-
. '. Please reduce the number of documents or apply additional filters.'
181-
]
182-
) if ( $foreign->{hits}->{total} > 1000 );
183-
$c->stash->{took} += $foreign->{took} unless ($is_get);
184-
185-
if ($has_many) {
186-
my $many;
187-
for ( @{ $foreign->{hits}->{hits} } ) {
188-
my $list = $many->{ $_->{_source}->{ $config->{foreign} } }
189-
||= [];
190-
push( @$list, $_ );
191-
}
192-
$foreign = $many;
193-
}
194-
else {
195-
$foreign = { map { $_->{_source}->{ $config->{foreign} } => $_ }
196-
@{ $foreign->{hits}->{hits} } };
197-
}
198-
for (@$data) {
199-
my $key = ref $cself eq 'CODE' ? $cself->($_) : $_->{$cself};
200-
next unless ($key);
201-
my $result = $foreign->{$key};
202-
$_->{$join}
203-
= $has_many
204-
? {
205-
hits => {
206-
hits => $result,
207-
total => scalar @{ $result || [] }
208-
}
209-
}
210-
: $result;
211-
}
212-
}
213-
}
214-
215131
sub not_found : Private {
216132
my ( $self, $c ) = @_;
217133
$c->cdn_never_cache(1);

t/server/controller/author.t

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,9 @@ test_psgi app, sub {
8282
my $json = decode_json_ok($res);
8383
is( @{ $json->{hits}->{hits} }, 0, '0 results' );
8484

85-
ok( $res = $cb->( GET '/author/DOY?join=release' ),
86-
'GET /author/DOY?join=release' );
87-
88-
$json = decode_json_ok($res);
89-
is( @{ $json->{release}->{hits}->{hits} }, 4, 'joined 4 releases' );
90-
91-
ok(
92-
$res = $cb->(
93-
POST '/author/DOY?join=release',
94-
Content => encode_json( {
95-
query => {
96-
constant_score =>
97-
{ filter => { term => { status => 'latest' } } }
98-
}
99-
} )
100-
),
101-
'POST /author/DOY?join=release with query body',
102-
);
103-
104-
$json = decode_json_ok($res);
105-
is( @{ $json->{release}->{hits}->{hits} }, 1, 'joined 1 release' );
106-
is( $json->{release}->{hits}->{hits}->[0]->{_source}->{status},
107-
'latest', '1 release has status latest' );
108-
10985
ok(
11086
$res = $cb->(
111-
POST '/author/_search?join=release',
87+
POST '/author/_search',
11288
Content => encode_json( {
11389
query => {
11490
constant_score => {
@@ -130,21 +106,14 @@ test_psgi app, sub {
130106
}
131107
} )
132108
),
133-
'POST /author/_search?join=release with query body'
109+
'POST /author/_search with query body'
134110
);
135111

136112
my $doy = $json;
137113
$json = decode_json_ok($res);
138114

139115
is( @{ $json->{hits}->{hits} }, 1, '1 hit' );
140116

141-
my $release_count = delete $doy->{release_count};
142-
is_deeply(
143-
[ sort keys %{$release_count} ],
144-
[qw< backpan-only cpan latest >],
145-
'release_count has the correct keys'
146-
);
147-
148117
my $links = delete $doy->{links};
149118
is_deeply(
150119
[ sort keys %{$links} ],

0 commit comments

Comments
 (0)