From 40b7bd13c14ce0da761fbc2a9546f14339a64aba Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 16 Mar 2026 12:57:35 +0000 Subject: [PATCH] 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 --- Koha/SearchEngine/Elasticsearch/Search.pm | 5 ++++- catalogue/search.pl | 1 + koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index ccf502c96c0..fea16dd0800 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -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' ) { diff --git a/catalogue/search.pl b/catalogue/search.pl index 957a9f9dcfc..a0974f3d69d 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -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( diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt index 75796aad548..7645de90eda 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt @@ -680,7 +680,10 @@ [% SEARCH_RESULT.result_number | html %]. [% SEARCH_RESULT.XSLTResultsRecord | $raw %] - + [% IF Koha.Preference('SearchEngine') == 'Elasticsearch' %] + [% SET score_index = loop().index %] + + [% END %]

[% IF ( SEARCH_RESULT.norequests ) %] No holds allowed -- 2.39.5