From 6e7d2b0d92b6ce5d63273b2202e177db74b9fc7d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 21 Mar 2024 16:48:56 +0100 Subject: [PATCH] Bug 36396: Link facet with authorised value category - ES This new feature adds the ability to link an ES facet to an authorised value category, in order to display a description instead of the code. Test plan: 1. Go to the ES mapping admin page 2. Notice the new "Authorized value category" column in the facets config table 3. Create a new facet that is linked to a search field containing values from an authorised value category. Pick this category in the list 4. Save 5. Reindex 6. Notice that the descriptions are now displayed (staff and OPAC) in the search result page. QA: See the related bug report for possible improvements. It's not easy at all to deal with the "specials" here, a lot of changes must be done in how we fetch/cache the AVs. Sponsored-by: The Research University in the Helmholtz Association (KIT) Signed-off-by: Lukas Koszyk --- Koha/SearchEngine/Elasticsearch/Search.pm | 10 +++++++- admin/searchengine/elasticsearch/mappings.pl | 5 ++++ .../intranet-tmpl/prog/en/includes/facets.inc | 3 +-- .../searchengine/elasticsearch/mappings.tt | 24 +++++++++++++++++++ .../prog/js/elasticsearch-mappings.js | 3 ++- 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index 4a26918b66..9760c65b57 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -441,7 +441,7 @@ sub _convert_facets { return if !$es; - my %type_to_label = map { $_->name => { order => $_->facet_order, label => $_->label } } + my %type_to_label = map { $_->name => { order => $_->facet_order, av_cat => $_->authorised_value_category, label => $_->label } } Koha::SearchEngine::Elasticsearch->get_facet_fields; # We also have some special cases, e.g. itypes that need to show the @@ -470,7 +470,12 @@ sub _convert_facets { label => $type_to_label{$type}{label}, type_link_value => $type, order => $type_to_label{$type}{order}, + av_cat => $type_to_label{$type}{av_cat}, }; + my %authorised_values; + if ( $type_to_label{$type}{av_cat} ) { + %authorised_values = map {$_->{authorised_value} => $_->{lib}} @{C4::Koha::GetAuthorisedValues($type_to_label{$type}{av_cat}, $opac)}; + } $limit = @{ $data->{buckets} } if ( $limit > @{ $data->{buckets} } ); foreach my $term ( @{ $data->{buckets} }[ 0 .. $limit - 1 ] ) { my $t = $term->{key}; @@ -480,6 +485,9 @@ sub _convert_facets { if ( exists( $special{$type} ) ) { $label = $special{$type}->{$t} // $t; } + elsif ( $type_to_label{$type}{av_cat} ) { + $label = $authorised_values{$t}; + } else { $label = $t; } diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl index 735baf16f7..c7f2e6ff02 100755 --- a/admin/searchengine/elasticsearch/mappings.pl +++ b/admin/searchengine/elasticsearch/mappings.pl @@ -28,6 +28,7 @@ use Koha::SearchEngine::Elasticsearch::QueryBuilder; use Koha::SearchMarcMaps; use Koha::SearchFields; use Koha::Caches; +use Koha::AuthorisedValueCategories; use Try::Tiny qw( catch try ); use Module::Load::Conditional qw( can_load ); @@ -118,6 +119,7 @@ if ( $op eq 'cud-edit' ) { my $field_weight = $field_weight[$i]; my $field_staff_client = $field_staff_client[$i]; my $field_opac = $field_opac[$i]; + my $av_category = $input->param('facet_av_cat_' . $field_name); my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, @@ -139,6 +141,7 @@ if ( $op eq 'cud-edit' ) { my $facet_order = first { $faceted_field_names[$_] eq $field_name } 0 .. $#faceted_field_names; $search_field->facet_order(defined $facet_order ? $facet_order + 1 : undef); + $search_field->authorised_value_category($av_category); $search_field->store; } @@ -294,12 +297,14 @@ while ( my $search_field = $search_fields->next ) { push @all_search_fields, $search_field_unblessed; } +my @authorised_value_categories = Koha::AuthorisedValueCategories->search->get_column('category_name'); push @messages, @errors; $template->param( indexes => \@indexes, all_search_fields => \@all_search_fields, facetable_fields => \@facetable_fields, messages => \@messages, + authorised_value_categories => \@authorised_value_categories, ); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc index 6bb127fa72..fa6c6def47 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc @@ -88,8 +88,7 @@ [% FOREACH facet IN facets_loo.facets %] [% IF facets_loo.type_label_CollectionCodes %] [% SET facet.facet_label_value = AuthorisedValues.GetDescriptionByKohaField( kohafield => 'items.ccode', authorised_value => facet.facet_label_value ) || facet.facet_label_value %] - [% END %] - [% IF facets_loo.type_label_Language %] + [% ELSIF facets_loo.type_label_Language %] [% SET facet.facet_label_value = Languages.GetByISOCode(lang,facet.facet_label_value) || facet.facet_label_value %] [% END %] [% IF loop.count > 5 && !facet.active %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt index ce884f8915..12cfbb9f73 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt @@ -1,5 +1,6 @@ [% USE raw %] [% USE Asset %] +[% USE AuthorisedValues %] [% SET footerjs = 1 %] [% PROCESS 'i18n.inc' %] [% INCLUDE 'doc-head-open.inc' %] @@ -493,6 +494,7 @@ a.add, a.delete { Order Search field Label + Authorized value category   @@ -519,6 +521,17 @@ a.add, a.delete { [% END %] + + + Delete @@ -536,6 +549,17 @@ a.add, a.delete { + + + Add diff --git a/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js b/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js index 23cabf0bb6..dd4aa2e578 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js +++ b/koha-tmpl/intranet-tmpl/prog/js/elasticsearch-mappings.js @@ -177,7 +177,8 @@ $(document).ready(function () { if (search_field_name.length > 0) { const next_id = Math.max.apply(null, dt_data.map(row => row[0])) + 1; const label = selected_option.data('label'); - new_line = [next_id, search_field_name, '%s'.format(label.escapeHtml(), search_field_name.escapeHtml()), build_delete_link()] + const av_cat_select = $(clone_line(line).find('td')[2]).find('select').attr({name: 'facet_av_cat_%s'.format(search_field_name.escapeHtml())}); + new_line = [next_id, search_field_name, '%s'.format(label.escapeHtml(), search_field_name.escapeHtml()), av_cat_select[0].outerHTML, build_delete_link()] dt.row.add(new_line).draw(); clean_line(line); -- 2.30.2