Bug 42107: Add search scores to staff results page

This patch ensures the scores from ES are passed through and added to the
search results in the staff side.

Initially hidden, the scores can be shown by entering the line below into the
JS console in developer tools in the browser, or added to IntranetUserJS to
make them always show:
$(".search_score").toggle();

To test:
1 - Have Koha using ES
2 - Do some searches, note results
3 - Apply patch, restart all
4 - Confirm results don't change
5 - Enter code from description into IntranetUserJS
6 - Do more searches
7 - Note the score is visible

To really note scores, you will need to add some weights to the ES config:
Set weights like:
title 32
author 16
subject 8

Signed-off-by: Andrew Auld <andrew.auld@openfifth.co.uk>
Signed-off-by: Thomas Klausner <domm@plix.at>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 925d5f50be)
Signed-off-by: Jacob O'Mara <Jacob.omara@openfifth.co.uk>
This commit is contained in:
Nick Clemens 2026-03-16 12:57:35 +00:00 committed by Jacob O'Mara
parent d9e17936d7
commit 80bc9a0a8c
No known key found for this signature in database
3 changed files with 9 additions and 2 deletions

View File

@ -174,13 +174,15 @@ sub search_compat {
# Convert each result into a MARC::Record
my @records;
my @scores;
# opac-search expects results to be put in the
# right place in the array, according to $offset
my $index = $offset;
my $hits = $results->{'hits'};
foreach my $es_record ( @{ $hits->{'hits'} } ) {
$records[ $index++ ] = $self->decode_record_from_result( $es_record->{'_source'} );
$records[$index] = $self->decode_record_from_result( $es_record->{'_source'} );
$scores[ $index++ ] = $es_record->{'_score'};
}
# consumers of this expect a name-spaced result, we provide the default
@ -188,6 +190,7 @@ sub search_compat {
my %result;
$result{biblioserver}{hits} = $hits->{'total'};
$result{biblioserver}{RECORDS} = \@records;
$result{biblioserver}{scores} = \@scores;
my $facets = $self->_convert_facets( $results->{aggregations} );
if ( C4::Context->interface eq 'opac' ) {

View File

@ -710,6 +710,7 @@ if ($hits) {
);
$template->param( hits_to_paginate => $hits_to_paginate );
$template->param( SEARCH_RESULTS => \@newresults );
$template->param( SCORES => $results_hashref->{$server}->{"scores"} );
# FIXME: no previous_page_offset when pages < 2
$template->param(

View File

@ -680,7 +680,10 @@
[% SEARCH_RESULT.result_number | html %].
[% SEARCH_RESULT.XSLTResultsRecord | $raw %]
[% IF Koha.Preference('SearchEngine') == 'Elasticsearch' %]
[% SET score_index = loop().index %]
<span class="search_score" style="display:none;">Score: [% SCORES.$score_index | html %]</span>
[% END %]
<p class="hold">
[% IF ( SEARCH_RESULT.norequests ) %]
<span class="noholdstext">No holds allowed</span>