App-ElasticSearch-Utilities

 view release on metacpan or  search on metacpan

lib/App/ElasticSearch/Utilities.pm  view on Meta::CPAN



sub es_apply_index_settings {
    my($index,$settings) = @_;

    if(!is_hashref($settings)) {
        output({stderr=>1,color=>'red'}, 'usage is es_apply_index_settings($index,$settings_hashref)');
        return;
    }

    return es_request('_settings',{ method => 'PUT', index => $index },$settings);
}


sub es_index_segments {
    my ($index) = @_;

    if( !defined $index || !length $index || !es_index_valid($index) ) {
        output({stderr=>1,color=>'red'}, "es_index_segments('$index'): invalid index");
        return;
    }

lib/App/ElasticSearch/Utilities/Connection.pm  view on Meta::CPAN

    }
    # Body Translations
    if(!defined $body && exists $options->{body}) {
        $body ||= delete $options->{body};
    }

    # Determine request method
    my $method = exists $options->{method} ? uc $options->{method} : 'GET';

    # Special Case for Index Creation
    if( $method eq 'PUT' && $options->{index} && $options->{command} eq '/' ) {
        $uri->path($options->{index});
    }

    debug({color=>'magenta'}, sprintf "Issuing %s with URI of '%s'", $method, $uri->as_string);
    if( defined $body ) {
        if( is_ref($body) )  {
            debug_var({indent=>1}, $body);
        }
        else {
            debug({indent=>1}, split /\r?\n/, $body);

lib/App/ElasticSearch/Utilities/Connection.pm  view on Meta::CPAN

    );

    return $self->request('', \%params,)->is_success;
}


sub put {
    my ($self,%options) = @_;

    return unless exists $options{body};
    my %params = ( method => 'PUT' );
    $params{index} = $options{index} if exists $options{index};

    my $resp = $self->request('', \%params, $options{body});
    return ( $resp->code, $resp->content );
}


sub bulk {
    my ($self,%options) = @_;

scripts/es-cluster-settings.pl  view on Meta::CPAN

    # Add deletes
    if( my $deletes = $opt->delete ) {
        foreach my $setting ( @{ $deletes } ) {
            $settings{$setting} = undef;
        }
    }

    # Peform the operation
    my $data = es_request('/_cluster/settings',
        {
            method => 'PUT',
            uri_param => { flat_settings => 'true' },
        },
        {
            $opt->duration => \%settings,
        }
    );

    die "Failed updating settings" unless $data;

    # Report success/failure

scripts/es-copy-index.pl  view on Meta::CPAN

            debug($content);
            die "Parsing JSON from $OPT{mapping} failed: $@";
        };
    }
    else {
        $mappings = $res->{$INDEX{from}}{mappings};
    }

    $res = es_request($ES{to}, '/',
        {
            method => 'PUT',
            index => $INDEX{to},
        },
        {
            settings => $to_settings,
            $mappings ? ( mappings => $mappings ) : (),
        }
    );

    if (!defined $res || !is_hashref($res) || !$res->{ok}) {
        die "Failed to create index in $HOST{to} : " . $JSON->encode($res);

scripts/es-daily-index-maintenance.pl  view on Meta::CPAN

            my $daily = $CFG{'replicas-max'} / $AGE;
            $replicas = ceil( $CFG{'replicas-max'} - ($daily * $days_old) );
            $replicas = $replicas < $CFG{'replicas-min'} ? $CFG{'replicas-min'} : $replicas;
            $replicas = $replicas > $CFG{'replicas-max'} ? $CFG{'replicas-max'} : $replicas;
        }
        my %shards = es_index_shards($index);
        debug({indent=>1}, "+ replica aging (P:$shards{primaries} R:$shards{replicas}->$replicas)");
        if ( $shards{primaries} > 0 && $shards{replicas} != $replicas ) {
            verbose({color=>'yellow'}, "$index: should have $replicas replicas, has $shards{replicas}");
            my $result = es_request('_settings',
                { index => $index, method => 'PUT' },
                { index => { number_of_replicas => $replicas} },
            );
            if($result) {
                output({color=>"green"}, "$index: Successfully set replicas to $replicas");
            }
            else {
                output({color=>'red'}, "$index: Unable to set replicas");
            }
        }
    }

scripts/es-index-blocks.pl  view on Meta::CPAN

#------------------------------------------------------------------------#
# Report Blocks
if( my @blocks = keys %blocks ) {
    foreach my $block ( sort @blocks ) {
        output({color=>'cyan',clear=>1}, "Index block: $block");
        foreach my $index (sort @{ $blocks{$block} }) {
            output({data=>1}, "$index is $block");
            if( $opt->remove_blocks ) {
                eval {
                    my $result = es_request('_settings',
                        { index  => $index, method => 'PUT' },
                        { $block => 'false' },
                    );
                    die "not acknowledged" unless $result->{acknowledged};
                    output({color=>'green',indent=>1}, "$block removed.");
                    1;
                } or do {
                    my $err = $@;
                    output({color=>'red',indent=>1}, "ERROR removing $block from $index: $err");
                };
            }



( run in 0.526 second using v1.01-cache-2.11-cpan-4e96b696675 )