From da801efee3e0d90b94712e5651c0021a95c8fd8e Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 2 May 2019 10:21:36 +0000
Subject: [PATCH] Bug 22826: Allow indexing of individual authorities in
 Elasticsearch

To test:
1 - perl misc/search_tools/rebuild_elastic_search.pl -h
2 - Note it indicates the bn option can be passed to index individual authids
3 - perl misc/search_tools/rebuild_elastic_search.pl -a -bn 92 -v
4 - Note the error
5 - Apply patch
6 - perl misc/search_tools/rebuild_elastic_search.pl -h
7 - Note new option ai|authid for indexing individual authids
8 - Note updated text for bn|biblionumber option
9 - perl misc/search_tools/rebuild_elastic_search.pl -a -bn 92 -v
10 - No errors, but no records indexed
11 - perl misc/search_tools/rebuild_elastic_search.pl -a -ai 92 -v
12 - 1 record indexed
13 - perl misc/search_tools/rebuild_elastic_search.pl -ai 92 -bn 92 -v
14 - 1 authority record and 1 biblio record indexed

Signed-off-by: Michal Denar <black23@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 misc/search_tools/rebuild_elasticsearch.pl | 29 +++++++++++++---------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/misc/search_tools/rebuild_elasticsearch.pl b/misc/search_tools/rebuild_elasticsearch.pl
index 1199b63922..b5237cb1a4 100755
--- a/misc/search_tools/rebuild_elasticsearch.pl
+++ b/misc/search_tools/rebuild_elasticsearch.pl
@@ -61,8 +61,12 @@ specifying neither and so both get indexed.
 =item B<-bn|--bnumber>
 
 Only index the supplied biblionumber, mostly for testing purposes. May be
-repeated. This also applies to authorities via authid, so if you're using it,
-you probably only want to do one or the other at a time.
+repeated.
+
+=item B<-ai|--authid>
+
+Only index the supplied authority id, mostly for testing purposes. May be
+repeated.
 
 =item B<-p|--processes>
 
@@ -104,7 +108,7 @@ my $verbose = 0;
 my $commit = 5000;
 my ($delete, $help, $man, $processes);
 my ($index_biblios, $index_authorities);
-my (@record_numbers);
+my (@biblionumbers,@authids);
 
 $|=1; # flushes output
 
@@ -113,7 +117,8 @@ GetOptions(
     'd|delete'      => \$delete,
     'a|authorities' => \$index_authorities,
     'b|biblios'     => \$index_biblios,
-    'bn|bnumber=i'  => \@record_numbers,
+    'bn|bnumber=i' => \@biblionumbers,
+    'ai|authid=i'  => \@authids,
     'p|processes=i' => \$processes,
     'v|verbose+'    => \$verbose,
     'h|help'        => \$help,
@@ -125,8 +130,8 @@ unless ($index_authorities || $index_biblios) {
     $index_authorities = $index_biblios = 1;
 }
 
-if ($processes && @record_numbers) {
-    die "Argument p|processes cannot be combined with bn|bnumber";
+if ($processes && ( @biblionumbers || @authids) ) {
+    die "Argument p|processes cannot be combined with bn|bnumber or ai|authid";
 }
 
 pod2usage(1) if $help;
@@ -162,9 +167,9 @@ if ($slice_count > 1) {
 my $next;
 if ($index_biblios) {
     _log(1, "Indexing biblios\n");
-    if (@record_numbers) {
+    if (@biblionumbers) {
         $next = sub {
-            my $r = shift @record_numbers;
+            my $r = shift @biblionumbers;
             return () unless defined $r;
             return ($r, Koha::BiblioUtils->get_from_biblionumber($r, item_data => 1 ));
         };
@@ -178,12 +183,12 @@ if ($index_biblios) {
 }
 if ($index_authorities) {
     _log(1, "Indexing authorities\n");
-    if (@record_numbers) {
+    if (@authids) {
         $next = sub {
-            my $r = shift @record_numbers;
+            my $r = shift @authids;
             return () unless defined $r;
             my $a = Koha::MetadataRecord::Authority->get_from_authid($r);
-            return ($r, $a->record);
+            return ($r, $a);
         };
     } else {
         my $records = Koha::MetadataRecord::Authority->get_all_authorities_iterator(%iterator_options);
@@ -248,7 +253,7 @@ sub _do_reindex {
     my $commit_count = $commit;
     my ( @id_buffer, @commit_buffer );
     while ( my $record = $next->() ) {
-        my $id     = $record->id;
+        my $id     = $record->id // $record->authid;
         my $record = $record->record;
         $count++;
         if ( $verbose == 1 ) {
-- 
2.20.1