From a2335a527a7f9f1572a5d25914fc0642761adb63 Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Mon, 27 May 2024 09:13:55 +0200 Subject: [PATCH] Bug 36884 : Make the record source a searchable field by the use of ElasticSearch --- Koha/Filter/MARC/EmbedRecordSource.pm | 105 ++++++++++++++++++++++++++ Koha/SearchEngine/Elasticsearch.pm | 23 ++++++ misc/migration_tools/rebuild_zebra.pl | 1 + 3 files changed, 129 insertions(+) create mode 100644 Koha/Filter/MARC/EmbedRecordSource.pm diff --git a/Koha/Filter/MARC/EmbedRecordSource.pm b/Koha/Filter/MARC/EmbedRecordSource.pm new file mode 100644 index 00000000000..67b09ce32eb --- /dev/null +++ b/Koha/Filter/MARC/EmbedRecordSource.pm @@ -0,0 +1,105 @@ +package Koha::Filter::MARC::EmbedRecordSource; + +# Copyright 2024 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +=head1 NAME + +Koha::Filter::MARC::EmbedRecordSource - embeds record source into MARC for indexing + +=head1 SYNOPSIS + + +=head1 DESCRIPTION + +Filter to embed record source into MARC records. + +=cut + +use strict; +use warnings; +use Koha::Biblios; + +use base qw(Koha::RecordProcessor::Base); +our $NAME = 'EmbedRecordSource'; + +=head2 filter + + my $newrecord = $filter->filter($record); + my $newrecords = $filter->filter(\@records); + +Embed see from headings into the specified record(s) and return the result. +In order to differentiate added headings from actual headings, a 'z' is +put in the first indicator. + +=cut +sub filter { + my $self = shift; + my $record = shift; + my $newrecord; + + return unless defined $record; + + if (ref $record eq 'ARRAY') { + my @recarray; + foreach my $thisrec (@$record) { + push @recarray, $self->_processrecord($thisrec); + } + $newrecord = \@recarray; + } elsif (ref $record eq 'MARC::Record') { + $newrecord = $self->_processrecord($record); + } + + return $newrecord; +} + +sub _processrecord { + my ($self, $record) = @_; + + $record->append_fields($self->fields($record)); + + return $record; +} + +=head2 fields + + my @fields = $filter->fields($record); + +Retrieve the fields that would be embedded if the record were processed by the filter. +Used during Elasticsearch indexing to give special treatment to these field (i.e. don't +include in facets, sorting, or suggestion fields) + +=cut +sub fields { + my ($self, $record) = @_; + my @newfield; + + my $biblionumber = $record->field('999')->subfield('c'); + return @newfield unless $biblionumber; + + my $biblio = Koha::Biblios->find($biblionumber); + return @newfield unless $biblio; + + my $source = $biblio->metadata->record_source; + return @newfield unless $source; + + my $marc_source = MARC::Field->new('942', 'z', ' ', 'z' => $source->name ); + push @newfield, $marc_source if (scalar($marc_source->subfields()) > 0); + return @newfield; +} + +1; diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 531d410e5b7..7e664d1961d 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -25,6 +25,7 @@ use Koha::Database; use Koha::Exceptions::Config; use Koha::Exceptions::Elasticsearch; use Koha::Filter::MARC::EmbedSeeFromHeadings; +use Koha::Filter::MARC::EmbedRecordSource; use Koha::SearchFields; use Koha::SearchMarcMaps; use Koha::Caches; @@ -664,6 +665,28 @@ sub marc_records_to_documents { } } + if ( Koha::RecordSources->count() and $self->index eq 'biblios') { + foreach my $field (Koha::Filter::MARC::EmbedRecordSource->new->fields($record)) { + my $data_field_rules = $data_fields_rules->{$field->tag()}; + if ($data_field_rules) { + my $subfields_mappings = $data_field_rules->{subfields}; + foreach my $subfield ($field->subfields()) { + my ($code, $data) = @{$subfield}; + my @mappings; + push @mappings, @{ $subfields_mappings->{$code} } if $subfields_mappings->{$code}; + if (@mappings) { + $self->_process_mappings(\@mappings, $data, $record_document, { + data_source => 'subfield', + code => $code, + field => $field + } + ); + } + } + } + } + } + if (C4::Context->preference('IncludeSeeFromInSearches') and $self->index eq 'biblios') { foreach my $field (Koha::Filter::MARC::EmbedSeeFromHeadings->new->fields($record)) { my $data_field_rules = $data_fields_rules->{$field->tag()}; diff --git a/misc/migration_tools/rebuild_zebra.pl b/misc/migration_tools/rebuild_zebra.pl index ec19ce1f5c4..3aada14bd5e 100755 --- a/misc/migration_tools/rebuild_zebra.pl +++ b/misc/migration_tools/rebuild_zebra.pl @@ -661,6 +661,7 @@ sub get_corrected_marc_record { my @filters; push @filters, 'EmbedItemsAvailability'; + push @filters, 'EmbedRecordSource'; push @filters, 'EmbedSeeFromHeadings' if C4::Context->preference('IncludeSeeFromInSearches'); -- 2.39.2