From 734a02cf823d55a1f14ae6da2f8e371e66cd810d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 6 Apr 2022 11:38:27 +0200 Subject: [PATCH] Bug 30465: Make BatchUpdateBiblio update the index in one request When using the batch record modification tool to modify several bibliographic records, we don't want to send one index request per biblio, we want to index them all on the fly after the records have been modified. Otherwise we will end up with one task per record, and records will be indexed in background. Test plan: Use the batch mod tool to modify bibliographic records and confirm the behaviour is correct. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Fridolin Somers --- C4/Biblio.pm | 14 ++++++++++---- Koha/BackgroundJob/BatchUpdateBiblio.pm | 8 +++++++- Koha/Items.pm | 11 ++++++----- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index da10581ede..de4b48ab47 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -357,7 +357,9 @@ Returns 1 on success 0 on failure sub ModBiblio { my ( $record, $biblionumber, $frameworkcode, $options ) = @_; + $options //= {}; + my $skip_record_index = $options->{skip_record_index} || 0; if (!$record) { carp 'No record passed to ModBiblio'; @@ -421,7 +423,7 @@ sub ModBiblio { _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); # update the MARC record (that now contains biblio and items) with the new record data - ModBiblioMarc( $record, $biblionumber ); + ModBiblioMarc( $record, $biblionumber, { skip_record_index => $skip_record_index } ); # modify the other koha tables _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); @@ -2871,12 +2873,14 @@ Function exported, but should NOT be used, unless you really know what you're do sub ModBiblioMarc { # pass the MARC::Record to this function, and it will create the records in # the marcxml field - my ( $record, $biblionumber ) = @_; + my ( $record, $biblionumber, $params ) = @_; if ( !$record ) { carp 'ModBiblioMarc passed an undefined record'; return; } + my $skip_record_index = $params->{skip_record_index} || 0; + # Clone record as it gets modified $record = $record->clone(); my $dbh = C4::Context->dbh; @@ -2936,8 +2940,10 @@ sub ModBiblioMarc { $m_rs->metadata( $record->as_xml_record($encoding) ); $m_rs->store; - my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); - $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" ); + unless ( $skip_record_index ) { + my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); + $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" ); + } return $biblionumber; } diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index e8022c0d49..35152e4526 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -20,6 +20,8 @@ use JSON qw( decode_json encode_json ); use Koha::DateUtils qw( dt_from_string ); use Koha::Virtualshelves; +use Koha::SearchEngine; +use Koha::SearchEngine::Indexer; use C4::Context; use C4::Biblio; @@ -89,7 +91,8 @@ sub process { C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record ); my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber ); C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, { - overlay_context => $args->{overlay_context}, + overlay_context => $args->{overlay_context}, + skip_record_index => 1, }); }; if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well @@ -110,6 +113,9 @@ sub process { $self->progress( ++$job_progress )->store; } + my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); + $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" ); + my $job_data = decode_json $self->data; $job_data->{messages} = \@messages; $job_data->{report} = $report; diff --git a/Koha/Items.pm b/Koha/Items.pm index d8bca20519..1900a40a0f 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -353,11 +353,12 @@ sub batch_update { Koha::Items->search( { itemnumber => \@modified_itemnumbers } ) ->get_column('biblionumber')); - my $indexer = Koha::SearchEngine::Indexer->new( - { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); - $indexer->index_records( \@biblionumbers, 'specialUpdate', - "biblioserver", undef ) - if @biblionumbers; + if ( @biblionumbers ) { + my $indexer = Koha::SearchEngine::Indexer->new( + { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); + + $indexer->update_index( \@biblionumbers ); + } } return ( { modified_itemnumbers => \@modified_itemnumbers, modified_fields => $modified_fields }, $self ); -- 2.39.5