From 118a372366fc9fef995b9c45f7bd621260c3d2aa 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 | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 0c144937ea3..3b78b9d73d0 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -823,8 +823,9 @@ sub marc_records_to_documents { if ($field =~ /__sort$/) { # 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 934ca679ece..9e42169d00a 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch.t @@ -187,7 +187,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', 'ISO2709'); @@ -469,7 +469,7 @@ subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' MARC::Field->new('008', '901111s19uu xxk|||| |00| ||eng c'), MARC::Field->new('100', '', '', a => 'Author 2'), # 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), @@ -648,6 +648,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(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value'); is_deeply($docs->[1]->{author}, ['Author 2'], 'Second document author field should be set correctly'); -- 2.48.1