Bug 41795

Summary: UNIMARC: a Zebra search for Corporate Body Name authorities will also return Collective Titles
Product: Koha Reporter: Andreas Roussos <a.roussos>
Component: Searching - ZebraAssignee: Andreas Roussos <a.roussos>
Status: Needs Signoff --- QA Contact: Testopia <testopia>
Severity: normal    
Priority: P5 - low    
Version: Main   
Hardware: All   
OS: All   
GIT URL: Initiative type: ---
Sponsorship status: --- Comma delimited list of Sponsors:
Crowdfunding goal: 0 Patch complexity: Small patch
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:
Attachments: Browser screenshot depicting the problem when searching
Sample MARC record containing a single Collective Title authority
Bug 41795: Include the completeness attribute when matching on authtype
Bug 41795: Fix failing test in UNIMARC instances
Bug 41795: Fix failing test in UNIMARC instances

Description Andreas Roussos 2026-02-09 00:02:04 UTC
As per the Bug Summary, when you search for Corporate Body
Name authorities (authtypecode 'CO') in the OPAC or in the
Staff interface, the results will also include Collective
Titles (authtypecode 'CO_UNI_TI').

The problem manifests in UNIMARC instances that use the
default authority type codes and a Zebra search backend.
AIUI, it has to do with 'Corporate Body Name' authorities
using an authtypecode ('CO') that just so happens to be
the prefix of another authtypecode ('CO_UNI_TI') used by
'Collective Title' authorities.

Out of the box, MARC21 instances aren't affected by this.
However, in the (unlikely?) scenario that you've decided
to catalog authorities under different authtypes than the
built-in ones and somehow managed to add new authority
types that share the same prefix ('AUTH', 'AUTH_CORP',
'AUTH_PERS', etc.), then you will run into problems when
searching as there will be no guarantee that filtering
for the specific authority type 'AUTH' will return only
the results you will be expecting.

Elasticsearch is not affected -- it's a Zebra-only thing.
Comment 1 Andreas Roussos 2026-02-09 00:03:20 UTC
Created attachment 192687 [details]
Browser screenshot depicting the problem when searching
Comment 2 Andreas Roussos 2026-02-09 00:05:11 UTC
Created attachment 192688 [details]
Sample MARC record containing a single Collective Title authority

This is meant to be used in the forthcoming Test Plan.
Comment 3 Andreas Roussos 2026-02-09 02:06:48 UTC
Created attachment 192689 [details] [review]
Bug 41795: Include the completeness attribute when matching on authtype

UNIMARC instances include the following two authority types
by default (among others): Corporate Body Name (code: 'CO')
and Collective Title (code: 'CO_UNI_TI').

The problem is that when searching for Corporate Body Name
authorities (in the OPAC or in the Staff interface), the
results will include Collective Title authorities as well.
It has to do with Corporate Body Name authorities using
an authtypecode (CO) that happens to be the prefix of the
authtypecode (CO_UNI_TI) used by Collective Titles.

NOTE: Elasticsearch is not affected by this.

The fix is to additionally pass the completeness attribute
to the RPN query sent to Zebra so that it will try to match
on what is literally found in the entire field's phrase
index (i.e. the value of UNIMARC 152$b), instead of performing
a partial match.

More information can be found at the official documentation:
https://software.indexdata.com/zebra/doc/querymodel-rpn.html#querymodel-bib1-completeness

Test plan (tailored for KTD):

0) Launch a UNIMARC Koha instance using Zebra:

   ktd --marcflavour unimarc --search-engine zebra up

1) Import the Sample MARC record attached to this Bug,
   containing a single Collective Title authority
   (adding one manually would take a long time due to
   the many mandatory subfields).

2) Perform a generic authority search (without entering
   anything in the search box) for all 'Corporate Body
   Name' authorities. Note the number of results returned
   (83), and also notice that in page 4 of the results you
   get a stray 'Collective Title' authority ("Selected
   works", the one you imported in Step 1).

3) Apply this patch (and `restart_all`).

4) Repeat the same search as in Step 2. This time, the
   number of results returned should be one less than
   before (82), and you should only see 'Corporate Body
   Name' authorities in the results.
Comment 4 Andreas Roussos 2026-02-09 02:06:50 UTC Comment hidden (obsolete)
Comment 5 Andreas Roussos 2026-02-09 06:32:36 UTC
Created attachment 192710 [details] [review]
Bug 41795: Fix failing test in UNIMARC instances

Previously, t/AuthoritiesMarc_MARC21.t would fail if
you tried to run it from within a UNIMARC instance.

This patch fixes that.

Test plan:

1) Inside a UNIMARC instance, run the test and
   notice the subtest failure:

   $ prove -v t/AuthoritiesMarc_MARC21.t

2) Apply this patch.

3) Re-run the test and notice that it now completes
   without any failures.