From b818a61c2e5c601f526a8d01ff1a4b9e506f0297 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 Signed-off-by: David Nind --- C4/Search.pm | 3 ++ Koha/SearchEngine/Elasticsearch/Search.pm | 2 ++ .../data/mysql/atomicupdate/bug_41287.pl | 29 +++++++++++++++++++ installer/data/mysql/mandatory/sysprefs.sql | 2 +- .../modules/admin/preferences/searching.pref | 5 ++-- 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_41287.pl diff --git a/C4/Search.pm b/C4/Search.pm index 5d6f3c6e33..df3a0ab592 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -593,6 +593,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 b0356ec8a5..ccf502c96c 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/atomicupdate/bug_41287.pl b/installer/data/mysql/atomicupdate/bug_41287.pl new file mode 100755 index 0000000000..a3e4b61a6b --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_41287.pl @@ -0,0 +1,29 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_success say_info); + +return { + bug_number => "41287", + description => "Add option for stringwise sorting of facets", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + # Do you stuffs here + $dbh->do( + q{ + INSERT IGNORE INTO systempreferences + ( variable, value, options, explanation, type ) VALUES + ('FacetOrder','Alphabetical','Alphabetical|Usage|Stringwise','Specify the order of facets within each category','Choice') + } + ); + $dbh->do( + q{ + UPDATE systempreferences + SET options = 'Alphabetical|Usage|Stringwise' + WHERE variable = 'FacetOrder' + } + ); + + say $out "Added new 'Stringwise' option to system preference 'FacetOrder'"; + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 35a5d15dd8..734150ba93 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 6c7e8d0e78..a8498f8ff4 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