File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -4,17 +4,21 @@ use strict;
4
4
use warnings;
5
5
use feature qw( state) ;
6
6
7
+ use Carp qw( croak ) ;
7
8
use HTTP::Request::Common qw( DELETE GET POST ) ; # # no perlimports
9
+ use MetaCPAN::ESConfig qw( es_doc_path ) ;
8
10
use MetaCPAN::Model ();
9
11
use MetaCPAN::Server ();
10
12
use MetaCPAN::Server::Config ();
11
13
use MetaCPAN::Types::TypeTiny qw( ES ) ;
14
+ use MetaCPAN::Util qw( hit_total ) ;
12
15
use Plack::Test; # # no perlimports
13
16
14
17
use base ' Exporter' ;
15
18
our @EXPORT_OK = qw(
16
19
POST GET DELETE
17
20
es
21
+ es_result
18
22
model
19
23
test_psgi app
20
24
query
@@ -57,6 +61,28 @@ sub query {
57
61
state $query = MetaCPAN::Query-> new( es => es() );
58
62
}
59
63
64
+ sub es_result {
65
+ my ( $type , $query , $size ) = @_ ;
66
+ $size //= wantarray ? 999 : 1;
67
+ if ( !wantarray && $size != 1 ) {
68
+ croak " multiple results requested with scalar return!" ;
69
+ }
70
+ my $res = es()-> search(
71
+ es_doc_path($type ),
72
+ body => {
73
+ size => ( wantarray ? 999 : 1 ),
74
+ query => $query ,
75
+ },
76
+ );
77
+ my @hits = map $_ -> {_source }, @{ $res -> {hits }{hits } };
78
+ if ( !wantarray ) {
79
+ croak " query did not return a single result"
80
+ if hit_total($res ) != 1;
81
+ return $hits [0];
82
+ }
83
+ return @hits ;
84
+ }
85
+
60
86
1;
61
87
62
88
=pod
You can’t perform that action at this time.
0 commit comments