From 6ac269f64a723cc9bb40b576c5d2e7763bea1be1 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 ++ .../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 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/atomicupdate/bug_41287.pl b/installer/data/mysql/atomicupdate/bug_41287.pl new file mode 100755 index 00000000000..a3e4b61a6be --- /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 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