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 <tomascohen@theke.io>
Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Nick Clemens 2025-02-07 19:21:28 +00:00 committed by Katrin Fischer
parent e2207cb478
commit d73e3399ca
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
3 changed files with 27 additions and 8 deletions

View file

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

View file

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

View file

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