From 9186341dddd4577bfac970cfbd119e599d4e5be2 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 10 Jul 2026 12:11:55 +0100 Subject: [PATCH] Bug 31925: Make QueryBuilder reuse the shared thesaurus/008 lookup Koha::SearchEngine::Elasticsearch::QueryBuilder kept its own copy of the thesaurus-name to 008/11-code table used when building "thesaurus" search queries against authority records. The previous commit moved that same table to Koha::Authority as a shared lookup. This commit deletes QueryBuilder's duplicate and points $thesaurus_to_value at $Koha::Authority::MARC21_THESAURUS_TO_CONTROL_FIELD_008_11 instead, so there is only one place to update if the code list ever changes. Pure refactor: $thesaurus_to_value keeps its existing name and contents, and build_authorities_query_compat() is unchanged, so query behaviour is identical before and after. Sponsored-by: Heythrop Library Signed-off-by: Clemens Gresser Signed-off-by: Phil Ringnalda Signed-off-by: David Cook Signed-off-by: Pedro Amorim --- .../Elasticsearch/QueryBuilder.pm | 21 ++++--------------- .../SearchEngine/Elasticsearch/QueryBuilder.t | 20 +++++++++++++++++- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 427b041ebe..e1299d74dd 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -47,6 +47,7 @@ use URI::Escape qw( uri_escape_utf8 ); use C4::Context; use Koha::Exceptions; +use Koha::Authority; use Koha::Caches; use Koha::AuthorisedValueCategories; @@ -608,23 +609,9 @@ our $koha_to_index_name = { all => '' }; -# Note that sears and aat map to 008/11 values here -# but don't appear in C4/Heading/MARC21 thesaurus -# because they don't have values in controlled field indicators -# https://www.loc.gov/marc/authority/ad008.html -our $thesaurus_to_value = { - lcsh => 'a', - lcac => 'b', - mesh => 'c', - nal => 'd', - notapplicable => 'n', - cash => 'k', - rvm => 'v', - aat => 'r', - sears => 's', - notdefined => 'z', - notspecified => '|' -}; +# Delegates to Koha::Authority's table so there is a single source of +# truth for thesaurus-name -> 008/11-code (bug 31925). +our $thesaurus_to_value = $Koha::Authority::MARC21_THESAURUS_TO_CONTROL_FIELD_008_11; sub build_authorities_query_compat { my ( diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index d7b9528145..47ca31b883 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -85,7 +85,7 @@ $se->mock( subtest 'build_authorities_query_compat() tests' => sub { - plan tests => 81; + plan tests => 83; my $qb; @@ -249,6 +249,24 @@ subtest 'build_authorities_query_compat() tests' => sub { "If mapping found for marclist the index is passed through converted" ); + # Thesaurus value mapping delegates to Koha::Authority (bug 31925) + $query = $qb->build_authorities_query_compat( + ['thesaurus'], undef, undef, ['contains'], ['lcsh'], 'AUTH_TYPE', + 'asc' + ); + is( + $query->{query}->{bool}->{must}[0]->{query_string}->{query}, 'a*', + "thesaurus value 'lcsh' is still translated to 008/11 code 'a'" + ); + $query = $qb->build_authorities_query_compat( + ['thesaurus'], undef, undef, ['contains'], ['mesh'], 'AUTH_TYPE', + 'asc' + ); + is( + $query->{query}->{bool}->{must}[0]->{query_string}->{query}, 'c*', + "thesaurus value 'mesh' is still translated to 008/11 code 'c'" + ); + }; subtest 'build_query tests' => sub {