Bug 23137: Add reset option to rebuild_elasticsearch.pl
authorNick Clemens <nick@bywatersolutions.com>
Thu, 19 Mar 2020 16:30:29 +0000 (16:30 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 29 Apr 2020 16:01:55 +0000 (17:01 +0100)
Setup:
1 - Be using Elasticsearch
2 - Reload mappings from the db
    Admin->Search configuration
    Reset Mappings
3 - Reindex ES and confirm searching is working

To test:
1 - Apply patch
2 - Alter your mappings file for elastic (just change a description for a field)
3 - perl misc/search_tools/rebuild_elasticsearch.pl -r -v
    Verbose not necessary, but good for letting you know things are progressing
4 - Confirm the mapping change shows in the interface
5 - Confirm reindex worked and searching is working

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

misc/search_tools/rebuild_elasticsearch.pl

index 4223f2d..5268d12 100755 (executable)
@@ -27,6 +27,13 @@ rebuild_elasticsearch.pl - inserts records from a Koha database into Elasticsear
 
 B<rebuild_elasticsearch.pl>
 [B<-c|--commit>=C<count>]
+[B<-d|--delete>]
+[B<-r|--reset>]
+[B<-a|--authorities>]
+[B<-b|--biblios>]
+[B<-bn|--bnumber>]
+[B<-ai|--authid>]
+[B<-p|--processes>]
 [B<-v|--verbose>]
 [B<-h|--help>]
 [B<--man>]
@@ -48,6 +55,11 @@ Higher should be faster, but will cause more RAM usage. Default is 5000.
 
 Delete the index and recreate it before indexing.
 
+=item B<-r|--reset>
+
+Reload mappings from files (specified in koha-conf.xml) before indexing.
+Implies --delete.
+
 =item B<-a|--authorities>
 
 Index the authorities only. Combining this with B<-b> is the same as
@@ -98,7 +110,9 @@ use Koha::Script;
 use C4::Context;
 use Koha::MetadataRecord::Authority;
 use Koha::BiblioUtils;
+use Koha::SearchEngine::Elasticsearch;
 use Koha::SearchEngine::Elasticsearch::Indexer;
+use Koha::Caches;
 use MARC::Field;
 use MARC::Record;
 use Modern::Perl;
@@ -106,7 +120,7 @@ use Pod::Usage;
 
 my $verbose = 0;
 my $commit = 5000;
-my ($delete, $help, $man, $processes);
+my ($delete, $reset, $help, $man, $processes);
 my ($index_biblios, $index_authorities);
 my (@biblionumbers,@authids);
 
@@ -115,6 +129,7 @@ $|=1; # flushes output
 GetOptions(
     'c|commit=i'    => \$commit,
     'd|delete'      => \$delete,
+    'r|reset'       => \$reset,
     'a|authorities' => \$index_authorities,
     'b|biblios'     => \$index_biblios,
     'bn|bnumber=i' => \@biblionumbers,
@@ -139,6 +154,14 @@ pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
 
 _sanity_check();
 
+if ($reset){
+    Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
+    my $cache = Koha::Caches->get_instance();
+    $cache->clear_from_cache('elasticsearch_search_fields_staff_client');
+    $cache->clear_from_cache('elasticsearch_search_fields_opac');
+    $delete = 1;
+}
+
 _verify_index_state($Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX, $delete) if ($index_biblios);
 _verify_index_state($Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX, $delete) if ($index_authorities);