3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 6;
24 use t::lib::TestBuilder;
30 use List::Util qw( any );
32 use Koha::SearchEngine::Elasticsearch;
33 use Koha::SearchEngine::Elasticsearch::Search;
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
38 subtest '_read_configuration() tests' => sub {
43 t::lib::Mocks::mock_config( 'elasticsearch', undef );
45 # 'elasticsearch' missing in configuration
47 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
49 'Koha::Exceptions::Config::MissingEntry',
50 'Configuration problem, exception thrown';
53 "Missing <elasticsearch> entry in koha-conf.xml",
54 'Exception message is correct'
57 # 'elasticsearch' present but no 'server' entry
58 t::lib::Mocks::mock_config( 'elasticsearch', {} );
60 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
62 'Koha::Exceptions::Config::MissingEntry',
63 'Configuration problem, exception thrown';
66 "Missing <elasticsearch>/<server> entry in koha-conf.xml",
67 'Exception message is correct'
70 # 'elasticsearch' and 'server' entries present, but no 'index_name'
71 t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server' } );
73 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
75 'Koha::Exceptions::Config::MissingEntry',
76 'Configuration problem, exception thrown';
79 "Missing <elasticsearch>/<index_name> entry in koha-conf.xml",
80 'Exception message is correct'
83 # Correct configuration, only one server
84 t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server', index_name => 'index' } );
86 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
87 is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
88 is_deeply( $configuration->{nodes}, ['a_server'], 'Server configuration parsed correctly' );
90 # Correct configuration, two servers
91 my @servers = ('a_server', 'another_server');
92 t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index' } );
94 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
95 is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
96 is( $configuration->{cxn_pool}, 'Static', 'cxn_pool configuration set correctly to Static if not specified' );
97 is_deeply( $configuration->{nodes}, \@servers , 'Server configuration parsed correctly' );
99 t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index', cxn_pool => 'Fluid' } );
101 $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
102 is( $configuration->{cxn_pool}, 'Fluid', 'cxn_pool configuration parsed correctly' );
104 my $params = Koha::SearchEngine::Elasticsearch::get_elasticsearch_params;
105 is_deeply( $configuration->{nodes}, \@servers , 'get_elasticsearch_params is just a wrapper for _read_configuration' );
109 subtest 'get_elasticsearch_settings() tests' => sub {
115 # test reading index settings
116 my $es = Koha::SearchEngine::Elasticsearch->new( {index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX} );
117 $settings = $es->get_elasticsearch_settings();
118 is( $settings->{index}{analysis}{analyzer}{analyzer_phrase}{tokenizer}, 'keyword', 'Index settings parsed correctly' );
121 subtest 'get_elasticsearch_mappings() tests' => sub {
127 # test reading mappings
128 my $es = Koha::SearchEngine::Elasticsearch->new( {index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX} );
129 $mappings = $es->get_elasticsearch_mappings();
130 is( $mappings->{data}{properties}{isbn__sort}{index}, 'false', 'Field mappings parsed correctly' );
133 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
137 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
138 t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
142 name => 'control_number',
148 marc_type => 'marc21',
158 marc_type => 'marc21',
159 marc_field => '020a',
168 marc_type => 'marc21',
169 marc_field => '100a',
178 marc_type => 'marc21',
179 marc_field => '110a',
188 marc_type => 'marc21',
189 marc_field => '245(ab)ab',
192 name => 'unimarc_title',
198 marc_type => 'unimarc',
199 marc_field => '245a',
205 suggestible => undef,
208 marc_type => 'marc21',
212 name => 'uniform_title',
218 marc_type => 'marc21',
219 marc_field => '240a',
222 name => 'title_wildcard',
228 marc_type => 'marc21',
232 name => 'sum_item_price',
238 marc_type => 'marc21',
239 marc_field => '952g',
242 name => 'items_withdrawn_status',
248 marc_type => 'marc21',
249 marc_field => '9520',
252 name => 'local_classification',
258 marc_type => 'marc21',
259 marc_field => '952o',
262 name => 'type_of_record',
268 marc_type => 'marc21',
269 marc_field => 'leader_/6',
272 name => 'type_of_record_and_bib_level',
278 marc_type => 'marc21',
279 marc_field => 'leader_/6-7',
288 marc_type => 'marc21',
289 marc_field => '007_/0',
298 marc_type => 'marc21',
299 marc_field => '952l',
303 my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
304 $se->mock('_foreach_mapping', sub {
305 my ($self, $sub) = @_;
307 foreach my $map (@mappings) {
321 my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
323 my $callno = 'ABC123';
324 my $callno2 = 'ABC456';
325 my $long_callno = '1234567890' x 30;
327 my $marc_record_1 = MARC::Record->new();
328 $marc_record_1->leader(' cam 22 a 4500');
329 $marc_record_1->append_fields(
330 MARC::Field->new('001', '123'),
331 MARC::Field->new('007', 'ku'),
332 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
333 MARC::Field->new('100', '', '', a => 'Author 1'),
334 MARC::Field->new('110', '', '', a => 'Corp Author'),
335 MARC::Field->new('210', '', '', a => 'Title 1'),
336 MARC::Field->new('240', '', '4', a => 'The uniform title with nonfiling indicator'),
337 MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
338 MARC::Field->new('999', '', '', c => '1234567'),
339 # ' ' for testing trimming of white space in boolean value callback:
340 MARC::Field->new('952', '', '', 0 => ' ', g => '123.30', o => $callno, l => 3),
341 MARC::Field->new('952', '', '', 0 => 0, g => '127.20', o => $callno2, l => 2),
342 MARC::Field->new('952', '', '', 0 => 1, g => '0.00', o => $long_callno, l => 1),
344 my $marc_record_2 = MARC::Record->new();
345 $marc_record_2->leader(' cam 22 a 4500');
346 $marc_record_2->append_fields(
347 MARC::Field->new('100', '', '', a => 'Author 2'),
348 # MARC::Field->new('210', '', '', a => 'Title 2'),
349 # MARC::Field->new('245', '', '', a => 'Title: second record'),
350 MARC::Field->new('999', '', '', c => '1234568'),
351 MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric', o => $long_callno),
353 my $records = [ $marc_record_1, $marc_record_2 ];
355 $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
357 my $docs = $see->marc_records_to_documents($records);
360 is(scalar @{$docs}, 2, 'Two records converted to documents');
362 is_deeply($docs->[0]->{control_number}, ['123'], 'First record control number should be set correctly');
364 is_deeply($docs->[0]->{'ff7-00'}, ['k'], 'First record ff7-00 should be set correctly');
366 is(scalar @{$docs->[0]->{author}}, 2, 'First document author field should contain two values');
367 is_deeply($docs->[0]->{author}, ['Author 1', 'Corp Author'], 'First document author field should be set correctly');
369 is(scalar @{$docs->[0]->{author__sort}}, 1, 'First document author__sort field should have a single value');
370 is_deeply($docs->[0]->{author__sort}, ['Author 1 Corp Author'], 'First document author__sort field should be set correctly');
372 is(scalar @{$docs->[0]->{title__sort}}, 1, 'First document title__sort field should have a single');
373 is_deeply($docs->[0]->{title__sort}, ['Title: first record Title: first record'], 'First document title__sort field should be set correctly');
375 is($docs->[0]->{issues}, 6, 'Issues field should be sum of the issues for each item');
376 is($docs->[0]->{issues__sort}, 6, 'Issues sort field should also be a sum of the issues');
378 is(scalar @{$docs->[0]->{title_wildcard}}, 2, 'First document title_wildcard field should have two values');
379 is_deeply($docs->[0]->{title_wildcard}, ['Title:', 'first record'], 'First document title_wildcard field should be set correctly');
382 is(scalar @{$docs->[0]->{author__suggestion}}, 2, 'First document author__suggestion field should contain two values');
384 $docs->[0]->{author__suggestion},
387 'input' => 'Author 1'
390 'input' => 'Corp Author'
393 'First document author__suggestion field should be set correctly'
396 is(scalar @{$docs->[0]->{title__suggestion}}, 3, 'First document title__suggestion field should contain three values');
398 $docs->[0]->{title__suggestion},
400 { 'input' => 'Title:' },
401 { 'input' => 'first record' },
402 { 'input' => 'Title: first record' }
404 'First document title__suggestion field should be set correctly'
407 ok(!(defined $docs->[0]->{title__facet}), 'First document should have no title__facet field');
409 is(scalar @{$docs->[0]->{author__facet}}, 2, 'First document author__facet field should have two values');
411 $docs->[0]->{author__facet},
412 ['Author 1', 'Corp Author'],
413 'First document author__facet field should be set correctly'
416 is(scalar @{$docs->[0]->{items_withdrawn_status}}, 2, 'First document items_withdrawn_status field should have two values');
418 $docs->[0]->{items_withdrawn_status},
420 'First document items_withdrawn_status field should be set correctly'
424 $docs->[0]->{sum_item_price},
426 'First document sum_item_price field should be set correctly'
429 ok(defined $docs->[0]->{marc_data}, 'First document marc_data field should be set');
430 ok(defined $docs->[0]->{marc_format}, 'First document marc_format field should be set');
431 is($docs->[0]->{marc_format}, 'base64ISO2709', 'First document marc_format should be set correctly');
433 my $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
435 ok($decoded_marc_record->isa('MARC::Record'), "base64ISO2709 record successfully decoded from result");
436 is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded base64ISO2709 record has same data as original record");
438 is(scalar @{$docs->[0]->{type_of_record}}, 1, 'First document type_of_record field should have one value');
440 $docs->[0]->{type_of_record},
442 'First document type_of_record field should be set correctly'
445 is(scalar @{$docs->[0]->{type_of_record_and_bib_level}}, 1, 'First document type_of_record_and_bib_level field should have one value');
447 $docs->[0]->{type_of_record_and_bib_level},
449 'First document type_of_record_and_bib_level field should be set correctly'
452 is(scalar @{$docs->[0]->{isbn}}, 4, 'First document isbn field should contain four values');
453 is_deeply($docs->[0]->{isbn}, ['978-1-56619-909-4', '9781566199094', '1-56619-909-3', '1566199093'], 'First document isbn field should be set correctly');
456 $docs->[0]->{'local_classification'},
457 [$callno, $callno2, $long_callno],
458 'First document local_classification field should be set correctly'
461 # Nonfiling characters for sort fields
463 $docs->[0]->{uniform_title},
464 ['The uniform title with nonfiling indicator'],
465 'First document uniform_title field should contain the title verbatim'
468 $docs->[0]->{uniform_title__sort},
469 ['uniform title with nonfiling indicator'],
470 'First document uniform_title__sort field should contain the title with the first four initial characters removed'
475 is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
476 is_deeply($docs->[1]->{author}, ['Author 2'], 'Second document author field should be set correctly');
478 is(scalar @{$docs->[1]->{items_withdrawn_status}}, 1, 'Second document items_withdrawn_status field should have one value');
480 $docs->[1]->{items_withdrawn_status},
482 'Second document items_withdrawn_status field should be set correctly'
486 $docs->[1]->{sum_item_price},
488 'Second document sum_item_price field should be set correctly'
492 $docs->[1]->{local_classification__sort},
493 [substr($long_callno, 0, 255)],
494 'Second document local_classification__sort field should be set correctly'
497 # Mappings marc_type:
499 ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
501 # Marc serialization format fallback for records exceeding ISO2709 max record size
503 my $large_marc_record = MARC::Record->new();
504 $large_marc_record->leader(' cam 22 a 4500');
506 $large_marc_record->append_fields(
507 MARC::Field->new('100', '', '', a => 'Author 1'),
508 MARC::Field->new('110', '', '', a => 'Corp Author'),
509 MARC::Field->new('210', '', '', a => 'Title 1'),
510 MARC::Field->new('245', '', '', a => 'Title:', b => 'large record'),
511 MARC::Field->new('999', '', '', c => '1234567'),
514 my $item_field = MARC::Field->new('952', '', '', o => '123456789123456789123456789', p => '123456789', z => 'test');
515 my $items_count = 1638;
516 while(--$items_count) {
517 $large_marc_record->append_fields($item_field);
520 $docs = $see->marc_records_to_documents([$large_marc_record]);
522 is($docs->[0]->{marc_format}, 'MARCXML', 'For record exceeding max record size marc_format should be set correctly');
524 $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
526 ok($decoded_marc_record->isa('MARC::Record'), "MARCXML record successfully decoded from result");
527 is($decoded_marc_record->as_xml_record(), $large_marc_record->as_xml_record(), "Decoded MARCXML record has same data as original record");
535 marc_type => 'marc21',
536 marc_field => '245((ab)ab',
539 my $exception = try {
540 $see->marc_records_to_documents($records);
546 ok(defined $exception, "Exception has been thrown when processing mapping with unmatched opening parenthesis");
547 ok($exception->isa("Koha::Exceptions::Elasticsearch::MARCFieldExprParseError"), "Exception is of correct class");
548 ok($exception->message =~ /Unmatched opening parenthesis/, "Exception has the correct message");
557 marc_type => 'marc21',
558 marc_field => '245(ab))ab',
562 $see->marc_records_to_documents($records);
568 ok(defined $exception, "Exception has been thrown when processing mapping with unmatched closing parenthesis");
569 ok($exception->isa("Koha::Exceptions::Elasticsearch::MARCFieldExprParseError"), "Exception is of correct class");
570 ok($exception->message =~ /Unmatched closing parenthesis/, "Exception has the correct message");
573 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents_array () tests' => sub {
577 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
578 t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ARRAY');
582 name => 'control_number',
588 marc_type => 'marc21',
593 my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
594 $se->mock('_foreach_mapping', sub {
595 my ($self, $sub) = @_;
597 foreach my $map (@mappings) {
611 my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
613 my $marc_record_1 = MARC::Record->new();
614 $marc_record_1->leader(' cam 22 a 4500');
615 $marc_record_1->append_fields(
616 MARC::Field->new('001', '123'),
617 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
618 MARC::Field->new('100', '', '', a => 'Author 1'),
619 MARC::Field->new('110', '', '', a => 'Corp Author'),
620 MARC::Field->new('210', '', '', a => 'Title 1'),
621 MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
622 MARC::Field->new('999', '', '', c => '1234567'),
624 my $marc_record_2 = MARC::Record->new();
625 $marc_record_2->leader(' cam 22 a 4500');
626 $marc_record_2->append_fields(
627 MARC::Field->new('100', '', '', a => 'Author 2'),
628 # MARC::Field->new('210', '', '', a => 'Title 2'),
629 # MARC::Field->new('245', '', '', a => 'Title: second record'),
630 MARC::Field->new('999', '', '', c => '1234568'),
631 MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric'),
633 my $records = [ $marc_record_1, $marc_record_2 ];
635 $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
637 my $docs = $see->marc_records_to_documents($records);
640 is(scalar @{$docs}, 2, 'Two records converted to documents');
642 is_deeply($docs->[0]->{control_number}, ['123'], 'First record control number should be set correctly');
644 is($docs->[0]->{marc_format}, 'ARRAY', 'First document marc_format should be set correctly');
646 my $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
648 ok($decoded_marc_record->isa('MARC::Record'), "ARRAY record successfully decoded from result");
649 is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded ARRAY record has same data as original record");
652 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () authority tests' => sub {
656 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
657 t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
659 my $builder = t::lib::TestBuilder->new;
660 my $auth_type = $builder->build_object({ class => 'Koha::Authority::Types', value =>{
661 auth_tag_to_report => '150'
673 marc_type => 'marc21',
674 marc_field => '150(ae)',
678 my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
679 $se->mock('_foreach_mapping', sub {
680 my ($self, $sub) = @_;
682 foreach my $map (@mappings) {
696 my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX });
697 my $marc_record_1 = MARC::Record->new();
698 $marc_record_1->append_fields(
699 MARC::Field->new('001', '123'),
700 MARC::Field->new('007', 'ku'),
701 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
702 MARC::Field->new('150', '', '', a => 'Subject', v => 'Genresubdiv', x => 'Generalsubdiv', z => 'Geosubdiv'),
704 my $marc_record_2 = MARC::Record->new();
705 $marc_record_2->append_fields(
706 MARC::Field->new('150', '', '', a => 'Subject', v => 'Genresubdiv', z => 'Geosubdiv', x => 'Generalsubdiv', e => 'wrongsubdiv' ),
708 my $records = [ $marc_record_1, $marc_record_2 ];
710 $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
712 my $docs = $see->marc_records_to_documents($records);
715 any { $_ eq "Subject formsubdiv Genresubdiv generalsubdiv Generalsubdiv geographicsubdiv Geosubdiv" }
716 @{$docs->[0]->{'match-heading'}},
717 "First record match-heading should contain the correctly formatted heading"
720 any { $_ eq "Subject formsubdiv Genresubdiv geographicsubdiv Geosubdiv generalsubdiv Generalsubdiv" }
721 @{$docs->[1]->{'match-heading'}},
722 "Second record match-heading should contain the correctly formatted heading without wrong subfield"
726 $schema->storage->txn_rollback;