lib/App/ElasticSearch/Utilities/Query.pm

filter: Statementstmt 50.6% 110 / 217 Branchbran 31.6% 19 / 60 Conditioncond 24.0% 6 / 25 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 62.8% 22 / 35 Podpod 100% 13 / 13 totaltotal 47.1% 172 / 365 CC 50 SCAR 28.1
CC
50
Cov
45%
aggregations_by
72.0
scroll_results
30.0
request_body
22.0
CRAP 16.6
« lib/App/ElasticSearch/Utilities/Aggregations.pm ↑ Summary lib/App/ElasticSearch/Utilities/QueryString.pm »
1 package App::ElasticSearch::Utilities::Query;
2 # ABSTRACT: Object representing ES Queries
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '9.1'; # VERSION
8  
9 5 ▶use App::ElasticSearch::Utilities qw(es_request es_indices);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use App::ElasticSearch::Utilities::Aggregations;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use CLI::Helpers qw(:output);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Ref::Util qw(is_arrayref is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Types::Standard qw(ArrayRef Enum HashRef Int Maybe Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Types::ElasticSearch qw(TimeConstant is_TimeConstant);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 const my $AGG_KEY => 'aggregations';
21 my %TO = (
22 19 ▶ array_ref => sub { defined $_[0] && is_arrayref($_[0]) ? $_[0] : defined $_[0] ? [ $_[0] ] : $_[0] },
Subroutine __ANON__: called CC=4 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0] && Ref::Util::XS::is_arrayref($_[0]) ? : 19 0 19
defined $_[0] ? : 0 0 0
Condition33%1/3
defined $_[0] and Ref::Util::XS::is_arrayref($_[0])
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $_[0] and Ref::Util::XS::is_arrayref($_[0])
defined $_[0] Ref::Util::XS::is_arrayref($_[0])
Truth table33%1/3
A and B
A= defined $_[0]B= Ref::Util::XS::is_arrayref($_[0])
ABresult
0X0
100
111
23 5 );
24  
25  
26 has fields_meta => (
27 is => 'rw',
28 isa => HashRef,
29 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 5 );
31  
32  
33 has query_stash => (
34 is => 'rw',
35 isa => HashRef,
36 lazy => 1,
37 init_arg => undef,
38 15 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
39 5 );
40  
41  
42 5 has scroll_id => (
43 is => 'rw',
44 isa => Str,
45 init_arg => undef,
46 writer => 'set_scroll_id',
47 clearer => 1,
48 );
49  
50  
51  
52 5 has 'minimum_should_match' => (
53 is => 'rw',
54 isa => Str,
55 );
56  
57 my %QUERY = (
58 must => { isa => ArrayRef, coerce => $TO{array_ref} },
59 must_not => { isa => ArrayRef, coerce => $TO{array_ref} },
60 should => { isa => ArrayRef, coerce => $TO{array_ref} },
61 filter => { isa => ArrayRef, coerce => $TO{array_ref} },
62 5 nested => { isa => HashRef },
63 nested_path => { isa => Str },
64 );
65  
66  
67  
68 my %REQUEST_BODY = (
69 from => { isa => Int },
70 0 ▶ size => { default => sub {50}, isa => Int },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 fields => { isa => ArrayRef, coerce => $TO{array_ref} },
72 sort => { isa => ArrayRef, coerce => $TO{array_ref} },
73 5 aggregations => { isa => HashRef },
74 );
75  
76  
77 my %PARAMS = (
78 scroll => { isa => Maybe[TimeConstant] },
79 timeout => { isa => TimeConstant },
80 terminate_after => { isa => Int },
81 0 ▶ track_total_hits => { isa => Enum[qw( true false )], default => sub { 'true' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 track_scores => { isa => Enum[qw( true false )] },
83 search_type => { isa => Enum[qw( dfs_query_then_fetch query_then_fetch )] },
84 0 ▶ rest_total_hits_as_int => { isa => Enum[qw( true false )], default => sub { 'true' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 5 );
86  
87 # Dynamically build our attributes
88 5 foreach my $attr (keys %QUERY) {
89 has $attr => (
90 is => 'rw',
91 lazy => 1,
92 writer => "set_$attr",
93 30 %{ $QUERY{$attr} },
94 );
95 }
96 5 foreach my $attr (keys %REQUEST_BODY) {
97 has $attr => (
98 is => 'rw',
99 lazy => 1,
100 writer => "set_$attr",
101 init_arg => undef,
102 25 %{ $REQUEST_BODY{$attr} },
103 );
104 }
105 5 foreach my $attr (keys %PARAMS) {
106 has $attr => (
107 is => 'rw',
108 lazy => 1,
109 writer => "set_$attr",
110 35 %{ $PARAMS{$attr} },
111 );
112 }
113  
114  
115 0 ▶sub as_search {
Subroutine as_search: not called CC=1 SCAR=6.9
Pod documented
116 0 my ($self,$indexes) = @_;
117 return (
118 0 _search => {
119 index => $indexes,
120 uri_param => $self->uri_params,
121 method => 'POST',
122 },
123 $self->request_body,
124 );
125 }
126  
127  
128 0 ▶sub execute {
Subroutine execute: not called CC=3 SCAR=24.8
Pod documented
129 0 my($self,$indexes) = @_;
130  
131 # Default to context based indexes
132 0 ▶ $indexes ||= es_indices();
Condition0%0/3
$indexes ||= es_indices()
l!l&&r!l&&!r
000
MC/DC0%0/2
$indexes ||= es_indices()
$indexes es_indices()
Truth table0%0/3
A ||= B
A= $indexesB= es_indices()
ABresult
1X1
011
000
133  
134 0 my $result = es_request( $self->as_search($indexes) );
135  
136 0 ▶ if( $result->{_scroll_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"_scroll_id"}) 0 0 0
137 $self->set_scroll_id($result->{_scroll_id})
138 0 }
139  
140 0 return $result;
141 }
142  
143  
144 0 ▶sub scroll_results {
Subroutine scroll_results: not called CC=5 SCAR=34.0
Pod documented
145 0 my($self) = @_;
146 0 my $result;
147 0 ▶ if( $self->scroll_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scroll_id) 0 0 0
148 0 $result = es_request( '_search/scroll',
149 { method => 'POST' },
150 {
151 scroll => $self->scroll,
152 scroll_id => $self->scroll_id,
153 }
154 );
155 0 ▶ if( $result && $result->{_scroll_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $result->{'_scroll_id'}) { } 0 0 0
Condition0%0/3
$result and $result->{"_scroll_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $result->{"_scroll_id"}
$result $result->{"_scroll_id"}
Truth table0%0/3
A and B
A= $resultB= $result->{"_scroll_id"}
ABresult
0X0
100
111
156 $self->set_scroll_id($result->{_scroll_id})
157 0 }
158 else {
159 0 $self->clear_scroll_id();
160 }
161 }
162 0 ▶ return $result ? $result : ();
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
163 }
164  
165  
166 0 ▶sub uri_params {
Subroutine uri_params: not called CC=4 SCAR=25.6
Pod documented
167 0 my $self = shift;
168  
169 0 my %params=();
170 0 foreach my $field (keys %PARAMS) {
171 0 my $v;
172 0 eval {
173 ## no critic
174 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
175 0 $v = $self->$field();
176 ## user critic
177 };
178 0 ▶ debug({color=>'magenta'}, sprintf "uri_params() - retrieving param '%s' got '%s'",
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
179 $field, ( defined $v ? $v : '' ),
180 );
181  
182 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
183 0 $params{$field} = $v;
184 }
185 0 return \%params;
186 }
187  
188  
189 0 ▶sub request_body {
Subroutine request_body: not called CC=5 SCAR=30.9
Pod documented
190 0 my $self = shift;
191  
192 0 my %body = ();
193 0 my %map = qw( fields _source );
194 0 foreach my $section (keys %REQUEST_BODY) {
195 0 my $val;
196 eval {
197 ## no critic
198 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
199 0 $val = $self->$section;
200 ## use critic
201 0 1;
202 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { no strict 'refs'; $val = $self->$section; 1 } } or do { debug({"color", "red"}, "request_body() - Failed to retrieve '${section}'") } 0 0 0
203 0 debug({color=>'red'}, "request_body() - Failed to retrieve '$section'");
204 };
205 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
206 0 my $data = { $section => $val };
207 0 ▶ my $param = $map{$section} || $section;
Condition0%0/3
$map{$section} || $section
l!l&&r!l&&!r
000
MC/DC0%0/2
$map{$section} || $section
$map{$section} $section
Truth table0%0/3
A || B
A= $map{$section}B= $section
ABresult
1X1
011
000
208 0 $body{$param} = $val;
209 }
210 0 $body{query} = $self->query;
211 0 return \%body;
212 }
213  
214  
215 18 ▶sub query {
Subroutine query: called CC=14 SCAR=28.6
Pod documented
216 18 my $self = shift;
217  
218 18 my $qref;
219 18 ▶ if( $self->nested ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nested) { } 0 18 18
220 0 $qref = {
221 nested => {
222 path => $self->nested_path,
223 query => $self->nested,
224 }
225 };
226 }
227 else {
228 18 ▶ my %bool = (
Branch: 1 branch
BranchTrueFalseTotal
$self->minimum_should_match ? : 1 17 18
229 $self->minimum_should_match ? ( minimum_should_match => $self->minimum_should_match ) : (),
230 );
231 18 foreach my $k (keys %QUERY) {
232 108 ▶ next if $k =~ /^nested/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^nested/ 36 72 108
233 72 $bool{$k} = [];
234 72 my $v;
235 eval {
236 72 debug({color=>'yellow'}, "query() - retrieving section '$k'");
237 ## no critic
238 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
239 72 $v = $self->$k();
240 ## user critic
241 72 ▶ debug_var({color=>'cyan'},$v) if defined $v and ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and ref $v 20 52 72
Condition66%2/3
defined $v and ref $v
!ll&&!rl&&r
52020
MC/DC50%1/2
defined $v and ref $v
defined $v ref $v
Truth table66%2/3
A and B
A= defined $vB= ref $v
ABresult
0X0
100
111
242 72 1;
243 72 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { debug({"color", "yellow"}, "query() - retrieving section '${k}'"); no strict 'refs'; $v = $self->$k; debug_var({"color", "cyan"}, $v) if defined $v and ref $v; 1 } } or do { debug({"color", "red"}, "query() - Failed to retrieve '${k}'") } 72 0 72
244 0 debug({color=>'red'}, "query() - Failed to retrieve '$k'");
245 };
246 72 ▶ $bool{$k} = clone $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 20 52 72
247 72 ▶ if(my $stash = $self->stash($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stash = $self->stash($k)) 0 72 72
248 0 ▶ push @{ $bool{$k} }, is_arrayref($stash) ? @{ $stash } : $stash;
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($stash) ? : 0 0 0
249 }
250 72 ▶ delete $bool{$k} if exists $bool{$k} and is_arrayref($bool{$k}) and not @{ $bool{$k} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $bool{$k} and Ref::Util::XS::is_arrayref($bool{$k}) and not @{$bool{$k};} 52 20 72
Condition50%3/6
exists $bool{$k} and Ref::Util::XS::is_arrayref($bool{$k}) and not @{$bool{$k};}
!ll&&!rl&&r
02052
exists $bool{$k} and Ref::Util::XS::is_arrayref($bool{$k})
!ll&&!rl&&r
0072
MC/DC33%1/3
exists $bool{$k} and Ref::Util::XS::is_arrayref($bool{$k}) and not @{$bool{$k};}
exists $bool{$k} Ref::Util::XS::is_arrayref($bool{$k}) not @{$bool{$k};}
Truth table50%2/4
A and B and not(C)
A= exists $bool{$k}B= Ref::Util::XS::is_arrayref($bool{$k})C= not @{$bool{$k};}
ABCresult
0XX0
10X0
1100
1111
251 }
252 18 $qref = { bool => \%bool };
253 }
254 18 return $qref;
255 }
256  
257  
258 0 ▶sub add_aggregations {
Subroutine add_aggregations: not called CC=3 SCAR=24.8
Pod documented
259 0 my $self = shift;
260 0 my %aggs = @_;
261  
262 0 my $aggs = $self->aggregations();
263 0 ▶ $aggs ||= {};
Condition0%0/2
$aggs ||= {}
l!l
00
MC/DC0%0/1
$aggs ||= {}
$aggs
Truth table0%0/2
A
A= $aggs
Aresult
11
00
264 0 foreach my $agg (keys %aggs) {
265 0 debug("aggregation[$agg] added to query");
266 0 $aggs->{$agg} = $aggs{$agg};
267 }
268 0 $self->set_aggregations($aggs);
269 0 $self->set_size(0);
270 0 $self->set_scroll(undef);
271 }
272  
273  
274 0 ▶sub wrap_aggregations {
Subroutine wrap_aggregations: not called CC=3 SCAR=24.8
Pod documented
275 0 my $self = shift;
276 0 my %wrapper = @_;
277 0 my $aggs = $self->aggregations;
278  
279 0 ▶ if( keys %{ $aggs } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$aggs;}) 0 0 0
280 0 foreach my $a (keys %wrapper) {
281 0 $wrapper{$a}->{$AGG_KEY} = clone $aggs;
282 }
283 }
284  
285 0 $self->set_aggregations(\%wrapper);
286 }
287  
288  
289 0 ▶sub aggregations_by {
Subroutine aggregations_by: not called CC=8 SCAR=42.8
Pod documented
290 0 my ($self,$dir,$aggs) = @_;
291  
292 0 my @sort = ();
293 0 my %aggs = ();
294 0 foreach my $def (@{ $aggs }) {
295 0 my ($name,$agg) = %{ expand_aggregate_string($def) };
296 0 ▶ next unless is_single_stat(keys %{ $agg });
Branch: 1 branch
BranchTrueFalseTotal
unless is_single_stat(keys %{$agg;}) 0 0 0
297 0 $aggs{$name} = $agg;
298 0 push @sort, { $name => $dir };
299 }
300 0 ▶ if( @sort ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sort) 0 0 0
301 0 push @sort, { '_count' => 'desc' };
302  
303 0 my $ref_aggs = $self->aggregations;
304 0 foreach my $name ( keys %{ $ref_aggs } ) {
305 0 foreach my $k ( keys %{ $ref_aggs->{$name} } ) {
306 0 ▶ next if $k eq $AGG_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if $k eq $AGG_KEY 0 0 0
307 0 $ref_aggs->{$name}{$k}{order} = \@sort;
308 0 foreach my $agg (keys %aggs) {
309 0 $ref_aggs->{$name}{$AGG_KEY}{$agg} = $aggs{$agg};
310 }
311 }
312 }
313 0 $self->set_aggregations( $ref_aggs );
314 }
315 }
316  
317 # Support Short-hand like ES
318 5 *aggs = \&aggregations;
319 5 *set_aggs = \&set_aggregations;
320 5 *add_aggs = \&add_aggregations;
321 5 *wrap_aggs = \&wrap_aggregations;
322 5 *aggs_by = \&aggregations_by;
323  
324  
325  
326  
327 0 ▶sub set_scan_scroll {
Subroutine set_scan_scroll: not called CC=3 SCAR=24.8
Pod documented
328 0 my ($self,$ctxt_life) = @_;
329  
330 # Validate Context Lifetime
331 0 ▶ if( !is_TimeConstant( $ctxt_life) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_TimeConstant($ctxt_life)) 0 0 0
332 0 undef($ctxt_life);
333 }
334 0 ▶ $ctxt_life ||= '1m';
Condition0%0/2
$ctxt_life ||= "1m"
l!l
00
MC/DC0%0/1
$ctxt_life ||= "1m"
$ctxt_life
Truth table0%0/2
A
A= $ctxt_life
Aresult
11
00
335  
336 0 $self->set_sort( [qw(_doc)] );
337 0 $self->set_scroll( $ctxt_life );
338 0 $self;
339 }
340  
341 0 ▶sub set_match_all {
Subroutine set_match_all: not called CC=1 SCAR=6.9
Pod documented
342 0 my ($self) = @_;
343 # Reset the relevant pieces of the query
344 0 $self->set_must_not([]);
345 0 $self->set_filter([]);
346 0 $self->set_should([]);
347 # Set the match_all bits
348 0 $self->set_must({match_all=>{}});
349 0 $self;
350 }
351  
352  
353 19 ▶sub add_bool {
Subroutine add_bool: called CC=4 SCAR=14.0
Pod documented
354 19 my $self = shift;
355 19 my %bools = @_;
356 19 foreach my $section ( sort keys %bools ) {
357 19 ▶ next unless exists $QUERY{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $QUERY{$section} 0 19 19
358 ## no critic
359 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
360 19 my $set = $self->$section;
361 0 ▶ push @{ $set }, is_arrayref($bools{$section}) ? @{ $bools{$section} } : $bools{$section};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($bools{$section}) ? : 0 19 19
362 19 my $setter = "set_$section";
363 19 $self->$setter($set);
364 ## use critic
365 }
366 19 $self;
367 }
368  
369  
370 72 ▶sub stash {
Subroutine stash: called CC=5 SCAR=20.9
Pod documented
371 72 my ($self,$section,$condition) = @_;
372  
373 72 my $stash = $self->query_stash;
374 72 ▶ if( exists $QUERY{$section} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $QUERY{$section}) 72 0 72
375 72 ▶ if( defined $condition ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $condition) 0 72 72
376 0 ▶ debug({color=>exists $stash->{$section} ? 'green' : 'red' }, "setting $section in stash");
Branch: 1 branch
BranchTrueFalseTotal
exists $stash->{$section} ? : 0 0 0
377 0 $stash->{$section} = $condition;
378 # Reset Scroll ID
379 0 $self->clear_scroll_id();
380 }
381 }
382 72 ▶ return exists $stash->{$section} ? $stash->{$section} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $stash->{$section} ? : 0 72 72
383 }
384  
385 # Return True
386 5 1;
387  
388 __END__
« lib/App/ElasticSearch/Utilities/Aggregations.pm ↑ Summary lib/App/ElasticSearch/Utilities/QueryString.pm »