From ffee770f6266101eaa362ca864030b0b64d3ca3e Mon Sep 17 00:00:00 2001 From: Mickey Nasriachi Date: Sat, 3 May 2025 14:35:19 +0000 Subject: [PATCH] Mapper module update 1. support modes (local, test, prod) 2. support skip_exists check for index ops (ignore op if index exists) --- lib/MetaCPAN/Mapper.pm | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/MetaCPAN/Mapper.pm b/lib/MetaCPAN/Mapper.pm index 8d86600..9c84e8c 100644 --- a/lib/MetaCPAN/Mapper.pm +++ b/lib/MetaCPAN/Mapper.pm @@ -11,11 +11,17 @@ use Search::Elasticsearch; use MetaCPAN::Ingest qw< config home >; sub new ( $class, %args ) { - my $node = $args{node}; + my $mode = $args{mode} // "local"; + my $node = $args{node}; my $config = config; - $node ||= $config->{es_node}; - $node or die "Cannot create an ES instance without a node\n"; + my $config_node = + $node ? $node : + $mode eq 'local' ? $config->{es_node} : + $mode eq 'test' ? $config->{es_test_node} : + $mode eq 'prod' ? $config->{es_production_node} : + undef; + $config_node or die "Cannot create an ES instance without a node\n"; return bless { es => Search::Elasticsearch->new( @@ -33,24 +39,27 @@ sub index_create ($self, $index) { $self->{es}->indices->create( index => $index ); } -sub index_delete ($self, $index) { +sub index_delete ($self, $index, $skip_exists) { + return if $skip_exists and !$self->index_exists($index); $self->{es}->indices->delete( index => $index ); } -sub index_put_mapping ($self, $index, $type, $mapping) { +sub index_put_mapping ($self, $index, $mapping) { $self->{es}->indices->put_mapping( index => $index, - type => $type, + type => $index, body => $mapping, ); } -sub index_add_mapping ($self, $index, $type) { +sub index_add_mapping ($self, $index, $skip_exists) { + return if $skip_exists and !$self->index_exists($index); + my $home = home(); my $map_file = $home->child('conf/es/' . $index . '/mapping.json'); my $mapping = decode_json $map_file->slurp(); - $self->index_put_mapping($index, $type, $mapping); + $self->index_put_mapping($index, $mapping); } 1;