From 4940a337fa23afe328213069ded1cedc4e46ff78 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 27 May 2024 23:37:01 +0000 Subject: [PATCH] Bug 36947: [Alternate 2] Do a locale-based sort for ES facet names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change uses a configurable locale-based collator to sort the ES facet names. Test plan: 0. Apply the patch 1. vi "/etc/default/koha-common" 2. Add the following to the bottom of the file: export LC_ALL=fi_FI.UTF-8 3. koha-plack --restart koha-common 4. Setup some test records with authors with accented and unaccented names, and different cases for the lead letter e.g. Aa author, Åa author2, aa author3, étienne 5. Switch to using Elasticsearch and reindex koha-elasticsearch -b -v --rebuild kohadev 6. Do a test search e.g. http://localhost:8081/cgi-bin/koha/catalogue/search.pl?q=test 7. Confirm the facet names are sorted in ascending order following Finnish collation rules e.g. Aa author aa author3 étienne Farley, David Humble, Jez Martin, Robert C. Åa author NOTE: Any collation and language can be used. Finnish is just an example of a Latin-based script which has a different alphabetical ordering than just A-Z Signed-off-by: Lari Strand --- Koha/SearchEngine/Elasticsearch/Search.pm | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index 54e6e644dc..3b428e3fbe 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -54,6 +54,9 @@ use MARC::File::XML; use MIME::Base64 qw( decode_base64 ); use JSON; +use POSIX qw(setlocale LC_COLLATE); +use Unicode::Collate::Locale; + Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store )); =head2 search @@ -502,8 +505,17 @@ sub _convert_facets { }; } if( C4::Context->preference('FacetOrder') eq 'Alphabetical' ){ - @{ $facet->{facets} } = - sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @{ $facet->{facets} }; + #Fetch locale as understood by Perl + #NOTE: Technically, we can define locale however we like, but this + #is a good default + my $locale = setlocale(LC_COLLATE); + + #Create locale-based collator + my $collator = Unicode::Collate::Locale->new( locale => $locale ); + if ($collator) { + @{ $facet->{facets} } = + sort { $collator->cmp( $a->{facet_label_value}, $b->{facet_label_value} ) } @{ $facet->{facets} }; + } } push @facets, $facet if exists $facet->{facets}; } -- 2.34.1