support custom index mappings from file
authorBill Erickson <berickxx@gmail.com>
Fri, 27 Sep 2019 17:14:00 +0000 (13:14 -0400)
committerBill Erickson <berickxx@gmail.com>
Tue, 22 Oct 2019 13:18:21 +0000 (09:18 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Elastic/Bib/Search.pm
Open-ILS/src/support-scripts/elastic-index.pl

index 8e19383..8e15361 100644 (file)
@@ -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 = <MAPPING_FILE>;
+            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
index 626034e..754a84f 100755 (executable)
@@ -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) {