View | Details | Raw Unified | Return to bug 17885
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch.pm (-22 / +17 lines)
Lines 24-35 use C4::Context; Link Here
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::SearchFields;
25
use Koha::SearchFields;
26
use Koha::SearchMarcMaps;
26
use Koha::SearchMarcMaps;
27
use Koha::SearchMappingManager;
27
28
28
use Carp;
29
use Carp;
29
use JSON;
30
use JSON;
30
use Modern::Perl;
31
use Modern::Perl;
31
use Readonly;
32
use Readonly;
32
use YAML::Syck;
33
use YAML::Syck;
34
use Try::Tiny;
35
use Scalar::Util qw(blessed);
33
36
34
use Data::Dumper;    # TODO remove
37
use Data::Dumper;    # TODO remove
35
38
Lines 278-283 sub _elasticsearch_mapping_for_default { Link Here
278
sub reset_elasticsearch_mappings {
281
sub reset_elasticsearch_mappings {
279
    my $mappings_yaml = C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
282
    my $mappings_yaml = C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml';
280
    my $indexes = LoadFile( $mappings_yaml );
283
    my $indexes = LoadFile( $mappings_yaml );
284
    my $marcflavour = C4::Context->preference('marcflavour') or die "reset_elasticsearch_mappings():> System preferene 'marcflavour not set'";
281
285
282
    while ( my ( $index_name, $fields ) = each %$indexes ) {
286
    while ( my ( $index_name, $fields ) = each %$indexes ) {
283
        while ( my ( $field_name, $data ) = each %$fields ) {
287
        while ( my ( $field_name, $data ) = each %$fields ) {
Lines 286-293 sub reset_elasticsearch_mappings { Link Here
286
            my $mappings = $data->{mappings};
290
            my $mappings = $data->{mappings};
287
            my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, label => $field_label, type => $field_type }, { key => 'name' });
291
            my $search_field = Koha::SearchFields->find_or_create({ name => $field_name, label => $field_label, type => $field_type }, { key => 'name' });
288
            for my $mapping ( @$mappings ) {
292
            for my $mapping ( @$mappings ) {
293
                next unless (lc($marcflavour) eq lc($mapping->{marc_type})); #Don't push all marc mappings for marc flavours we don't need
294
289
                my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} });
295
                my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} });
290
                $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping->{facet}, suggestible => $mapping->{suggestible}, sort => $mapping->{sort} } );
296
297
                ##update_or_create
298
                my $p = {
299
                    search_field_id => $search_field->id,
300
                    search_marc_map_id => $marc_field->id,
301
                };
302
                $p->{facet}       = $mapping->{facet}       if $mapping->{facet};
303
                $p->{suggestible} = $mapping->{suggestible} if $mapping->{suggestible};
304
                $p->{sort}        = $mapping->{sort}        if $mapping->{sort};
305
                Koha::Database->schema->resultset('SearchMarcToField')->update_or_create($p);
291
            }
306
            }
292
        }
307
        }
293
    }
308
    }
