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 <lukasz.koszyk@kit.edu>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Jonathan Druart 2024-03-21 16:48:56 +01:00 committed by Katrin Fischer
parent 58d097d2aa
commit 901f550a47
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
5 changed files with 41 additions and 4 deletions

View File

@ -442,7 +442,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
@ -471,7 +471,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};
@ -481,6 +486,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;
}

View File

@ -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(
{
@ -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;
}
@ -297,12 +300,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;

View File

@ -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 %]

View File

@ -1,5 +1,6 @@
[% USE raw %]
[% USE Asset %]
[% USE AuthorisedValues %]
[% SET footerjs = 1 %]
[% PROCESS 'i18n.inc' %]
[% INCLUDE 'doc-head-open.inc' %]
@ -498,6 +499,7 @@ a.add, a.delete {
<th>Order</th>
<th class="NoSort">Search field</th>
<th class="NoSort">Label</th>
<th class="NoSort">Authorized value category</th>
<th class="NoSort">&nbsp;</th>
</tr>
</thead>
@ -524,6 +526,17 @@ a.add, a.delete {
[% END %]
<input type="hidden" name="facet_name" value="[% f.name | html %]" />
</td>
<td>
<select name="facet_av_cat_[% f.name | html %]">
[% FOR av_cat IN authorised_value_categories %]
[% IF f.authorised_value_category == av_cat %]
<option value="[% av_cat | html %]" selected="selected">[% av_cat | html %]</option>
[% ELSE %]
<option value="[% av_cat | html %]">[% av_cat | html %]</option>
[% END %]
[% END %]
</select>
</td>
<td>
<a class="btn btn-default btn-xs delete-facet" style="cursor: pointer;"><i class="fa fa-trash"></i> Delete</a>
</td>
@ -541,6 +554,17 @@ a.add, a.delete {
</select>
</td>
<td></td>
<td>
<select data-id="facet-av-cat">
[% FOR av_cat IN authorised_value_categories %]
[% IF f.authorised_value_category == av_cat %]
<option value="[% av_cat | html %]" selected="selected">[% av_cat | html %]</option>
[% ELSE %]
<option value="[% av_cat | html %]">[% av_cat | html %]</option>
[% END %]
[% END %]
</select>
</td>
<td><a class="btn btn-default btn-xs add-facet"><i class="fa fa-plus"></i> Add</a></td>
</tr>
</tfoot>

View File

@ -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, '<span>%s</span><input type="hidden" name="facet_name" value="%s" />'.format(label.escapeHtml(), search_field_name.escapeHtml()), build_delete_link('delete-facet')]
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, '<span>%s</span><input type="hidden" name="facet_name" value="%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);