From ea2dfa655030fc992158205ca2dafb06241d0899 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 20 Dec 2024 13:40:00 +0000 Subject: [PATCH] Bug 38694: Tidy routines for QA --- .../Elasticsearch/QueryBuilder.pm | 103 +++++++++--------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 932288653ee..50aec79fc4e 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -186,28 +186,30 @@ according to these values. Valid values for C are 'asc' and 'desc'. sub build_query { my ( $self, $query, %options ) = @_; - my $stemming = C4::Context->preference("QueryStemming") || 0; - my $auto_truncation = C4::Context->preference("QueryAutoTruncate") || 0; - my $fuzzy_enabled = C4::Context->preference("QueryFuzzy") || 0; + my $stemming = C4::Context->preference("QueryStemming") || 0; + my $auto_truncation = C4::Context->preference("QueryAutoTruncate") || 0; + my $fuzzy_enabled = C4::Context->preference("QueryFuzzy") || 0; $query = '*' unless defined $query; my $res; - my $fields = $self->_search_fields({ - is_opac => $options{is_opac}, - weighted_fields => $options{weighted_fields}, - }); - if ($options{whole_record}) { + my $fields = $self->_search_fields( + { + is_opac => $options{is_opac}, + weighted_fields => $options{weighted_fields}, + } + ); + if ( $options{whole_record} ) { push @$fields, 'marc_data_array.*'; } my $query_string = { - query => $query, - fuzziness => $fuzzy_enabled ? 'auto' : '0', - default_operator => 'AND', - fields => $fields, - lenient => JSON::true, - analyze_wildcard => JSON::true, - }; + query => $query, + fuzziness => $fuzzy_enabled ? 'auto' : '0', + default_operator => 'AND', + fields => $fields, + lenient => JSON::true, + analyze_wildcard => JSON::true, + }; $query_string->{type} = 'cross_fields' if C4::Context->preference('ElasticsearchCrossFields'); $res->{query} = { bool => { must => [ { query_string => $query_string } ] } }; @@ -218,7 +220,7 @@ sub build_query { foreach my $sort ( @{ $options{sort} } ) { my ( $f, $d ) = @$sort{qw/ field direction /}; die "Invalid sort direction, $d" - if $d && ( $d ne 'asc' && $d ne 'desc' ); + if $d && ( $d ne 'asc' && $d ne 'desc' ); $d = 'asc' unless $d; $f = $self->_sort_field($f); @@ -228,14 +230,14 @@ sub build_query { # See _convert_facets in Search.pm for how these get turned into # things that Koha can use. - my $size = C4::Context->preference('FacetMaxCount'); + my $size = C4::Context->preference('FacetMaxCount'); my @facets = Koha::SearchEngine::Elasticsearch->get_facet_fields; - for my $f ( @facets ) { + for my $f (@facets) { my $name = $f->name; - $res->{aggregations}->{$name} = { terms => { field => "${name}__facet" , size => $size } }; - }; + $res->{aggregations}->{$name} = { terms => { field => "${name}__facet", size => $size } }; + } - $res = _rebuild_to_es_advanced_query($res) if @$es_advanced_searches ; + $res = _rebuild_to_es_advanced_query($res) if @$es_advanced_searches; return $res; } @@ -260,17 +262,17 @@ reproduce this search, and C<$query_desc> set to something else. =cut sub build_query_compat { - my ( $self, $operators, $operands, $indexes, $orig_limits, $sort_by, $scan, - $lang, $params ) - = @_; - + my ( + $self, $operators, $operands, $indexes, $orig_limits, $sort_by, $scan, + $lang, $params + ) = @_; my $query; - my $query_str = ''; + my $query_str = ''; my $search_param_query_str = ''; - my $limits = (); - if ( $scan ) { - ($query, $query_str) = $self->_build_scan_query( $operands, $indexes ); + my $limits = (); + if ($scan) { + ( $query, $query_str ) = $self->_build_scan_query( $operands, $indexes ); $search_param_query_str = $query_str; } else { my @sort_params = $self->_convert_sort_fields(@$sort_by); @@ -281,19 +283,20 @@ sub build_query_compat { : []; $limits = $self->_fix_limit_special_cases($orig_limits); if ( $params->{suppress} ) { push @$limits, "suppress:false"; } + # Merge the indexes in with the search terms and the operands so that # each search thing is a handy unit. unshift @$operators, undef; # The first one can't have an op my @search_params; my $truncate = C4::Context->preference("QueryAutoTruncate") || 0; - my $ea = each_array( @$operands, @$operators, @index_params ); + my $ea = each_array( @$operands, @$operators, @index_params ); while ( my ( $oand, $otor, $index ) = $ea->() ) { next if ( !defined($oand) || $oand eq '' ); $oand = $self->clean_search_term($oand); $oand = $self->_truncate_terms($oand) if $truncate && $self->_is_safe_to_auto_truncate( $index->{field}, $oand ); push @search_params, { - operand => $oand, # the search terms + operand => $oand, # the search terms operator => defined($otor) ? uc $otor : undef, # AND and so on $index ? %$index : (), }; @@ -305,49 +308,51 @@ sub build_query_compat { # more robust. my @search_param_query_array = $self->_create_query_string(@search_params); $search_param_query_str = join( ' ', @search_param_query_array ); - my $search_param_limit_str = - $self->_join_queries( $self->_convert_index_strings(@$limits) ); + my $search_param_limit_str = $self->_join_queries( $self->_convert_index_strings(@$limits) ); if ( @search_param_query_array > 1 && $search_param_limit_str ) { $search_param_query_str = "($search_param_query_str)"; } - $query_str = join( ' AND ', + $query_str = join( + ' AND ', $search_param_query_str || (), - $search_param_limit_str || () ); + $search_param_limit_str || () + ); # If there's no query on the left, let's remove the junk left behind $query_str =~ s/^ AND //; my %options; - $options{sort} = \@sort_params; - $options{is_opac} = $params->{is_opac}; - $options{weighted_fields} = $params->{weighted_fields}; - $options{whole_record} = $params->{whole_record}; + $options{sort} = \@sort_params; + $options{is_opac} = $params->{is_opac}; + $options{weighted_fields} = $params->{weighted_fields}; + $options{whole_record} = $params->{whole_record}; $options{field_match_boost_query} = $field_match_boost_query if @$field_match_boost_query; - $query = $self->build_query( $query_str, %options ); + $query = $self->build_query( $query_str, %options ); } # We roughly emulate the CGI parameters of the zebra query builder my $query_cgi = ''; - shift @$operators; # Shift out the one we unshifted before + shift @$operators; # Shift out the one we unshifted before my $ea = each_array( @$operands, @$operators, @$indexes ); while ( my ( $oand, $otor, $index ) = $ea->() ) { $query_cgi .= '&' if $query_cgi; - $query_cgi .= 'idx=' . uri_escape_utf8( $index // '') . '&q=' . uri_escape_utf8( $oand ); - $query_cgi .= '&op=' . uri_escape_utf8( $otor ) if $otor; + $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); my $simple_query; $simple_query = $operands->[0] if @$operands == 1; my $query_desc; - if ( $simple_query ) { + if ($simple_query) { $query_desc = $simple_query; } else { $query_desc = $search_param_query_str; } - my $limit = $self->_join_queries( $self->_convert_index_strings(@$limits)); - my $limit_cgi = ( $orig_limits and @$orig_limits ) - ? '&limit=' . join( '&limit=', map { uri_escape_utf8($_) } @$orig_limits ) - : ''; + my $limit = $self->_join_queries( $self->_convert_index_strings(@$limits) ); + my $limit_cgi = + ( $orig_limits and @$orig_limits ) + ? '&limit=' . join( '&limit=', map { uri_escape_utf8($_) } @$orig_limits ) + : ''; my $limit_desc; $limit_desc = "$limit" if $limit; -- 2.39.5