Lines 421-447 sub _foreach_mapping { Link Here
421
    my ( $self, $sub ) = @_;
436
    my ( $self, $sub ) = @_;
422
437
423
    # TODO use a caching framework here
438
    # TODO use a caching framework here
424
    my $search_fields = Koha::Database->schema->resultset('SearchField')->search(
439
    my $search_fields = Koha::SearchMappingManager::get_search_mappings();
425
        {
426
            'search_marc_map.index_name' => $self->index,
427
        },
428
        {   join => { search_marc_to_fields => 'search_marc_map' },
429
            '+select' => [
430
                'search_marc_to_fields.facet',
431
                'search_marc_to_fields.suggestible',
432
                'search_marc_to_fields.sort',
433
                'search_marc_map.marc_type',
434
                'search_marc_map.marc_field',
435
            ],
436
            '+as'     => [
437
                'facet',
438
                'suggestible',
439
                'sort',
440
                'marc_type',
441
                'marc_field',
442
            ],
443
        }
444
    );
445
440
446
    while ( my $search_field = $search_fields->next ) {
441
    while ( my $search_field = $search_fields->next ) {
447
        $sub->(
442
        $sub->(
(-)a/Koha/SearchMappingManager.pm (+149 lines)
Line 0 Link Here
1
package Koha::SearchMappingManager;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
use Modern::Perl;
19
20
use Carp;
21
22
use Koha::Database;
23
24
=head1 NAME
25
26
Koha::SearchMappingManager - Manager for operating on search field mappings
27
28
=head1 SYNOPSIS
29
30
This class helps to interface with the complex internals of the koha.search_*-tables
31
and their respective Koha::Objects in a correct manner.
32
33
=cut
34
35
36
37
38
=head2 get_search_mappings
39
40
    my $search_fields = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios|authorities'});
41
42
    while ( my $search_field = $search_fields->next ) {
43
        $sub->(
44
            $search_field->get_column('name'),
45
            $search_field->get_column('type'),
46
            $search_field->get_column('facet'),
47
            $search_field->get_column('suggestible'),
48
            $search_field->get_column('sort'),
49
            $search_field->get_column('marc_type'),
50
            $search_field->get_column('marc_field'),
51
        );
52
    }
53
54
Get each entry from the searchengine mappings tables.
55
56
@PARAMS HASHRef of keys:
57
        index_name => 'biblios|authorities' #Which Koha record types to look for
58
        name       => 'title'               #The koha.search_fields.name
59
@RETURNS Koha::Schma::Resultset::SearchField with enriched fields from other relevant tables
60
@THROWS die when parameters are not properly given. Should throw a Koha::Exception::BadParameter,
61
            but pushing all those Exceptions to the community version would take ages.
62
63
=cut
64
65
sub get_search_mappings {
66
    my ($params) = @_;
67
    die "get_search_mappings():> parameter 'index_name' is missing" unless ($params->{index_name});
68
69
    my $search = {
70
        'search_marc_map.index_name' => $params->{index_name},
71
    };
72
    $search->{'me.name'} = $params->{name} if $params->{name};
73
74
    return Koha::Database->schema->resultset('SearchField')->search(
75
        $search,
76
        {   join => { search_marc_to_fields => 'search_marc_map' },
77
            '+select' => [
78
                'search_marc_to_fields.facet',
79
                'search_marc_to_fields.suggestible',
80
                'search_marc_to_fields.sort',
81
                'search_marc_map.marc_type', 
82
                'search_marc_map.marc_field',
83
            ],
84
            '+as'     => [
85
                'facet',  
86
                'suggestible',
87
                'sort',
88
                'marc_type',
89
                'marc_field',
90
            ],
91
        }
92
    );
93
}
94
95
=head2 flush
96
97
    Koha::SearchMappingManager::flush();
98
99
Removes all entries from the Koha's search mapping tables
100
101
=cut
102
103
sub flush {
104
    my $schema = Koha::Database->schema;
105
    $schema->resultset('SearchField')->delete_all();
106
    $schema->resultset('SearchMarcMap')->delete_all();
107
    $schema->resultset('SearchMarcToField')->delete_all();
108
}
109
110
111
=head2 add_mapping
112
113
    Koha::SearchMappingManager::add_mapping({name => 'ln-test',
114
                                             label => 'original language',
115
                                             type => 'keyword',
116
                                             index_name => 'biblios',
117
                                             marc_type => 'marc21',
118
                                             marc_field => '024a',
119
                                             facet => 1,
120
                                             suggestible => 1,
121
                                             sort => 1});
122
123
=cut
124
125
sub add_mapping {
126
    my ($params) = @_;
127
128
    my $search_field = Koha::SearchFields->find_or_create({  name =>  $params->{name} ,
129
                                                             label => $params->{label},
130
                                                             type =>  $params->{type} ,},
131
                                                          { key => 'name' });
132
133
    my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $params->{index_name},
134
                                                            marc_type  => $params->{marc_type} ,
135
                                                            marc_field => $params->{marc_field}, });
136
137
    ##update_or_create
138
    my $p = {
139
        search_field_id => $search_field->id,
140
        search_marc_map_id => $marc_field->id,
141
    };
142
    $p->{facet}       = $params->{facet}       if $params->{facet};
143
    $p->{suggestible} = $params->{suggestible} if $params->{suggestible};
144
    $p->{sort}        = $params->{sort}        if $params->{sort};
145
    Koha::Database->schema->resultset('SearchMarcToField')->update_or_create($p);
146
}
147
148
1;
149
(-)a/t/db_dependent/Koha/SearchEngine/ElasticSearch.t (-1 / +127 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2015 Catalyst IT
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl qw(2014);
21
use utf8;
22
use Test::More;
23
24
use Koha::SearchEngine::Elasticsearch;
25
use Koha::SearchMappingManager;
26
27
28
subtest "Reset Elasticsearch mappings", \&reset_elasticsearch_mappings;
29
sub reset_elasticsearch_mappings {
30
    my ($rv, $mappings, $count, $mapping);
31
32
    ok(1, 'Scenario: Reset Elasticsearch mappings to an empty database');
33
    #There might or might not be any mappings. Whatever the initial status is, make sure we start from empty tables
34
    $rv = Koha::SearchMappingManager::flush();
35
    ok($rv, 'Given empty search mapping tables');
36
    eval {
37
        $rv = Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
38
    };
39
    if ($@){
40
        ok(0, $@);
41
    }
42
    ok(not($@), 'When reset_elasticsearch_mappings() has been ran');
43
44
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'});
45
    $count = $mappings->count();
46
    ok($count, 'Then search mapping tables have been populated');
47
48
49
50
    ok(1, 'Scenario: Reset Elasticsearch mappings when custom mappings already exist');
51
    $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
52
                                                   label => 'original language',
53
                                                   type => 'keyword',
54
                                                   index_name => 'biblios',
55
                                                   marc_type => 'marc21',
56
                                                   marc_field => '024a',
57
                                                   facet => 1,
58
                                                   suggestible => 1,
59
                                                   sort => 1});
60
    ok($rv, 'Given a mapping table with a custom search field');
61
    eval {
62
        $rv = Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
63
    };
64
    if ($@){
65
        ok(0, $@);
66
    }
67
    ok(not($@), 'When reset_elasticsearch_mappings() has been ran');
68
69
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'});
70
    $count = $mappings->count();
71
    ok($count > 10, 'Then search mapping tables have been populated');
72
}
73
74
subtest "Get Elasticsearch mappings", \&get_search_mappings;
75
sub get_search_mappings {
76
    my ($mappings, $mapping);
77
78
    ok(1, 'Scenario: Get a single search mapping by name');
79
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ff7-00'});
80
    ok($mappings, 'When a search mappings is fetched');
81
    $mapping = $mappings->next();
82
    is($mapping->get_column('name'),       'ff7-00', 'Then the search mapping "name" matches');
83
    is($mapping->get_column('type'),       '',       'And the search mapping "type" matches');
84
    is($mapping->get_column('facet'),      '0',      'And the search mapping "facet" matches');
85
    is($mapping->get_column('suggestible'), '0',     'And the search mapping "suggestible" matches');
86
    is($mapping->get_column('sort'),        undef,   'And the search mapping "sort" matches');
87
    is($mapping->get_column('marc_type'),  'marc21', 'And the search mapping "marc_type" matches');
88
    is($mapping->get_column('marc_field'), '007_/1', 'And the search mapping "marc_field" matches');
89
90
    ok(1, 'Scenario: Get all search mappings');
91
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'});
92
    ok($mappings, 'When search mappings are fetched');
93
    ok($mappings->count() > 10, 'Then we have "'.$mappings->count().'" search mappings :)')
94
}
95
96
subtest "Add a search mapping", \&add_mapping;
97
sub add_mapping {
98
    my ($rv, $mappings, $mapping, $count);
99
100
    ok(1, "Scenario: Add the same mapping twice and hope for no duplicate mappings");
101
    $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
102
                                                   label => 'original language',
103
                                                   type => 'keyword',
104
                                                   index_name => 'biblios',
105
                                                   marc_type => 'marc21',
106
                                                   marc_field => '024a',
107
                                                   facet => 1,
108
                                                   suggestible => 1,
109
                                                   sort => 1});
110
    $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
111
                                                   label => 'original language',
112
                                                   type => 'keyword',
113
                                                   index_name => 'biblios',
114
                                                   marc_type => 'marc21',
115
                                                   marc_field => '024a',
116
                                                   facet => 1,
117
                                                   suggestible => 1,
118
                                                   sort => 1});
119
    ok(1, "When the same search mapping is added twice");
120
121
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ln-test'});
122
    $count = $mappings->count();
123
    is($count, 1, "Then we received only one mapping from the database");
124
}
125
126
done_testing;
127

Return to bug 17885