App-ElasticSearch-Utilities
view release on metacpan or search on metacpan
lib/App/ElasticSearch/Utilities/Query.pm view on Meta::CPAN
debug({color=>'yellow'}, "query() - retrieving section '$k'");
## no critic
no strict 'refs';
$v = $self->$k();
## user critic
debug_var({color=>'cyan'},$v) if defined $v and ref $v;
1;
} or do {
debug({color=>'red'}, "query() - Failed to retrieve '$k'");
};
$bool{$k} = clone $v if defined $v;
if(my $stash = $self->stash($k)) {
push @{ $bool{$k} }, is_arrayref($stash) ? @{ $stash } : $stash;
}
delete $bool{$k} if exists $bool{$k} and is_arrayref($bool{$k}) and not @{ $bool{$k} };
}
$qref = { bool => \%bool };
}
return $qref;
}
sub add_aggregations {
my $self = shift;
my %aggs = @_;
my $aggs = $self->aggregations();
$aggs ||= {};
foreach my $agg (keys %aggs) {
debug("aggregation[$agg] added to query");
$aggs->{$agg} = $aggs{$agg};
}
$self->set_aggregations($aggs);
$self->set_size(0);
$self->set_scroll(undef);
}
sub wrap_aggregations {
my $self = shift;
my %wrapper = @_;
my $aggs = $self->aggregations;
if( keys %{ $aggs } ) {
foreach my $a (keys %wrapper) {
$wrapper{$a}->{$AGG_KEY} = clone $aggs;
}
}
$self->set_aggregations(\%wrapper);
}
sub aggregations_by {
my ($self,$dir,$aggs) = @_;
my @sort = ();
my %aggs = ();
foreach my $def (@{ $aggs }) {
my ($name,$agg) = %{ expand_aggregate_string($def) };
next unless is_single_stat(keys %{ $agg });
$aggs{$name} = $agg;
push @sort, { $name => $dir };
}
if( @sort ) {
push @sort, { '_count' => 'desc' };
my $ref_aggs = $self->aggregations;
foreach my $name ( keys %{ $ref_aggs } ) {
foreach my $k ( keys %{ $ref_aggs->{$name} } ) {
next if $k eq $AGG_KEY;
$ref_aggs->{$name}{$k}{order} = \@sort;
foreach my $agg (keys %aggs) {
$ref_aggs->{$name}{$AGG_KEY}{$agg} = $aggs{$agg};
}
}
}
$self->set_aggregations( $ref_aggs );
}
}
# Support Short-hand like ES
*aggs = \&aggregations;
*set_aggs = \&set_aggregations;
*add_aggs = \&add_aggregations;
*wrap_aggs = \&wrap_aggregations;
*aggs_by = \&aggregations_by;
sub set_scan_scroll {
my ($self,$ctxt_life) = @_;
# Validate Context Lifetime
if( !is_TimeConstant( $ctxt_life) ) {
undef($ctxt_life);
}
$ctxt_life ||= '1m';
$self->set_sort( [qw(_doc)] );
$self->set_scroll( $ctxt_life );
$self;
}
sub set_match_all {
my ($self) = @_;
# Reset the relevant pieces of the query
$self->set_must_not([]);
$self->set_filter([]);
$self->set_should([]);
# Set the match_all bits
$self->set_must({match_all=>{}});
$self;
}
sub add_bool {
my $self = shift;
my %bools = @_;
foreach my $section ( sort keys %bools ) {
( run in 0.530 second using v1.01-cache-2.11-cpan-39bf76dae61 )