From d73e3399ca349b07ebd4427f93819ba7a611cbae Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 7 Feb 2025 19:21:28 +0000 Subject: [PATCH] Bug 39070: Don't request facets when searching for recrod matches This patch adds a new option to Elasticsearch to skip facets in build_query and build_query_compat. There should be no behavior change, however, queries to ES should be simpler To test: 1 - Export some record from Koha 2 - Stage the records for import, matching on 999c 3 - Confirm expected matches 4 - Switch batch to use ISBN for matching 5 - Confirm expected matches 6 - Apply patches, restart all 7 - Switch batch to use 999c 8 - Confirm matches 9 - Switch batch to use ISBN 10 - Confirm matches Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Cook Signed-off-by: Katrin Fischer --- C4/Matcher.pm | 6 ++++++ .../Elasticsearch/QueryBuilder.pm | 19 ++++++++++++------- .../SearchEngine/Elasticsearch/QueryBuilder.t | 10 +++++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/C4/Matcher.pm b/C4/Matcher.pm index 2bb6418cb4..9369782ff2 100644 --- a/C4/Matcher.pm +++ b/C4/Matcher.pm @@ -700,6 +700,12 @@ sub get_matches { # Use state variables to avoid recreating the objects every time. # With Elasticsearch this also avoids creating a massive amount of # ES connectors that would eventually run out of file descriptors. + state $query_builder = + Koha::SearchEngine::QueryBuilder->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); + ( undef, $query ) = $query_builder->build_query_compat( + undef, [$query], undef, undef, undef, undef, undef, + { skip_facets => 1 } + ); state $searcher = Koha::SearchEngine::Search->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); ( $error, $searchresults, $total_hits ) = $searcher->simple_search_compat( $query, 0, $max_matches, undef, skip_normalize => 1 ); diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 7948384837..0e9d964727 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -227,13 +227,17 @@ 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 @facets = Koha::SearchEngine::Elasticsearch->get_facet_fields; - for my $f (@facets) { - my $name = $f->name; - $res->{aggregations}->{$name} = { terms => { field => "${name}__facet", size => $size } }; + unless ( $options{skip_facets} ) { + + # See _convert_facets in Search.pm for how these get turned into + # things that Koha can use. + my $size = C4::Context->preference('FacetMaxCount'); + my @facets = Koha::SearchEngine::Elasticsearch->get_facet_fields; + for my $f (@facets) { + my $name = $f->name; + $res->{aggregations}->{$name} = { terms => { field => "${name}__facet", size => $size } }; + } + } $res = _rebuild_to_es_advanced_query($res) if @$es_advanced_searches; @@ -320,6 +324,7 @@ sub build_query_compat { $options{is_opac} = $params->{is_opac}; $options{weighted_fields} = $params->{weighted_fields}; $options{whole_record} = $params->{whole_record}; + $options{skip_facets} = $params->{skip_facets}; $query = $self->build_query( $query_str, %options ); } diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 2c0b746fac..c3c43d1fc4 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -227,7 +227,7 @@ subtest 'build_authorities_query_compat() tests' => sub { }; subtest 'build_query tests' => sub { - plan tests => 65; + plan tests => 68; my $qb; @@ -262,6 +262,10 @@ subtest 'build_query tests' => sub { 'we ask for the size as defined by the syspref FacetMaxCount for holdingbranch' ); + $options{skip_facets} = 1; + $query = $qb->build_query( 'test', %options ); + ok( !defined $query->{aggregations}, 'Skipping facets means we do not have aggregations in the the query' ); + t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' ); ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] ); @@ -270,6 +274,10 @@ subtest 'build_query tests' => sub { "(donald duck)", "query not altered if QueryAutoTruncate disabled" ); + ok( defined $query->{aggregations}, 'Aggregations generated normally' ); + ( undef, $query ) = + $qb->build_query_compat( undef, ['donald duck'], undef, undef, undef, undef, undef, { skip_facets => 1 } ); + ok( !defined $query->{aggregations}, 'Aggregations generated normally' ); ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] ); is(