From 78911fb30baa5e1400a2963a9e9ca3748fca0d3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?=
Date: Tue, 7 Oct 2025 11:36:53 -0300
Subject: [PATCH] Bug 40966: Make sure 'whole_record' and 'weighted_fields' are
passed around
The ES query builder correctly handles this parameters (passed from
search.pl and opac-search.pl) when it builds the ES query. But the
returned 'query_cgi' that is used later for keeping the search results
query context for building things like facets, just doesn't pass this
values around, leading to a loss of them on the generated links.
This patch makes sure they are passed around.
To test:
1. Apply the regression tests
2. Run:
$ ktd --shell
k$ prove t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
=> FAIL: Tests fail! These params are not passed around!
3. Set ElasticsearchMARCFormat="Searchable array"
4. Reindex all the things:
k$ koha-elasticsearch --rebuild -d -v kohadev
5. Go to advanced search, using the 'More options' option.
6. Check 'Search entire MARC record' and 'Apply weights to search'
7. Perform a search on 'FULL TEXT'
=> SUCCESS: The URL contains `whole_record=on` and `weighted_fields=on`
8. Hover on any facet and look at the link
=> FAIL: It doesn't contain them!
9. Apply this patch
10. Restart all
k$ restart_all
11. Refresh the search results
12. Repeat 6
=> SUCCESS: Links have the needed parameters!
13. Repeat 2
=> SUCCESS: Tests pass! the parameters are passed around!
14. Sign off :-D
---
.../Elasticsearch/QueryBuilder.pm | 5 +++-
catalogue/search.pl | 28 ++++++++++++++-----
.../prog/en/modules/catalogue/advsearch.tt | 2 ++
.../bootstrap/en/modules/opac-advsearch.tt | 2 ++
opac/opac-search.pl | 18 +++++++-----
5 files changed, 40 insertions(+), 15 deletions(-)
diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
index 8ee830c6498..ad339ae7e43 100644
--- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
+++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
@@ -352,7 +352,10 @@ sub build_query_compat {
$query_cgi .= 'idx=' . uri_escape_utf8( $index // '' ) . '&q=' . uri_escape_utf8($oand);
$query_cgi .= '&op=' . uri_escape_utf8($otor) if $otor;
}
- $query_cgi .= '&scan=1' if ($scan);
+ $query_cgi .= '&scan=1' if ($scan);
+ $query_cgi .= '&weight_search=1' if ( $params->{weighted_fields} );
+ $query_cgi .= '&weight_search_submitted=1' if ( $params->{weight_search_submitted} );
+ $query_cgi .= '&whole_record=1' if ( $params->{whole_record} );
my $simple_query;
$simple_query = $operands->[0] if @$operands == 1;
diff --git a/catalogue/search.pl b/catalogue/search.pl
index b723855d066..596c5d428a1 100755
--- a/catalogue/search.pl
+++ b/catalogue/search.pl
@@ -435,11 +435,13 @@ for ( my $ii = 0 ; $ii < @operands ; ++$ii ) {
# Params that can only have one value
my $scan = $params->{'scan'};
-my $count = C4::Context->preference('numSearchResults') || 20;
-my $results_per_page = $params->{'count'} || $count;
-my $offset = $params->{'offset'} || 0;
-my $whole_record = $params->{'whole_record'} || 0;
-my $weight_search = $params->{'advsearch'} ? $params->{'weight_search'} || 0 : 1;
+my $count = C4::Context->preference('numSearchResults') || 20;
+my $results_per_page = $params->{'count'} || $count;
+my $offset = $params->{'offset'} || 0;
+my $whole_record = $params->{'whole_record'} || 0;
+my $weight_search = $params->{'weight_search_submitted'}
+ ? ( $params->{'weight_search'} ? 1 : 0 ) # Form was submitted, use actual checkbox value
+ : 0; # Form not submitted or no weight_search context
$offset = 0 if $offset < 0;
# Define some global variables
@@ -461,7 +463,13 @@ for ( my $i = 0 ; $i < @operands ; $i++ ) {
)
= $builder->build_query_compat(
\@operators, \@operands, \@indexes, \@limits,
- \@sort_by, $scan, $lang, { weighted_fields => $weight_search, whole_record => $whole_record }
+ \@sort_by,
+ $scan, $lang,
+ {
+ weighted_fields => $weight_search,
+ whole_record => $whole_record,
+ weight_search_submitted => $params->{'weight_search_submitted'}
+ }
);
$template->param( search_query => $query ) if C4::Context->preference('DumpSearchQueryTemplate');
@@ -558,7 +566,13 @@ if ( $hits == 0 && $basic_search ) {
)
= $builder->build_query_compat(
\@operators, \@operands, \@indexes, \@limits,
- \@sort_by, $scan, $lang, { weighted_fields => $weight_search, whole_record => $whole_record }
+ \@sort_by,
+ $scan, $lang,
+ {
+ weighted_fields => $weight_search,
+ whole_record => $whole_record,
+ weight_search_submitted => $params->{'weight_search_submitted'}
+ }
);
my $quoted_results_hashref;
eval {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt
index ab6b53aac6d..8f28f329beb 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt
@@ -156,10 +156,12 @@
[% END %]
+
[% ELSE %]
+
[% END %]
[% END %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-advsearch.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-advsearch.tt
index a4651d0a4e3..2df04d7e19f 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-advsearch.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-advsearch.tt
@@ -151,6 +151,7 @@
[% IF ( expanded_options ) %]
[% IF Koha.Preference('SearchEngine') == 'Elasticsearch' %]
+
[% END %]
[% ELSE %]
+
[% END %]
[% IF Koha.Preference('IncludeSeeFromInSearches') %]
diff --git a/opac/opac-search.pl b/opac/opac-search.pl
index db9dc776c3f..dc7a86ae4d6 100755
--- a/opac/opac-search.pl
+++ b/opac/opac-search.pl
@@ -494,7 +494,9 @@ $offset = 0 if $offset < 0;
my $page = $cgi->param('page') || 1;
$offset = ( $page - 1 ) * $results_per_page if $page > 1;
my $hits;
-my $weight_search = $cgi->param('advsearch') ? $cgi->param('weight_search') || 0 : 1;
+my $weight_search = $cgi->param('weight_search_submitted')
+ ? ( $cgi->param('weight_search') ? 1 : 0 ) # Form was submitted, use actual checkbox value
+ : 0; # Form not submitted or no weight_search context
# Define some global variables
my ( $error, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc, $query_type );
@@ -523,9 +525,10 @@ if ( C4::Context->preference('OpacSuppression') ) {
0,
$lang,
{
- suppress => $suppress,
- is_opac => 1,
- weighted_fields => $weight_search
+ suppress => $suppress,
+ is_opac => 1,
+ weighted_fields => $weight_search,
+ weight_search_submitted => $cgi->param('weight_search_submitted')
}
);
@@ -644,9 +647,10 @@ for ( my $i = 0 ; $i < @servers ; $i++ ) {
0,
$lang,
{
- suppress => $suppress,
- is_opac => 1,
- weighted_fields => $weight_search
+ suppress => $suppress,
+ is_opac => 1,
+ weighted_fields => $weight_search,
+ weight_search_submitted => $cgi->param('weight_search_submitted')
}
);
my $quoted_results_hashref;
--
2.51.0