From 4086c1c4ff57c46881e4e44e06672eae7091373c Mon Sep 17 00:00:00 2001 From: Kevin Carnes Date: Tue, 8 Feb 2022 14:48:19 +0100 Subject: [PATCH] Bug 28610: Elasticsearch 7 - hits.total is now an object In Elasticsearch 7 hits.total is now an object which is not always an exact value. You can always get an exact total by adding a track_total_hits parameter set to true when using the Elasticsearch search method To test: 1) Run prove t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t 2) If you observe an error about types, apply patch for bug 25669 3) Run prove t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t 4) Observe that tests with count fail 5) Apply patch 6) Observe that tests with count pass 7) Sign off Sponsored-by: Lund University Library Signed-off-by: Julian Maurice Signed-off-by: Nick Clemens Signed-off-by: Fridolin Somers Signed-off-by: Kyle M Hall (cherry picked from commit 301ed1567b78956b7d6ec9ef2378e37f87ca1371) Signed-off-by: Victor Grousset/tuxayo --- Koha/SearchEngine/Elasticsearch/Search.pm | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index 1c8f609bf9..4ef1652ac1 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -50,9 +50,9 @@ use Koha::SearchEngine::Search; use Koha::Exceptions::Elasticsearch; use MARC::Record; use MARC::File::XML; -use Data::Dumper; #TODO remove use Carp qw(cluck); use MIME::Base64; +use JSON; Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store )); @@ -95,12 +95,16 @@ sub search { my $results = eval { $elasticsearch->search( index => $self->index_name, + track_total_hits => JSON::true, body => $query ); }; if ($@) { die $self->process_error($@); } + if (ref $results->{hits}->{total} eq 'HASH') { + $results->{hits}->{total} = $results->{hits}->{total}->{value}; + } return $results; } @@ -121,9 +125,13 @@ sub count { # and just return number of hits my $result = $elasticsearch->search( index => $self->index_name, + track_total_hits => JSON::true, body => $query ); + if (ref $result->{hits}->{total} eq 'HASH') { + return $result->{hits}->{total}->{value}; + } return $result->{hits}->{total}; } -- 2.39.5