Bugzilla – Attachment 177715 Details for
Bug 39070
Elasticsearch facets are not used/needed when finding record matches
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 39070: Don't request facets when searching for recrod matches
Bug-39070-Dont-request-facets-when-searching-for-r.patch (text/plain), 6.15 KB, created by
David Cook
on 2025-02-10 23:49:55 UTC
(
hide
)
Description:
Bug 39070: Don't request facets when searching for recrod matches
Filename:
MIME Type:
Creator:
David Cook
Created:
2025-02-10 23:49:55 UTC
Size:
6.15 KB
patch
obsolete
>From b0ceeba3ebbf455dd4efc0ac301d0b193bfb6f62 Mon Sep 17 00:00:00 2001 >From: Nick Clemens <nick@bywatersolutions.com> >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 <tomascohen@theke.io> >Signed-off-by: David Cook <dcook@prosentient.com.au> >--- > C4/Matcher.pm | 10 +++++-- > .../Elasticsearch/QueryBuilder.pm | 29 +++++++++++-------- > .../SearchEngine/Elasticsearch/QueryBuilder.t | 10 ++++++- > 3 files changed, 34 insertions(+), 15 deletions(-) > >diff --git a/C4/Matcher.pm b/C4/Matcher.pm >index fbfcc1cb40..8be5b3bec7 100644 >--- a/C4/Matcher.pm >+++ b/C4/Matcher.pm >@@ -655,9 +655,15 @@ 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 $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX}); >+ 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 ); >+ $searcher->simple_search_compat( $query, 0, $max_matches, undef, skip_normalize => 1 ); > > if ( defined $error ) { > warn "search failed ($query) $error"; >diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm >index ad240bdde6..b1fd3c64e3 100644 >--- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm >+++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm >@@ -224,14 +224,18 @@ 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 ; > return $res; >@@ -310,11 +314,12 @@ sub build_query_compat { > # 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{sort} = \@sort_params; >+ $options{is_opac} = $params->{is_opac}; > $options{weighted_fields} = $params->{weighted_fields}; >- $options{whole_record} = $params->{whole_record}; >- $query = $self->build_query( $query_str, %options ); >+ $options{whole_record} = $params->{whole_record}; >+ $options{skip_facets} = $params->{skip_facets}; >+ $query = $self->build_query( $query_str, %options ); > } > > # We roughly emulate the CGI parameters of the zebra query builder >diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t >index ca0e3666b6..747c4bbea5 100755 >--- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t >+++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t >@@ -219,7 +219,7 @@ subtest 'build_authorities_query_compat() tests' => sub { > }; > > subtest 'build_query tests' => sub { >- plan tests => 65; >+ plan tests => 68; > > my $qb; > >@@ -254,6 +254,10 @@ subtest 'build_query tests' => sub { > is( $query->{aggregations}{homebranch}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount for homebranch'); > is( $query->{aggregations}{holdingbranch}{terms}{size}, 37,'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'] ); >@@ -262,6 +266,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( >-- >2.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 39070
:
177655
|
177656
| 177715