- if ($QParser) {
- $query = join(" || ", map { "$matchpoint->{'index'}:$_" } @source_keys);
- require C4::Search;
- ($error, $searchresults, $total_hits) = C4::Search::SimpleSearch($query, 0, $max_matches, [ $self->{'record_type'} . 'server' ] );
- } else {
- if ($self->{'record_type'} eq 'biblio') {
- $query = join(" or ", map { "$matchpoint->{'index'}=$_" } @source_keys);
- require C4::Search;
- ($error, $searchresults, $total_hits) = C4::Search::SimpleSearch($query, 0, $max_matches);
- } elsif ($self->{'record_type'} eq 'authority') {
- my $authresults;
- my @marclist;
- my @and_or;
- my @excluding = [];
- my @operator;
- my @value;
- foreach my $key (@source_keys) {
- push @marclist, $matchpoint->{'index'};
- push @and_or, 'or';
- 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
- );
- foreach my $result (@$authresults) {
- push @$searchresults, $result->{'authid'};
+ if ( $self->{'record_type'} eq 'biblio' ) {
+
+ my $phr = ( C4::Context->preference('AggressiveMatchOnISBN') || C4::Context->preference('AggressiveMatchOnISSN') ) ? ',phr' : q{};
+ $query = join( " OR ",
+ map { "$matchpoint->{'index'}$phr=\"$_\"" } @source_keys );
+ #NOTE: double-quote the values so you don't get a "Embedded truncation not supported" error when a term has a ? in it.
+
+ # 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 );
+
+ if ( defined $error ) {
+ warn "search failed ($query) $error";
+ }
+ else {
+ foreach my $matched ( @{$searchresults} ) {
+ my $target_record = C4::Search::new_record_from_zebra( 'biblioserver', $matched );
+ my ( $biblionumber_tag, $biblionumber_subfield ) = C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber" );
+ my $id = ( $biblionumber_tag > 10 ) ?
+ $target_record->field($biblionumber_tag)->subfield($biblionumber_subfield) :
+ $target_record->field($biblionumber_tag)->data();
+ $matches->{$id}->{score} += $matchpoint->{score};
+ $matches->{$id}->{record} = $target_record;