@@ -11,11 +11,17 @@ use Search::Elasticsearch;
11
11
use MetaCPAN::Ingest qw< config home > ;
12
12
13
13
sub new ( $class , %args ) {
14
- my $node = $args {node };
14
+ my $mode = $args {mode } // " local" ;
15
+ my $node = $args {node };
15
16
16
17
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 " ;
19
25
20
26
return bless {
21
27
es => Search::Elasticsearch-> new(
@@ -33,24 +39,27 @@ sub index_create ($self, $index) {
33
39
$self -> {es }-> indices-> create( index => $index );
34
40
}
35
41
36
- sub index_delete ($self , $index ) {
42
+ sub index_delete ($self , $index , $skip_exists ) {
43
+ return if $skip_exists and !$self -> index_exists($index );
37
44
$self -> {es }-> indices-> delete ( index => $index );
38
45
}
39
46
40
- sub index_put_mapping ($self , $index , $type , $ mapping ) {
47
+ sub index_put_mapping ($self , $index , $mapping ) {
41
48
$self -> {es }-> indices-> put_mapping(
42
49
index => $index ,
43
- type => $type ,
50
+ type => $index ,
44
51
body => $mapping ,
45
52
);
46
53
}
47
54
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
+
49
58
my $home = home();
50
59
my $map_file = $home -> child(' conf/es/' . $index . ' /mapping.json' );
51
60
my $mapping = decode_json $map_file -> slurp();
52
61
53
- $self -> index_put_mapping($index , $type , $ mapping );
62
+ $self -> index_put_mapping($index , $mapping );
54
63
}
55
64
56
65
1;
0 commit comments