From 9b5d5a03837fa9e716800396a4701c62faf3f074 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 | 39 +++++++++++++++---- .../prog/en/modules/catalogue/advsearch.tt | 21 +++++++++- .../bootstrap/en/modules/opac-advsearch.tt | 9 ++++- opac/opac-search.pl | 30 ++++++++++---- 5 files changed, 84 insertions(+), 20 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 8ee830c649..ad339ae7e4 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 762d952ae7..806178c836 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -277,6 +277,7 @@ if ( $template_type eq 'advsearch' ) { @operands = $cgi->multi_param('q'); @operators = @sanitized_operators; @indexes = $cgi->multi_param('idx'); + $expanded = 1; # Force expanded options when editing search $template->param( sort => scalar $cgi->param('sort_by'), ); @@ -341,7 +342,15 @@ if ( $template_type eq 'advsearch' ) { { $expanded = C4::Context->preference("expandedSearchOption") || 0 if !defined($expanded) || $expanded !~ /^0|1$/; - $template->param( expanded_options => $expanded ); + + # Force expanded options if weight_search_submitted or whole_record are present + $expanded = 1 if $cgi->param('weight_search_submitted') || $cgi->param('whole_record'); + + $template->param( + expanded_options => $expanded, + weight_search_submitted => scalar $cgi->param('weight_search_submitted'), + whole_record => scalar $cgi->param('whole_record'), + ); } $template->param( virtualshelves => C4::Context->preference("virtualshelves") ); @@ -436,11 +445,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 @@ -462,7 +473,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'); @@ -559,7 +576,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 ab6b53aac6..736eef6f95 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt @@ -153,13 +153,30 @@ [% IF ( expanded_options ) %]

[% IF Koha.Preference('ElasticsearchMARCFormat') == 'ARRAY' %] - + [% 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 0cbe7c8390..0efd23e1fe 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-advsearch.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-advsearch.tt @@ -151,13 +151,20 @@ [% 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 db9dc776c3..26d0a9437a 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -216,7 +216,9 @@ if ( $cgi->cookie("search_path_code") ) { if ( $pathcode eq 'ads' ) { $template->param( 'ReturnPath' => '/cgi-bin/koha/opac-search.pl?returntosearch=1' ); } elsif ( $pathcode eq 'exs' ) { - $template->param( 'ReturnPath' => '/cgi-bin/koha/opac-search.pl?expanded_options=1&returntosearch=1' ); + my $return_params = 'expanded_options=1&returntosearch=1'; + $return_params .= '&weight_search_submitted=1' if $cgi->param('weight_search_submitted'); + $template->param( 'ReturnPath' => "/cgi-bin/koha/opac-search.pl?$return_params" ); } else { warn "ReturnPath switch error"; } @@ -357,6 +359,14 @@ if ( $template_type && $template_type eq 'advsearch' ) { } } + # Force expanded options if weight_search_submitted is present + if ( $cgi->param('weight_search_submitted') ) { + $template->param( expanded_options => 1 ); + } + + # Always pass weight_search_submitted to template for checkbox logic + $template->param( weight_search_submitted => scalar $cgi->param('weight_search_submitted') ); + output_html_with_http_headers $cgi, $cookie, $template->output; exit; } @@ -494,7 +504,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 +535,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 +657,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.50.1 (Apple Git-155)