oils_persist:tablename="elastic.index"
reporter:label="Elastic Index">
<fields oils_persist:primary="id" oils_persist:sequence="elastic.index_id_seq">
+ <field name="id" reporter:selector="name" reporter:datatype="id"/>
<field reporter:label="Cluster" name="cluster" reporter:datatype="link"/>
<field reporter:label="Index Class" name="index_class" reporter:datatype="text"/>
<field reporter:label="Index Name" name="name" reporter:datatype="text"/>
# load the config via cstore.
sub load_config {
- my $self = @_;
+ my ($self) = @_;
my $e = new_editor();
my $cluster = $self->cluster;
}
}
-sub find_or_create_index_config {
+sub find_index_config {
my $self = shift;
my ($conf) = grep {
$_->name eq $self->index_name &&
- $_->type eq $self->index_class
+ $_->index_class eq $self->index_class
} @{$self->indices};
+ return $conf;
+}
+
+sub find_or_create_index_config {
+ my $self = shift;
+
+ my $conf = $self->find_index_config;
return $conf if $conf;
$logger->info("ES creating new index configuration for ".
- sprintf("cluster=%s type=%s name=%s",
+ sprintf("cluster=%s index_class=%s name=%s",
$self->cluster, $self->index_class, $self->index_name));
my $e = new_editor(xact => 1);
$conf->cluster($self->cluster);
$conf->index_class($self->index_class);
- $conf->index_name($self->index_name);
+ $conf->name($self->index_name);
# Created by default with active=false and num_shards=1
unless ($e->create_elastic_index($conf)) {
- $logger->error("ES failed creating index " .
- $self->index_name . ": " . $e->die_event);
+ $logger->error("ES failed creating index ".$self->index_name);
return undef;
}
}
}
+# Activates the currently loaded index while deactivating any active
+# index with the same cluster and index_class.
+sub activate_index {
+ my ($self) = @_;
+
+ my $index = $self->index_name;
+
+ if (!$self->es->indices->exists(index => $index)) {
+ $logger->warn("ES cannot activate index '$index' which does not exist");
+ return;
+ }
+
+ my ($active) = grep {
+ $_->index_class eq $self->index_class &&
+ $_->cluster eq $self->cluster &&
+ $_->active eq 't' &&
+ $_->name ne $index
+ } @{$self->indices};
+
+ my $e = new_editor(xact => 1);
+
+ if ($active) {
+ $logger->info(
+ "ES deactivating index ".$active->name." before activating $index");
+
+ $active->active('f');
+ unless ($e->update_elastic_index($active)) {
+ $logger->error("ES failed deactivating index ".$active->name);
+ $e->rollback;
+ return 0;
+ }
+ }
+
+ my $conf = $self->find_index_config;
+
+ if (!$conf) {
+ $logger->error("ES no such index to activate: $index");
+ $e->rollback;
+ return 0;
+ }
+
+ $conf->active('t');
+ unless ($e->update_elastic_index($conf)) {
+ $logger->error("ES failed deactivating index: $index");
+ $e->rollback;
+ return 0;
+ }
+
+ $e->commit;
+
+ return 1;
+}
+
sub delete_index {
my ($self) = @_;
my $delete_index;
my $index_class = 'bib-search';
my $index_name;
+my $activate_index;
my $populate;
my $index_record;
my $start_record;
'index-name=s' => \$index_name,
'index-class=s' => \$index_class,
'index-record=s' => \$index_record,
+ 'activate-index' => \$activate_index,
'start-record=s' => \$start_record,
'stop-record=s' => \$stop_record,
'modified-since=s' => \$modified_since,
--index-class <class>
Specifies which data set the current index manages (e.g. bib-search)
+ Must match a well-known index class with backing code.
--index-name <name>
- Specify an index name. Defaults to --index-class.
+ Specify an index name. Can be any value ES index name.
+ Defaults to --index-class.
--delete-index
Delete the specified index and all of its data.
--create-index
Create an index whose name equals --index-name.
+ --activate-index
+ Activate the selected index while deactivating all other
+ indexes of the same index_class and cluster.
+
--batch-size <number>
Index at most this many records per batch.
Default is 500.
$es->create_index or die "Index create failed.\n";
}
+if ($activate_index) {
+ $es->activate_index or die "Index activation failed.\n";
+}
+
if ($populate) {
my $settings = {