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

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Tomás Cohen Arazi 2025-10-07 11:36:53 -03:00 committed by Lucas Gass
parent a1fa2dc71e
commit 402a251392
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
5 changed files with 85 additions and 20 deletions

View File

@ -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;

View File

@ -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
: 1; # Form not submitted
$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 {

View File

@ -153,13 +153,30 @@
[% IF ( expanded_options ) %]
<p>
[% IF Koha.Preference('ElasticsearchMARCFormat') == 'ARRAY' %]
<label><input type="checkbox" name="whole_record" /> Search entire MARC record</label>
<label>
[% IF whole_record %]
<input type="checkbox" name="whole_record" checked="checked" />
[% ELSE %]
<input type="checkbox" name="whole_record" />
[% END %]
Search entire MARC record</label
>
[% END %]
<span id="weight_search">
<label><input type="checkbox" name="weight_search" checked="checked" /> Apply field weights to search</label>
<input type="hidden" name="weight_search_submitted" value="1" />
<label>
[% IF weight_search_submitted && !weight_search %]
<input type="checkbox" name="weight_search" />
[% ELSE %]
[%# enabled by default or when weight_search==true %]
<input type="checkbox" name="weight_search" checked="checked" />
[% END %]
Apply field weights to search</label
>
</span>
</p>
[% ELSE %]
<input type="hidden" name="weight_search_submitted" value="1" />
<input type="hidden" name="weight_search" value="1" />
[% END %]
[% END %]

View File

@ -151,13 +151,20 @@
[% IF ( expanded_options ) %]
[% IF Koha.Preference('SearchEngine') == 'Elasticsearch' %]
<div id="weight_search">
<input type="hidden" name="weight_search_submitted" value="1" />
<label>
<input type="checkbox" name="weight_search" checked="checked" />
[% IF weight_search_submitted && !weight_search %]
<input type="checkbox" name="weight_search" />
[% ELSE %]
[%# enabled by default or when weight_search==true %]
<input type="checkbox" name="weight_search" checked="checked" />
[% END %]
Apply field weights to search
</label>
</div>
[% END %]
[% ELSE %]
<input type="hidden" name="weight_search_submitted" value="1" />
<input type="hidden" name="weight_search" value="1" />
[% END %]
[% IF Koha.Preference('IncludeSeeFromInSearches') %]

View File

@ -216,7 +216,10 @@ 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');
$return_params .= '&weight_search=1' if $cgi->param('weight_search');
$template->param( 'ReturnPath' => "/cgi-bin/koha/opac-search.pl?$return_params" );
} else {
warn "ReturnPath switch error";
}
@ -357,6 +360,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 +505,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
: 1; # Form not submitted
# Define some global variables
my ( $error, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc, $query_type );
@ -523,9 +536,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 +658,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;