Skip to content

Mapper module update #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/MetaCPAN/Mapper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;