From 11667dc2bf12b47ca793a6df434dd6b4ed2db3d9 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. --- C4/Biblio.pm | 14 ++++++++++---- Koha/BackgroundJob/BatchUpdateBiblio.pm | 8 +++++++- Koha/Items.pm | 11 ++++++----- Koha/SearchEngine/Elasticsearch/Indexer.pm | 13 +++++++++++-- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index c4de6b064d..644a0a669f 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -351,7 +351,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'; @@ -415,7 +417,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 ); @@ -2852,12 +2854,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; @@ -2917,8 +2921,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..a204df49a0 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", undef, { index_now => 1 } ); + 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 ); diff --git a/Koha/SearchEngine/Elasticsearch/Indexer.pm b/Koha/SearchEngine/Elasticsearch/Indexer.pm index ca0d19a442..3788531ae6 100644 --- a/Koha/SearchEngine/Elasticsearch/Indexer.pm +++ b/Koha/SearchEngine/Elasticsearch/Indexer.pm @@ -315,14 +315,23 @@ at the moment. The other variables are used for parity with Zebra indexing calls. Currently the calls are passed through to Zebra as well. +=item C<$more_params> + +index_now can be set to force indexation on the fly. + +=back + =cut sub index_records { - my ( $self, $record_numbers, $op, $server, $records ) = @_; + my ( $self, $record_numbers, $op, $server, $records, $more_params ) = @_; $record_numbers = [$record_numbers] if ref $record_numbers ne 'ARRAY' && defined $record_numbers; $records = [$records] if ref $records ne 'ARRAY' && defined $records; + + my $index_now = $more_params->{index_now} || 0; + if ( $op eq 'specialUpdate' ) { - if ($records){ + if ( $records || $index_now ) { $self->update_index( $record_numbers, $records ); } else { $self->update_index_background( $record_numbers, $server ); -- 2.34.1