From e7048d01d94c51bbf2ac7c3424599933382a1f38 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 12 Dec 2025 13:18:35 +0000 Subject: [PATCH] Bug 41287: Add option to use simple Stringwise sorting for facets This patch restores the previous perl sorting to restore speed. For English language libraries this will be correct in most instances, and is much more performant. To test: 1 - Apply patch, update database 2 - Perform some searches, note ordering of facets 3 - Update system preference 'FacetOrder' to 'simple alphabetical' 4 - Peform some searches, note ordering of facets, should be correct unless you have unicode characters 5 - Sign off NOTE: Benchmarks attached as a comment, you can also enable NYTProf and view results for yourself: https://wiki.koha-community.org/wiki/Profiling_with_Devel::NYTProf#NYTProf_with_Plack --- C4/Search.pm | 3 +++ Koha/SearchEngine/Elasticsearch/Search.pm | 2 ++ installer/data/mysql/mandatory/sysprefs.sql | 2 +- .../prog/en/modules/admin/preferences/searching.pref | 5 +++-- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 23dd75db4ff..1ec3f34b022 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -587,6 +587,9 @@ sub getRecords { $f->{facets} = $sorted_facets; } } + if ( C4::Context->preference('FacetOrder') eq 'Stringwise' ) { + @{ $f->{facets} } = sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @{ $f->{facets} }; + } } } diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index b0356ec8a52..ccf502c96c0 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -575,6 +575,8 @@ sub _convert_facets { if ($sorted_facets) { $facet->{facets} = $sorted_facets; } + } elsif ( C4::Context->preference('FacetOrder') eq 'Stringwise' ) { + @{ $facet->{facets} } = sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @{ $facet->{facets} }; } push @facets, $facet if exists $facet->{facets}; } diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 35a5d15dd8f..734150ba931 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -281,7 +281,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('ExtendedPatronAttributes','1',NULL,'Use extended patron IDs and attributes','YesNo'), ('FacetLabelTruncationLength','20',NULL,'Specify the facet max length in OPAC','Integer'), ('FacetMaxCount','20',NULL,'Specify the max facet count for each category','Integer'), -('FacetOrder','Alphabetical','Alphabetical|Usage','Specify the order of facets within each category','Choice'), +('FacetOrder','Alphabetical','Alphabetical|Usage|Stringwise','Specify the order of facets within each category','Choice'), ('FacetSortingLocale','default','','Choose the locale for sorting facet names when FacetOrder is set to Alphabetical. This enables proper Unicode-aware sorting of accented characters and locale-specific alphabetical ordering.','Choice'), ('FailedLoginAttempts','','','Number of login attempts before lockout the patron account','Integer'), ('FallbackToSMSIfNoEmail', 0, 'Enable|Disable', 'Send messages by SMS if no patron email is defined', 'YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref index 6c7e8d0e78a..4c586b7621f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref @@ -301,10 +301,11 @@ Searching: - pref: FacetOrder type: choice choices: - Alphabetical: "alphabetically" + Alphabetical: "alphabetically using locale" Usage: "by usage count" + Stringwise: "simple alphabetical" default: Alphabetical - - for each category. + - for each category. Locale can be set using the FacetSortingLocale preference. - - By default, show - pref: OPACnumSearchResults -- 2.39.5