return ("english");
}
-sub create_index {
- my ($self) = @_;
+sub create_index_mappings {
+ my ($self, $custom_mappings) = @_;
- if ($self->es->indices->exists(index => $INDEX_NAME)) {
- $logger->warn("ES index '$INDEX_NAME' already exists");
- return;
- }
+ if ($custom_mappings) {
+ $logger->info("ES generating index mappings from custom file $custom_mappings");
- $logger->info(
- "ES creating index '$INDEX_NAME' on cluster '".$self->cluster."'");
+ my $json;
+ {
+ local $/=undef;
+
+ if (!open(MAPPING_FILE, $custom_mappings)) {
+ $logger->error("ES cannot open mappings file: $!");
+ return undef;
+ }
+
+ $json = <MAPPING_FILE>;
+ close MAPPING_FILE;
+ }
+
+ my $struct = OpenSRF::Utils::JSON->JSON2perl($json);
+ return $struct->{'bib-search'}->{'mappings'};
+ }
my $mappings = $BASE_PROPERTIES;
$mappings->{$field_name} = $def;
}
+ return $mappings;
+}
+
+sub create_index {
+ my ($self, $custom_mappings) = @_;
+
+ if ($self->es->indices->exists(index => $INDEX_NAME)) {
+ $logger->warn("ES index '$INDEX_NAME' already exists");
+ return;
+ }
+
+ $logger->info(
+ "ES creating index '$INDEX_NAME' on cluster '".$self->cluster."'");
+
+ my $mappings = $self->create_index_mappings($custom_mappings);
+
my $settings = $BASE_INDEX_SETTINGS;
$settings->{number_of_replicas} = scalar(@{$self->nodes});
$settings->{number_of_shards} = $self->index->num_shards;
body => {settings => $settings}
};
-
$logger->info("ES creating index '$INDEX_NAME'");
# Create the base index with settings
my $modified_since;
my $max_duration;
my $batch_size = 500;
+my $custom_mappings;
# Database settings read from ENV by default.
my $db_host = $ENV{PGHOST} || 'localhost';
'modified-since=s' => \$modified_since,
'max-duration=s' => \$max_duration,
'batch-size=s' => \$batch_size,
+ 'custom-mappings=s' => \$custom_mappings,
'db-name=s' => \$db_name,
'db-host=s' => \$db_host,
'db-port=s' => \$db_port,
are provided (e.g. --index-start-record) then all
applicable values will be indexed.
+ --custom-mappings
+ Path to a JSON file continaining custom index mapping
+ definitions. The mapppings must match the stock mapping
+ structure, fields may only be removed. Added fields will
+ be ignored at data population time (barring code changes).
+
+ For example:
+
+ curl http://ELASTIC_HOST/bib-search > mappings.json
+ # edit mappings.json and remove stuff you don't want.
+ $0 --create-index --custom-mappings mappings.json
+
+ Note that removing field mappings does not remove the
+ data from the source document, it only means the data
+ will not be analyzed/procesed/indexed and it will not be
+ searchable.
+
HELP
exit(0);
}
}
if ($create_index) {
- $es->create_index or die "Index create failed.\n";
+ $es->create_index($custom_mappings) or die "Index create failed.\n";
}
if ($populate) {