From 9f97cbd3ad247062b492c4178a417e6047832e7a Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 11 Apr 2025 12:36:40 +0000
Subject: [PATCH] Bug 39503: Collapse thesaurus if value in 008_/11 = 'z' and
 040 $f

With bug 30280 and subsequent patches we attempt to respect the
thesaurus indication when linking with LinkerConsiderThesaurus on.
However, there are still cases when we link wrong.

This happens when thesaurus in bibliographic field 6XX is defined by
subfield $2. In such a case the authority record should have 008/11 = 'z'
and, according to https://www.loc.gov/marc/authority/ad008.html
(cf. comment for 008/11 = 'z': "A MARC code for the conventions used to
formulate the heading *may* be contained in subfield $f"), may or may have
thesaurus declared in 040 $f.

This is why we make a second search attempt in C4::Heading::_search,
but doing so we have to make sure that the resulting authority
record has not a different thesaurus declared in 040 $f. (Note
that 008/11 and 040 $f are indexed with  the same search
field: subject-heading-thesaurus. So we search for the
second time in the same search field but with
'notdefined' == 008/11='z' instead of the original
thesaurus taken from subfield $2 of the bibliographic
field 6XX.) This means we have to explicitly check the
content of 040 $f of the resulting authority record.

Test plan:
==========
1. Turn on the LinkerConsiderThesaurus systempreference.
2. Create an authority record (e.g. TOPIC_TERM) with 008/11 = 'z' and
   040 $f containing a thesaurus indication (e.g. 'kaba'), and a term
   in field 150 $a (e.g. Early music).
3. Edit a bibliographic record: in field 650 put 2nd indicator '7',
   the term 'Early music' in $a, and a different thesaurus code in $2
   (e.g. 'dbn'). Click the 'Link authorities automatically' button.
   Note that 650 'Early music' (dbn) has been linked with and authority
   record 'Early music' (kaba). This is wrong.
4. Apply the patch ; restart_all ; reindex ES for authorities with:
   koha-elasticsearch --rebuild -d -a kohadev
5. Repeat p. 3. Now the 650 'Early music' (dbn) has not been linked to
   an authority record (650 - No matching authority found.)

Signed-off-by: Janusz Kaczmarek <januszop@gmail.com>
Signed-off-by: Roman Dolny <roman.dolny@jezuici.pl>
---
 Koha/SearchEngine/Elasticsearch.pm | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm
index 945dcd9e7a..39f1d2cbdb 100644
--- a/Koha/SearchEngine/Elasticsearch.pm
+++ b/Koha/SearchEngine/Elasticsearch.pm
@@ -918,6 +918,16 @@ sub marc_records_to_documents {
             }
         }
 
+        if (   $self->index eq $AUTHORITIES_INDEX
+            && exists $record_document->{'subject-heading-thesaurus'}
+            && scalar @{ $record_document->{'subject-heading-thesaurus'} } > 1 )
+        {
+            # We should really only have two thesauri defined in the case where 008/_11 = 'z' and 040$f is defined
+            # In that case, we should drop the z
+            @{ $record_document->{'subject-heading-thesaurus'} } =
+                map { $_ eq 'z' ? () : $_ } @{ $record_document->{'subject-heading-thesaurus'} };
+        }
+
         # Check if there is at least one available item
         if ( $self->index eq $BIBLIOS_INDEX ) {
             my ( $tag, $code ) = C4::Biblio::GetMarcFromKohaField('biblio.biblionumber');
-- 
2.39.5