Bug 17600: Standardize our EXPORT_OK
[srvgit] / misc / search_tools / rebuild_elasticsearch.pl
index 6cff2ea..337ac19 100755 (executable)
@@ -31,6 +31,7 @@ B<rebuild_elasticsearch.pl>
 [B<-r|--reset>]
 [B<-a|--authorities>]
 [B<-b|--biblios>]
+[B<--desc>]
 [B<-bn|--bnumber>]
 [B<-ai|--authid>]
 [B<-p|--processes>]
@@ -70,6 +71,12 @@ specifying neither and so both get indexed.
 Index the biblios only. Combining this with B<-a> is the same as
 specifying neither and so both get indexed.
 
+=item B<--desc>
+
+Index the records in descending id order. Intended to index newer record before older records.
+Default is to index in ascending order.
+Does not work with --bnumber or --authid
+
 =item B<-bn|--bnumber>
 
 Only index the supplied biblionumber, mostly for testing purposes. May be
@@ -105,7 +112,7 @@ Full documentation.
 =cut
 
 use autodie;
-use Getopt::Long;
+use Getopt::Long qw( GetOptions );
 use Koha::Script;
 use C4::Context;
 use Koha::MetadataRecord::Authority;
@@ -113,15 +120,16 @@ use Koha::BiblioUtils;
 use Koha::SearchEngine::Elasticsearch;
 use Koha::SearchEngine::Elasticsearch::Indexer;
 use MARC::Field;
-use MARC::Record;
 use Modern::Perl;
-use Pod::Usage;
+use Pod::Usage qw( pod2usage );
+use Try::Tiny qw( catch try );
 
 my $verbose = 0;
 my $commit = 5000;
 my ($delete, $reset, $help, $man, $processes);
 my ($index_biblios, $index_authorities);
 my (@biblionumbers,@authids);
+my $desc;
 
 $|=1; # flushes output
 
@@ -131,8 +139,9 @@ GetOptions(
     'r|reset'       => \$reset,
     'a|authorities' => \$index_authorities,
     'b|biblios'     => \$index_biblios,
-    'bn|bnumber=i' => \@biblionumbers,
-    'ai|authid=i'  => \@authids,
+    'desc'          => \$desc,
+    'bn|bnumber=i'  => \@biblionumbers,
+    'ai|authid=i'   => \@authids,
     'p|processes=i' => \$processes,
     'v|verbose+'    => \$verbose,
     'h|help'        => \$help,
@@ -179,10 +188,15 @@ if ($slice_count > 1) {
     }
     # Fudge the commit count a bit to spread out the Elasticsearch commits
     $commit *= 1 + 0.10 * $slice_index;
+    $commit = int( $commit );
     _log(1, "Processing slice @{[$slice_index + 1]} of $slice_count\n");
     $iterator_options{slice} = { index => $slice_index, count => $slice_count };
 }
 
+if( $desc ){
+    $iterator_options{desc} = 1;
+}
+
 my $next;
 if ($index_biblios) {
     _log(1, "Indexing biblios\n");
@@ -288,12 +302,18 @@ sub _do_reindex {
         push @commit_buffer, $record;
         if ( !( --$commit_count ) ) {
             _log( 1, "Committing $commit records...\n" );
-            my $response = $indexer->update_index( \@id_buffer, \@commit_buffer );
-            _handle_response($response);
+            my $response;
+            try{
+                $response = $indexer->update_index( \@id_buffer, \@commit_buffer );
+                _handle_response($response);
+                _log( 1, "Commit complete\n" );
+            } catch {
+                _log(1,"Elasticsearch exception thrown: ".$_->type."\n");
+                _log(2,"Details: ".$_->details."\n");
+            };
             $commit_count  = $commit;
             @id_buffer     = ();
             @commit_buffer = ();
-            _log( 1, "Commit complete\n" );
         }
     }