Skip to content

Commit 34a6c92

Browse files
authored
Merge pull request #67 from metacpan/mickey/mapper
Mapper module update
2 parents f7f1e7a + ffee770 commit 34a6c92

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/MetaCPAN/Mapper.pm

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ use Search::Elasticsearch;
1111
use MetaCPAN::Ingest qw< config home >;
1212

1313
sub new ( $class, %args ) {
14-
my $node = $args{node};
14+
my $mode = $args{mode} // "local";
15+
my $node = $args{node};
1516

1617
my $config = config;
17-
$node ||= $config->{es_node};
18-
$node or die "Cannot create an ES instance without a node\n";
18+
my $config_node =
19+
$node ? $node :
20+
$mode eq 'local' ? $config->{es_node} :
21+
$mode eq 'test' ? $config->{es_test_node} :
22+
$mode eq 'prod' ? $config->{es_production_node} :
23+
undef;
24+
$config_node or die "Cannot create an ES instance without a node\n";
1925

2026
return bless {
2127
es => Search::Elasticsearch->new(
@@ -33,24 +39,27 @@ sub index_create ($self, $index) {
3339
$self->{es}->indices->create( index => $index );
3440
}
3541

36-
sub index_delete ($self, $index) {
42+
sub index_delete ($self, $index, $skip_exists) {
43+
return if $skip_exists and !$self->index_exists($index);
3744
$self->{es}->indices->delete( index => $index );
3845
}
3946

40-
sub index_put_mapping ($self, $index, $type, $mapping) {
47+
sub index_put_mapping ($self, $index, $mapping) {
4148
$self->{es}->indices->put_mapping(
4249
index => $index,
43-
type => $type,
50+
type => $index,
4451
body => $mapping,
4552
);
4653
}
4754

48-
sub index_add_mapping ($self, $index, $type) {
55+
sub index_add_mapping ($self, $index, $skip_exists) {
56+
return if $skip_exists and !$self->index_exists($index);
57+
4958
my $home = home();
5059
my $map_file = $home->child('conf/es/' . $index . '/mapping.json');
5160
my $mapping = decode_json $map_file->slurp();
5261

53-
$self->index_put_mapping($index, $type, $mapping);
62+
$self->index_put_mapping($index, $mapping);
5463
}
5564

5665
1;

0 commit comments

Comments
 (0)