From 44f39fd99b1e83c677eadf4c133ac0a37f7eaa29 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 21 Mar 2023 17:31:31 +0000 Subject: [PATCH] Bug 33277: Support custom thesaurus authority with no 040 specified --- C4/Heading.pm | 17 ++++++++++++++++- t/db_dependent/Heading.t | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/C4/Heading.pm b/C4/Heading.pm index 8b8ece8aac..3054fc5305 100644 --- a/C4/Heading.pm +++ b/C4/Heading.pm @@ -233,7 +233,22 @@ sub _search { \@value, $self->{'auth_type'}, 'AuthidAsc' ); - return $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata ); + + my ( $matched_auths, $total ) = $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata ); + # Some auth records may not contain the 040$f to specify their source + # This is legal, so we do a fallback search + if( !$total && $thesaurus && !( grep /$thesaurus/,('lcsh','lcac','mesh','nal','notspecified','cash','rvm','sears','aat') ) ){ + pop @value; + push @value, 'z'; + $search_query = $builder->build_authorities_query_compat( + \@marclist, \@and_or, \@excluding, \@operator, + \@value, $self->{'auth_type'}, + 'AuthidAsc' + ); + ( $matched_auths, $total ) = $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata ); + } + return ( $matched_auths, $total ); + } =head1 INTERNAL FUNCTIONS diff --git a/t/db_dependent/Heading.t b/t/db_dependent/Heading.t index c2a2de9c04..3dd209d8c5 100755 --- a/t/db_dependent/Heading.t +++ b/t/db_dependent/Heading.t @@ -65,7 +65,7 @@ subtest "UNIMARC tests" => sub { subtest "_search tests" => sub { - plan tests => 6; + plan tests => 7; t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch'); @@ -74,13 +74,13 @@ subtest "_search tests" => sub { $search->mock('search_auth_compat', sub { my $self = shift; my $search_query = shift; - return $search_query; + return ($search_query, 1 ); }); my $field = MARC::Field->new( '650', ' ', '0', a => 'Uncles', x => 'Fiction' ); my $heading = C4::Heading->new_from_field($field); - my $search_query = $heading->_search( 'match-heading' ); + my ($search_query) = $heading->_search( 'match-heading' ); my $terms = $search_query->{query}->{bool}->{must}; my $expected_terms = [ { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, @@ -90,7 +90,7 @@ subtest "_search tests" => sub { $field = MARC::Field->new( '650', ' ', '3', a => 'Uncles', x => 'Fiction' ); $heading = C4::Heading->new_from_field($field); - $search_query = $heading->_search( 'match-heading' ); + ($search_query) = $heading->_search( 'match-heading' ); $terms = $search_query->{query}->{bool}->{must}; $expected_terms = [ { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, @@ -100,7 +100,7 @@ subtest "_search tests" => sub { $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' ); $heading = C4::Heading->new_from_field($field); - $search_query = $heading->_search( 'match-heading' ); + ($search_query) = $heading->_search( 'match-heading' ); $terms = $search_query->{query}->{bool}->{must}; $expected_terms = [ { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, @@ -110,7 +110,7 @@ subtest "_search tests" => sub { $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,' ); $heading = C4::Heading->new_from_field($field); - $search_query = $heading->_search( 'match-heading' ); + ($search_query) = $heading->_search( 'match-heading' ); $terms = $search_query->{query}->{bool}->{must}; $expected_terms = [ { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } }, @@ -119,7 +119,7 @@ subtest "_search tests" => sub { $field = MARC::Field->new( '100', ' ', '', a => 'Yankovic, Al', d => '1959-,', e => '[author]' ); $heading = C4::Heading->new_from_field($field); - $search_query = $heading->_search( 'match-heading' ); + ($search_query) = $heading->_search( 'match-heading' ); $terms = $search_query->{query}->{bool}->{must}; $expected_terms = [ { term => { 'match-heading.ci_raw' => 'Yankovic, Al 1959' } }, @@ -128,11 +128,31 @@ subtest "_search tests" => sub { $field = MARC::Field->new( '100', ' ', '', a => 'Tolkien, J.R.R.,', e => '[author]' ); $heading = C4::Heading->new_from_field($field); - $search_query = $heading->_search( 'match-heading' ); + ($search_query) = $heading->_search( 'match-heading' ); $terms = $search_query->{query}->{bool}->{must}; $expected_terms = [ { term => { 'match-heading.ci_raw' => 'Tolkien, J.R.R' } }, ]; is_deeply( $terms, $expected_terms, "Search formed as expected for a non-subject field with double punctuation, period+comma "); + $search->mock('search_auth_compat', sub { + my $self = shift; + my $search_query = shift; + if( $search_query->{query}->{bool}->{must}[1]->{term}->{'subject-heading-thesaurus.ci_raw'} eq 'special_sauce' ){ + return; + } + return ($search_query, 1); + }); + + # Special case where thesaurus defined in subfield 2 should also match record with no thesaurus + $field = MARC::Field->new( '650', ' ', '7', a => 'Uncles', x => 'Fiction', 2 => 'special_sauce' ); + $heading = C4::Heading->new_from_field($field); + ($search_query) = $heading->_search( 'match-heading' ); + $terms = $search_query->{query}->{bool}->{must}; + $expected_terms = [ + { term => { 'match-heading.ci_raw' => 'Uncles generalsubdiv Fiction' } }, + { term => { 'subject-heading-thesaurus.ci_raw' => 'z' } }, + ]; + is_deeply( $terms, $expected_terms, "When thesaurus in subfield 2, and nothing is found, we should search again for 008_11 = z"); + }; -- 2.30.2