- my $query = join(" or ", map { "$matchpoint->{'index'}=$_" } @source_keys);
- # FIXME only searching biblio index at the moment
- my ($error, $searchresults, $total_hits) = SimpleSearch($query, 0, $max_matches);
-
- if (defined $error ) {
- warn "search failed ($query) $error";
- } else {
- foreach my $matched (@{$searchresults}) {
- $matches{$matched} += $matchpoint->{'score'};
+ my $query;
+ my $error;
+ my $searchresults;
+ my $total_hits;
+ 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;
+ }
+ }
+
+ }
+ elsif ( $self->{'record_type'} eq 'authority' ) {
+ 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;
+ }
+ # 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'};
+ $matches->{$id}->{record} = $id;