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
my $self = shift;
my ($source_record, $max_matches) = @_;
- my %matches = ();
+ my $matches = {};
+ my $marcframework_used = ''; # use the default framework
my $QParser;
$QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
my $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 {
+ 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 = ( $biblionumber_tag > 10 ) ?
+ $matched->field($biblionumber_tag)->subfield($biblionumber_subfield) :
+ $matched->field($biblionumber_tag)->data();
+ $matches->{$id}->{score} += $matchpoint->{score};
+ $matches->{$id}->{record} = $matched;
+ }
+ }
+ else {
+ foreach my $matched ( @{$searchresults} ) {
+ $matches->{$matched}->{score} += $matchpoint->{'score'};
+ $matches->{$matched}->{record} = $matched;
+ }
+ }
+ }
+
}
elsif ( $self->{'record_type'} eq 'authority' ) {
- my $authresults;
my @marclist;
my @and_or;
my @excluding = [];
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'};
- }
- }
+ my $builder = Koha::SearchEngine::QueryBuilder->new({index => $Koha::SearchEngine::AUTHORITIES_INDEX});
+ my $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 );
- if ( defined $error ) {
- warn "search failed ($query) $error";
- }
- else {
- foreach my $matched ( @{$searchresults} ) {
- $matches{$matched} += $matchpoint->{'score'};
+ foreach my $result (@$authresults) {
+ my $id = $result->{authid};
+ $matches->{$id}->{score} += $matchpoint->{'score'};
+ $matches->{$id}->{record} = $id;
}
}
}
# get rid of any that don't meet the threshold
- %matches = map { ($matches{$_} >= $self->{'threshold'}) ? ($_ => $matches{$_}) : () } keys %matches;
-
- # get rid of any that don't meet the required checks
- %matches = map { _passes_required_checks($source_record, $_, $self->{'required_checks'}) ? ($_ => $matches{$_}) : () }
- keys %matches unless ($self->{'record_type'} eq 'auth');
+ $matches = { map { ($matches->{$_}->{score} >= $self->{'threshold'}) ? ($_ => $matches->{$_}) : () } keys %$matches };
my @results = ();
if ($self->{'record_type'} eq 'biblio') {
require C4::Biblio;
- foreach my $marcblob (keys %matches) {
- my $target_record = C4::Search::new_record_from_zebra('biblioserver',$marcblob);
- my $record_number;
- my $result = C4::Biblio::TransformMarcToKoha($target_record, '');
- $record_number = $result->{'biblionumber'};
- push @results, { 'record_id' => $record_number, 'score' => $matches{$marcblob} };
+ # get rid of any that don't meet the required checks
+ $matches = {
+ map {
+ _passes_required_checks( $source_record, $_, $self->{'required_checks'} )
+ ? ( $_ => $matches->{$_} )
+ : ()
+ } keys %$matches
+ };
+
+ foreach my $id ( keys %$matches ) {
+ my $target_record = C4::Search::new_record_from_zebra( 'biblioserver', $matches->{$id}->{record} );
+ my $result = C4::Biblio::TransformMarcToKoha( $target_record, $marcframework_used );
+ push @results, {
+ record_id => $result->{biblionumber},
+ score => $matches->{$id}->{score}
+ };
}
} elsif ($self->{'record_type'} eq 'authority') {
require C4::AuthoritiesMarc;
- foreach my $authid (keys %matches) {
- push @results, { 'record_id' => $authid, 'score' => $matches{$authid} };
+ foreach my $id (keys %$matches) {
+ push @results, {
+ record_id => $id,
+ score => $matches->{$id}->{score}
+ };
}
}
@results = sort {
@results = @results[0..$max_matches-1];
}
return @results;
-
}
=head2 dump