From 050710d78f03b8c615eb825756a9f3a1cc552f9d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 14 Mar 2024 11:41:42 +0100 Subject: [PATCH] Bug 35138: Make ES facetable fields configurable - POC Move the ES facet fields that are currently hardcoded in the codebase to a new ES_FACETS authorised value category. Librarians can then add/remove facet fields and modify their lib/description. Remaining work: 1. Adjust OPAC templates 2. AV is not ideal here, we should certainly have a dedicated DB table for that. It will bring more flexibility. We can image: * display or not (could be opac/staff/both/none) * define the size per field (number of facet to display) * order: move search_field.facet_order to this new table. In my testing this is not worker, they are displayed alpha (??) 3. Adjust the UI Still no translatability however. --- C4/Search.pm | 1 + Koha/SearchEngine/Elasticsearch.pm | 4 ++- .../Elasticsearch/QueryBuilder.pm | 33 ++++++++--------- Koha/SearchEngine/Elasticsearch/Search.pm | 27 +++++++------- .../data/mysql/atomicupdate/bug_35138.pl | 30 ++++++++++++++++ .../intranet-tmpl/prog/en/includes/facets.inc | 35 ++++++++----------- 6 files changed, 78 insertions(+), 52 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_35138.pl diff --git a/C4/Search.pm b/C4/Search.pm index 248856e42b2..ea70377373f 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -579,6 +579,7 @@ sub getRecords { "type_label_" . $facets_info->{$link_value}->{'label_value'} => 1, + label => $facets_info->{$link_value}->{'label_value'}, facets => \@this_facets_array, } unless ( diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 3f26cbc0274..601ab50d257 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -28,6 +28,7 @@ use Koha::Filter::MARC::EmbedSeeFromHeadings; use Koha::SearchFields; use Koha::SearchMarcMaps; use Koha::Caches; +use Koha::AuthorisedValueCategories; use C4::Heading; use C4::AuthoritiesMarc qw( GuessAuthTypeCode ); use C4::Biblio; @@ -1404,7 +1405,8 @@ sub get_facetable_fields { # These should correspond to the ES field names, as opposed to the CCL # things that zebra uses. - my @search_field_names = qw( author itype location su-geo title-series subject ccode holdingbranch homebranch ln ); + #my @search_field_names = qw( author itype location su-geo title-series subject ccode holdingbranch homebranch ln ); + my @search_field_names = Koha::AuthorisedValueCategories->find('ES_FACETS')->authorised_values->get_column('authorised_value'); my @faceted_fields = Koha::SearchFields->search( { name => { -in => \@search_field_names }, facet_order => { '!=' => undef } }, { order_by => ['facet_order'] } )->as_list; diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 35ab4b7f91d..224d1a245a4 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -48,6 +48,7 @@ use URI::Escape qw( uri_escape_utf8 ); use C4::Context; use Koha::Exceptions; use Koha::Caches; +use Koha::AuthorisedValueCategories; our %index_field_convert = ( 'kw' => '', @@ -225,26 +226,22 @@ sub build_query { # See _convert_facets in Search.pm for how these get turned into # things that Koha can use. my $size = C4::Context->preference('FacetMaxCount'); - $res->{aggregations} = { - author => { terms => { field => "author__facet" , size => $size } }, - subject => { terms => { field => "subject__facet", size => $size } }, - itype => { terms => { field => "itype__facet", size => $size} }, - location => { terms => { field => "location__facet", size => $size } }, - 'su-geo' => { terms => { field => "su-geo__facet", size => $size} }, - 'title-series' => { terms => { field => "title-series__facet", size => $size } }, - ccode => { terms => { field => "ccode__facet", size => $size } }, - ln => { terms => { field => "ln__facet", size => $size } }, + my @facets = Koha::AuthorisedValueCategories->find('ES_FACETS')->authorised_values->as_list; + for my $f ( @facets ) { + my $ff = $f->authorised_value; + $res->{aggregations}->{$ff} = { terms => { field => "${ff}__facet" , size => $size } }; }; - my $display_library_facets = C4::Context->preference('DisplayLibraryFacets'); - if ( $display_library_facets eq 'both' - or $display_library_facets eq 'home' ) { - $res->{aggregations}{homebranch} = { terms => { field => "homebranch__facet", size => $size } }; - } - if ( $display_library_facets eq 'both' - or $display_library_facets eq 'holding' ) { - $res->{aggregations}{holdingbranch} = { terms => { field => "holdingbranch__facet", size => $size } }; - } + # FIXME We need a way to show/hide the facet individually + #my $display_library_facets = C4::Context->preference('DisplayLibraryFacets'); + #if ( $display_library_facets eq 'both' + # or $display_library_facets eq 'home' ) { + # $res->{aggregations}{homebranch} = { terms => { field => "homebranch__facet", size => $size } }; + #} + #if ( $display_library_facets eq 'both' + # or $display_library_facets eq 'holding' ) { + # $res->{aggregations}{holdingbranch} = { terms => { field => "holdingbranch__facet", size => $size } }; + #} return $res; } diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index 933b52f79dd..c0fe227d891 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -45,6 +45,7 @@ use C4::Context; use C4::AuthoritiesMarc; use Koha::ItemTypes; use Koha::AuthorisedValues; +use Koha::AuthorisedValueCategories; use Koha::SearchEngine::QueryBuilder; use Koha::SearchEngine::Search; use Koha::Exceptions::Elasticsearch; @@ -443,18 +444,19 @@ sub _convert_facets { # These should correspond to the ES field names, as opposed to the CCL # things that zebra uses. my %type_to_label; - my %label = ( - author => 'Authors', - itype => 'ItemTypes', - location => 'Location', - 'su-geo' => 'Places', - 'title-series' => 'Series', - subject => 'Topics', - ccode => 'CollectionCodes', - holdingbranch => 'HoldingLibrary', - homebranch => 'HomeLibrary', - ln => 'Language', - ); + #my %label = ( + # author => 'Authors', + # itype => 'ItemTypes', + # location => 'Location', + # 'su-geo' => 'Places', + # 'title-series' => 'Series', + # subject => 'Topics', + # ccode => 'CollectionCodes', + # holdingbranch => 'HoldingLibrary', + # homebranch => 'HomeLibrary', + # ln => 'Language', + #); + my %label = map {$_->authorised_value => $_->lib } Koha::AuthorisedValueCategories->find('ES_FACETS')->authorised_values->as_list; my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields; for my $f (@facetable_fields) { @@ -486,6 +488,7 @@ sub _convert_facets { my $facet = { type_id => $type . '_id', "type_label_$type_to_label{$type}{label}" => 1, + label => $type_to_label{$type}{label}, type_link_value => $type, order => $type_to_label{$type}{order}, }; diff --git a/installer/data/mysql/atomicupdate/bug_35138.pl b/installer/data/mysql/atomicupdate/bug_35138.pl new file mode 100755 index 00000000000..19f579d76d3 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_35138.pl @@ -0,0 +1,30 @@ +use Modern::Perl; + +return { + bug_number => "35138", + description => "Add ES_FACETS AV cat", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + $dbh->do(q{INSERT IGNORE INTO authorised_value_categories(category_name, is_system) VALUES('ES_FACETS', 1)}); + my $sth = $dbh->prepare( + q{INSERT IGNORE INTO authorised_values(category, authorised_value, lib) VALUES('ES_FACETS', ?, ?)}); + + my $facets = { + author => 'Authors', + itype => 'ItemTypes', + location => 'Location', + 'su-geo' => 'Places', + 'title-series' => 'Series', + subject => 'Topics', + ccode => 'CollectionCodes', + holdingbranch => 'HoldingLibrary', + homebranch => 'HomeLibrary', + ln => 'Language', + }; + while ( my ( $av, $lib ) = each %$facets ) { + $sth->execute( $av, $lib ); + } + }, +}; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc index 2fcce67e7d2..6bb127fa729 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc @@ -51,38 +51,31 @@ [% IF facets_loo.facets.size > 0 %]
  • [% facets_loo.type_label | html %] - [% IF facets_loo.type_label_Authors %] + [% SWITCH facets_loo.label %] + [% CASE 'Authors' %] Authors - [% END %] - [% IF facets_loo.type_label_Titles %] + [% CASE 'Titles' %] Titles - [% END %] - [% IF facets_loo.type_label_Topics %] + [% CASE 'Topics' %] Topics - [% END %] - [% IF facets_loo.type_label_Places %] + [% CASE 'Places' %] Places - [% END %] - [% IF facets_loo.type_label_Series %] + [% CASE 'Series' %] Series - [% END %] - [% IF facets_loo.type_label_ItemTypes %] + [% CASE 'ItemTypes' %] Item types - [% END %] - [% IF ( facets_loo.type_label_HomeLibrary ) %] + [% CASE 'HomeLibrary' %] Home libraries - [% END %] - [% IF ( facets_loo.type_label_HoldingLibrary ) %] + [% CASE 'HoldingLibrary' %] Holding libraries - [% END %] - [% IF facets_loo.type_label_Location %] + [% CASE 'Location' %] Locations - [% END %] - [% IF facets_loo.type_label_CollectionCodes %] + [% CASE 'CollectionCodes' %] Collections - [% END %] - [% IF facets_loo.type_label_Language %] + [% CASE 'Languages' %] Languages + [% CASE %] + [% facets_loo.label | html %] [% END %]