Koha/t/db_dependent/Koha_Elasticsearch_Indexer.t
Ere Maijala 23f78a6c2a Bug 20248: Improve Elasticsearch mappings UI and rebuild_elastic_search.pl.
Improvements:
1) Mappings UI now has button that allows one to reset the mappings.
2) Mappings UI displays the items in alphabetical order.
3) Indexing script drops and recreates the index right away, which
helps prevent ES from autocreating a bad index if someone does something
while the first batch of records is being processed.
4) Indexing script has nicer output.

To test:
1) Change mappings.yaml file
2) Reset mappings in UI in mappings.pl
3) Verify the mappings have been changed in UI
4) The field order is alphabetical
5) Rebuild script has clean output
6) Run test t/db_dependent/Koha_Elasticsearch_Indexer.t

Signed-off-by: Bouzid Fergani <bouzid.fergani@inlibro.com>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
2018-08-22 14:47:43 +00:00

165 lines
4.7 KiB
Perl

# Copyright 2015 Catalyst IT
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 7;
use Test::MockModule;
use t::lib::Mocks;
use MARC::Record;
use Koha::Database;
my $schema = Koha::Database->schema();
use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
my $indexer;
ok(
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
'Creating new indexer object'
);
my $marc_record = MARC::Record->new();
$marc_record->append_fields(
MARC::Field->new( '001', '1234567' ),
MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
MARC::Field->new( '245', '', '', 'a' => 'Title' )
);
my $records = [$marc_record];
ok( my $converted = $indexer->_convert_marc_to_json($records),
'Convert some records' );
is( $converted->count, 1, 'One converted record' );
SKIP: {
eval { $indexer->get_elasticsearch_params; };
skip 'Elasticsearch configuration not available', 1
if $@;
ok( $indexer->update_index(undef,$records), 'Update Index' );
}
subtest 'create_index() tests' => sub {
plan tests => 3;
my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
$se->mock( 'get_elasticsearch_params', sub {
my ($self, $sub ) = @_;
my $method = $se->original( 'get_elasticsearch_params' );
my $params = $method->( $self );
$params->{index_name} .= '__test';
return $params;
});
my $indexer;
ok(
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }),
'Creating a new indexer object'
);
ok(
$indexer->create_index(),
'Creating an index'
);
$indexer->drop_index();
ok(
$indexer->drop_index(),
'Dropping the index'
);
};
subtest '_convert_marc_to_json() tests' => sub {
plan tests => 4;
$schema->storage->txn_begin;
t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
my @mappings = (
{
name => 'author',
type => 'string',
facet => 1,
suggestible => 1,
sort => '~',
marc_type => 'marc21',
marc_field => '100a',
},
{
name => 'author',
type => 'string',
facet => 1,
suggestible => 1,
sort => '~',
marc_type => 'marc21',
marc_field => '110a',
},
);
my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
$se->mock( '_foreach_mapping', sub {
my ($self, $sub ) = @_;
foreach my $map ( @mappings ) {
$sub->(
$map->{name},
$map->{type},
$map->{facet},
$map->{suggestible},
$map->{sort},
$map->{marc_type},
$map->{marc_field}
);
}
});
my $marc_record = MARC::Record->new();
$marc_record->append_fields(
MARC::Field->new( '001', '1234567' ),
MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
MARC::Field->new( '100', '', '', 'a' => 'Author' ),
MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
MARC::Field->new( '245', '', '', 'a' => 'Title' ),
);
my $marc_record_2 = MARC::Record->new();
$marc_record_2->append_fields(
MARC::Field->new( '001', '1234567' ),
MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
MARC::Field->new( '100', '', '', 'a' => 'Author' ),
MARC::Field->new( '245', '', '', 'a' => 'Title' ),
);
my @records = ( $marc_record, $marc_record_2 );
my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' })->_convert_marc_to_json( \@records );
my $conv = $importer->next();
is( $conv->{author}[0], "Author", "First mapped author should be 100a");
is( $conv->{author}[1], "Corp Author", "Second mapped author should be 110a");
$conv = $importer->next();
is( $conv->{author}[0], "Author", "First mapped author should be 100a");
is( scalar @{$conv->{author}} , 1, "We should map field only if exists, shouldn't add extra nulls");
$schema->storage->txn_rollback;
};