From 89d86629ecb038a3d62db36204030d0d3dfab991 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Fri, 27 Sep 2019 13:14:00 -0400 Subject: [PATCH] support custom index mappings from file Signed-off-by: Bill Erickson --- .../src/perlmods/lib/OpenILS/Elastic/Bib/Search.pm | 45 +++++++++++++++++----- Open-ILS/src/support-scripts/elastic-index.pl | 21 +++++++++- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Elastic/Bib/Search.pm b/Open-ILS/src/perlmods/lib/OpenILS/Elastic/Bib/Search.pm index 8e19383ff1..8e15361abf 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Elastic/Bib/Search.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Elastic/Bib/Search.pm @@ -180,16 +180,28 @@ sub language_analyzers { 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 = ; + close MAPPING_FILE; + } + + my $struct = OpenSRF::Utils::JSON->JSON2perl($json); + return $struct->{'bib-search'}->{'mappings'}; + } my $mappings = $BASE_PROPERTIES; @@ -261,6 +273,22 @@ sub create_index { $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; @@ -270,7 +298,6 @@ sub create_index { body => {settings => $settings} }; - $logger->info("ES creating index '$INDEX_NAME'"); # Create the base index with settings diff --git a/Open-ILS/src/support-scripts/elastic-index.pl b/Open-ILS/src/support-scripts/elastic-index.pl index 626034ea13..754a84fd1b 100755 --- a/Open-ILS/src/support-scripts/elastic-index.pl +++ b/Open-ILS/src/support-scripts/elastic-index.pl @@ -20,6 +20,7 @@ my $stop_record; 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'; @@ -42,6 +43,7 @@ GetOptions( '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, @@ -112,6 +114,23 @@ sub help { 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); } @@ -141,7 +160,7 @@ if ($delete_index) { } 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) { -- 2.11.0