Bug 18736: (follow-up) Fix missing rounding and bad formatting
[koha.git] / C4 / Matcher.pm
index 01fdbc6..dc84804 100644 (file)
@@ -23,6 +23,7 @@ use MARC::Record;
 
 use Koha::SearchEngine;
 use Koha::SearchEngine::Search;
+use Koha::SearchEngine::QueryBuilder;
 use Koha::Util::Normalize qw/legacy_default remove_spaces upper_case lower_case/;
 
 =head1 NAME
@@ -43,7 +44,7 @@ C4::Matcher - find MARC records matching another one
   $matcher->add_matchpoint('isbn', 1000, [ { tag => '020', subfields => 'a', norms => [] } ]);
 
   $matcher->add_simple_required_check('245', 'a', -1, 0, '', '245', 'a', -1, 0, '');
-  $matcher->add_required_check([ { tag => '245', subfields => 'a', norms => [] } ], 
+  $matcher->add_required_check([ { tag => '245', subfields => 'a', norms => [] } ],
                                [ { tag => '245', subfields => 'a', norms => [] } ]);
 
   my @matches = $matcher->get_matches($marc_record, $max_matches);
@@ -330,7 +331,7 @@ sub _store_matchpoint {
     my $matcher_id = $self->{'id'};
     $sth = $dbh->prepare_cached("INSERT INTO matchpoints (matcher_id, search_index, score)
                                  VALUES (?, ?, ?)");
-    $sth->execute($matcher_id, $matchpoint->{'index'}, $matchpoint->{'score'});
+    $sth->execute($matcher_id, $matchpoint->{'index'}, $matchpoint->{'score'}||0);
     my $matchpoint_id = $dbh->{'mysql_insertid'};
     my $seqnum = 0;
     foreach my $component (@{ $matchpoint->{'components'} }) {
@@ -342,7 +343,7 @@ sub _store_matchpoint {
         $sth->bind_param(2, $seqnum);
         $sth->bind_param(3, $component->{'tag'});
         $sth->bind_param(4, join "", sort keys %{ $component->{'subfields'} });
-        $sth->bind_param(5, $component->{'offset'});
+        $sth->bind_param(5, $component->{'offset'}||0);
         $sth->bind_param(6, $component->{'length'});
         $sth->execute();
         my $matchpoint_component_id = $dbh->{'mysql_insertid'};
@@ -663,7 +664,10 @@ sub get_matches {
                     #NOTE: double-quote the values so you don't get a "Embedded truncation not supported" error when a term has a ? in it.
             }
 
-            my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
+            # 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});
             ( $error, $searchresults, $total_hits ) =
               $searcher->simple_search_compat( $query, 0, $max_matches, undef, skip_normalize => 1 );
 
@@ -674,7 +678,9 @@ sub get_matches {
                 if ( C4::Context->preference('SearchEngine') eq 'Elasticsearch' ) {
                     foreach my $matched ( @{$searchresults} ) {
                         my ( $biblionumber_tag, $biblionumber_subfield ) = C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber", $marcframework_used );
-                        my $id = $matched->field($biblionumber_tag)->subfield($biblionumber_subfield);
+                        my $id = ( $biblionumber_tag > 10 ) ?
+                            $matched->field($biblionumber_tag)->subfield($biblionumber_subfield) :
+                            $matched->field($biblionumber_tag)->data();
                         $matches->{$id}->{score} += $matchpoint->{score};
                         $matches->{$id}->{record} = $matched;
                     }
@@ -689,7 +695,6 @@ sub get_matches {
 
         }
         elsif ( $self->{'record_type'} eq 'authority' ) {
-            my $authresults;
             my @marclist;
             my @and_or;
             my @excluding = [];
@@ -701,13 +706,17 @@ sub get_matches {
                 push @operator, 'exact';
                 push @value,    $key;
             }
-            require C4::AuthoritiesMarc;
-            ( $authresults, $total_hits ) =
-              C4::AuthoritiesMarc::SearchAuthorities(
-                \@marclist,  \@and_or, \@excluding, \@operator,
-                \@value,     0,        20,          undef,
-                'AuthidAsc', 1
-              );
+            # 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 $builder  = Koha::SearchEngine::QueryBuilder->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
+            state $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
+            my $search_query = $builder->build_authorities_query_compat(
+                \@marclist, \@and_or, \@excluding, \@operator,
+                \@value, undef, 'AuthidAsc'
+            );
+            my ( $authresults, $total ) = $searcher->search_auth_compat( $search_query, 0, 20 );
+
             foreach my $result (@$authresults) {
                 my $id = $result->{authid};
                 $matches->{$id}->{score} += $matchpoint->{'score'};