From 29fa444fad1bbbf7a66e6ec5379703d2739c3cb5 Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Thu, 6 Feb 2025 12:50:24 +0100 Subject: [PATCH] Bug 24720: Don't strip if contains only non-word characters --- Koha/SearchEngine/Elasticsearch.pm | 5 +++-- t/db_dependent/Koha/SearchEngine/Elasticsearch.t | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 09fe2f744af..6aa6bbc7088 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -890,8 +890,9 @@ sub marc_records_to_documents { # Make sure to keep the sort field length sensible. 255 was chosen as a nice round value. $record_document->{$field} = [ substr( join( ' ', @{ $record_document->{$field} } ), 0, 255 ) ]; - # Strip any initial non-word characters which don't make much sense to sort on - $record_document->{$field} = [ map { s/^\W+//u; $_ } @{$record_document->{$field}} ]; + # Strip any initial non-word characters which don't make much sense to sort on, + # leave as is if contains nothing but non-word characters + $record_document->{$field} = [ map { s/^\W+(?=\w)//u; $_ } @{$record_document->{$field}} ]; } } } diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch.t index 6d71d1ad7b8..83aabb07fdf 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch.t @@ -211,7 +211,7 @@ subtest 'get_elasticsearch_mappings() tests' => sub { subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub { - plan tests => 71; + plan tests => 72; t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); t::lib::Mocks::mock_preference( 'ElasticsearchMARCFormat', 'base64ISO2709' ); @@ -500,6 +500,7 @@ subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' # MARC::Field->new('210', '', '', a => 'Title 2'), # MARC::Field->new('245', '', '', a => 'Title: second record'), + MARC::Field->new( '246', '', '', a => '!!!' ), MARC::Field->new( '260', '', '', a => 'New York :', b => 'Ace ,', c => '1963-2003' ), MARC::Field->new( '999', '', '', c => '1234568' ), MARC::Field->new( '952', '', '', 0 => 1, g => 'string where should be numeric', o => $long_callno ), @@ -714,6 +715,12 @@ subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' # Second record: + is( + $docs->[1]->{title_sort__sort}[0], + '!!!', + 'Second document title_sort should have been left as is as only contains non word characters', + ); + is_deeply( $docs->[1]->{author}, ['Author 2'], 'Second document author field should be set correctly' ); is( -- 2.51.2