Skip to content

Commit

Permalink
copy a type between indexes (workaround reindex api)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed Nov 15, 2016
1 parent 9f8729d commit 3a56b4d
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion lib/MetaCPAN/Script/Mapping.pm
Expand Up @@ -46,6 +46,20 @@ has patch_mapping => (
documentation => 'type mapping patches',
);

has copy_to_index => (
is => 'ro',
isa => Str,
default => "",
documentation => 'index to copy type to',
);

has copy_type => (
is => 'ro',
isa => Str,
default => "",
documentation => 'type to copy',
);

has reindex => (
is => 'ro',
isa => Bool,
Expand All @@ -64,6 +78,7 @@ sub run {
my $self = shift;
$self->index_create if $self->create_index;
$self->index_delete if $self->delete_index;
$self->copy_index if $self->copy_to_index;
$self->types_list if $self->list_types;
$self->delete_mapping if $self->delete;
}
Expand All @@ -81,7 +96,6 @@ sub _check_index_exists {
log_error {"Index doesn't exists: $name"};
exit 0;
}

}

sub index_delete {
Expand Down Expand Up @@ -152,6 +166,38 @@ sub index_create {
if @patch_types;
}

sub copy_index {
my $self = shift;
my $type = $self->copy_type;
$type or die "can't copy without a type\n";

my $scroll = $self->es()->scroll_helper(
size => 500,
scroll => '30m',
index => $self->index->name,
type => $type,
body => { query => { filtered => { query => { match_all => {} } } } },
);

my $bulk = $self->es->bulk_helper(
index => $self->copy_to_index,
type => $type,
max_count => 500,
);

while ( my $search = $scroll->next ) {
$bulk->create(
{
id => $search->{_id},
source => $search->{_source}
}
);
}

$bulk->flush;

}

sub types_list {
my $self = shift;
print "$_\n" for sort keys %{ $self->index->types };
Expand Down

0 comments on commit 3a56b4d

Please sign in to comment.