From 25c87a754203f38267eba219d34f86b5922be298 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 15 Feb 2017 15:48:17 +0100 Subject: [PATCH] Bug 9988: Add extract_biblionumber to Koha/SearchEngine MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit When we will replace the Zebra code in sub merge, we will call SearchEngine to pass records and we need a routine to extract a biblionumber from a search result record. A record from Zebra still must be converted to MARC::Record. This is no longer needed for a ElasticSearch record. Test plan: Run t/db_dependent/Koha/SearchEngine/Search.t Signed-off-by: Marcel de Rooy Signed-off-by: Marc Véron Signed-off-by: Jacek Ablewicz Signed-off-by: Julian Maurice Signed-off-by: Kyle M Hall --- Koha/SearchEngine/Elasticsearch/Search.pm | 16 ++++++ Koha/SearchEngine/Search.pm | 23 ++++++++- Koha/SearchEngine/Zebra/Search.pm | 17 ++++++ t/db_dependent/Koha/SearchEngine/Search.t | 63 +++++++++++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 t/db_dependent/Koha/SearchEngine/Search.t diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index ae2dd4c68b..26bc4fc3c1 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -45,6 +45,7 @@ use C4::Context; use Koha::ItemTypes; use Koha::AuthorisedValues; use Koha::SearchEngine::QueryBuilder; +use Koha::SearchEngine::Search; use MARC::Record; use Catmandu::Store::ElasticSearch; @@ -344,6 +345,21 @@ sub simple_search_compat { return (undef, \@records, $results->total); } +=head2 extract_biblionumber + + my $biblionumber = $searcher->extract_biblionumber( $searchresult ); + +$searchresult comes from simple_search_compat. + +Returns the biblionumber from the search result record. + +=cut + +sub extract_biblionumber { + my ( $self, $searchresultrecord ) = @_; + return Koha::SearchEngine::Search::extract_biblionumber( $searchresultrecord ); +} + =head2 json2marc my $marc = $self->json2marc($marc_json); diff --git a/Koha/SearchEngine/Search.pm b/Koha/SearchEngine/Search.pm index 7d928d7ba1..fdaf7d373d 100644 --- a/Koha/SearchEngine/Search.pm +++ b/Koha/SearchEngine/Search.pm @@ -43,8 +43,9 @@ Creates a new C of whatever the relevant type is. =cut -use C4::Context; use Modern::Perl; +use C4::Context; +use C4::Biblio qw//; sub new { my $engine = C4::Context->preference("SearchEngine") // 'Zebra'; @@ -55,4 +56,24 @@ sub new { return $class->new(@_); } +=head2 extract_biblionumber + + my $biblionumber = $searcher->extract_biblionumber( $marc ); + +Returns the biblionumber from $marc. The routine is called from the +extract_biblionumber method of the specific search engine. + +=cut + +sub extract_biblionumber { + my ( $record ) = @_; + return if ref($record) ne 'MARC::Record'; + my ( $biblionumbertagfield, $biblionumbertagsubfield ) = C4::Biblio::GetMarcFromKohaField( 'biblio.biblionumber', '' ); + if( $biblionumbertagfield < 10 ) { + my $controlfield = $record->field( $biblionumbertagfield ); + return $controlfield ? $controlfield->data : undef; + } + return $record->subfield( $biblionumbertagfield, $biblionumbertagsubfield ); +} + 1; diff --git a/Koha/SearchEngine/Zebra/Search.pm b/Koha/SearchEngine/Zebra/Search.pm index cb69e22fa2..2fb45daf56 100644 --- a/Koha/SearchEngine/Zebra/Search.pm +++ b/Koha/SearchEngine/Zebra/Search.pm @@ -23,6 +23,7 @@ use base qw(Class::Accessor); use C4::Search; # :( use C4::AuthoritiesMarc; +use Koha::SearchEngine::Search; =head1 NAME @@ -76,6 +77,22 @@ sub simple_search_compat { return C4::Search::SimpleSearch(@_); } +=head2 extract_biblionumber + + my $biblionumber = $searcher->extract_biblionumber( $searchresult ); + +$searchresult comes from simple_search_compat. + +Returns the biblionumber from the search result record. + +=cut + +sub extract_biblionumber { + my ( $self, $searchresultrecord ) = @_; + my $record = C4::Search::new_record_from_zebra( 'biblioserver', $searchresultrecord ); + return Koha::SearchEngine::Search::extract_biblionumber( $record ); +} + =head2 search_auth_compat This passes the search query on to C4::AuthoritiesMarc::SearchAuthorities diff --git a/t/db_dependent/Koha/SearchEngine/Search.t b/t/db_dependent/Koha/SearchEngine/Search.t new file mode 100644 index 0000000000..bbef2f10b6 --- /dev/null +++ b/t/db_dependent/Koha/SearchEngine/Search.t @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +# Tests for Koha/SearchEngine/Search + +use Modern::Perl; + +use Test::More tests => 1; + +use MARC::Field; +use MARC::Record; +use Test::MockModule; +use Test::MockObject; + +use t::lib::Mocks; + +#use C4::Biblio qw//; +use Koha::Database; +use Koha::SearchEngine::Search; + +BEGIN { + my $mock = Test::MockObject->new(); + $mock->fake_module( 'Catmandu::Store::ElasticSearch' ); +} + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +subtest 'Test extract_biblionumber' => sub { + plan tests => 2; + + t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' ); + my $biblio_mod = Test::MockModule->new( 'C4::Biblio' ); + my $search_mod = Test::MockModule->new( 'C4::Search' ); + my $koha_fields = [ '001', '' ]; + $biblio_mod->mock( 'GetMarcFromKohaField', sub { return @$koha_fields; }); + $search_mod->mock( 'new_record_from_zebra', \&test_record ); + + # Extract using 001 + my $searcher = Koha::SearchEngine::Search->new; + my $bibno = $searcher->extract_biblionumber( 'fake_result' ); + is( $bibno, 3456, 'Extracted biblio number for Zebra' ); + + # Now use 999c with Elasticsearch + t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' ); + $search_mod->unmock( 'new_record_from_zebra' ); + $koha_fields = [ '999', 'c' ]; + $searcher = Koha::SearchEngine::Search->new({ index => 'biblios' }); + $bibno = $searcher->extract_biblionumber( test_record() ); + is( $bibno, 4567, 'Extracted biblio number for Zebra' ); +}; + +# -- Helper routine +sub test_record { + my $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '001', '3456' ), + MARC::Field->new( '245', '', '', a => 'Some title' ), + MARC::Field->new( '999', '', '', c => '4567' ), + ); + return $marc; +} + +$schema->storage->txn_rollback; -- 2.39.5