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

(-)a/Koha/SearchEngine/Elasticsearch.pm (-23 / +2 lines)
Lines 26-31 use Koha::Exceptions::Config; Link Here
26
use Koha::Exceptions::Elasticsearch;
26
use Koha::Exceptions::Elasticsearch;
27
use Koha::SearchFields;
27
use Koha::SearchFields;
28
use Koha::SearchMarcMaps;
28
use Koha::SearchMarcMaps;
29
use Koha::SearchMappingManager;
29
use C4::Heading;
30
use C4::Heading;
30
31
31
use Carp;
32
use Carp;
Lines 1130-1158 sub _foreach_mapping { Link Here
1130
    my ( $self, $sub ) = @_;
1131
    my ( $self, $sub ) = @_;
1131
1132
1132
    # TODO use a caching framework here
1133
    # TODO use a caching framework here
1133
    my $search_fields = Koha::Database->schema->resultset('SearchField')->search(
1134
    my $search_fields = Koha::SearchMappingManager::get_search_mappings();
1134
        {
1135
            'search_marc_map.index_name' => $self->index,
1136
        },
1137
        {   join => { search_marc_to_fields => 'search_marc_map' },
1138
            '+select' => [
1139
                'search_marc_to_fields.facet',
1140
                'search_marc_to_fields.suggestible',
1141
                'search_marc_to_fields.sort',
1142
                'search_marc_to_fields.search',
1143
                'search_marc_map.marc_type',
1144
                'search_marc_map.marc_field',
1145
            ],
1146
            '+as'     => [
1147
                'facet',
1148
                'suggestible',
1149
                'sort',
1150
                'search',
1151
                'marc_type',
1152
                'marc_field',
1153
            ],
1154
        }
1155
    );
1156
1135
1157
    while ( my $search_field = $search_fields->next ) {
1136
    while ( my $search_field = $search_fields->next ) {
1158
        $sub->(
1137
        $sub->(
(-)a/Koha/SearchMappingManager.pm (+151 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_to_fields.search',
82
                'search_marc_map.marc_type',
83
                'search_marc_map.marc_field',
84
            ],
85
            '+as'     => [
86
                'facet',
87
                'suggestible',
88
                'sort',
89
                'search',
90
                'marc_type',
91
                'marc_field',
92
            ],
93
        }
94
    );
95
}
96
97
=head2 flush
98
99
    Koha::SearchMappingManager::flush();
100
101
Removes all entries from the Koha's search mapping tables
102
103
=cut
104
105
sub flush {
106
    my $schema = Koha::Database->schema;
107
    $schema->resultset('SearchField')->delete_all();
108
    $schema->resultset('SearchMarcMap')->delete_all();
109
    $schema->resultset('SearchMarcToField')->delete_all();
110
}
111
112
113
=head2 add_mapping
114
115
    Koha::SearchMappingManager::add_mapping({name => 'ln-test',
116
                                             label => 'original language',
117
                                             type => 'keyword',
118
                                             index_name => 'biblios',
119
                                             marc_type => 'marc21',
120
                                             marc_field => '024a',
121
                                             facet => 1,
122
                                             suggestible => 1,
123
                                             sort => 1});
124
125
=cut
126
127
sub add_mapping {
128
    my ($params) = @_;
129
130
    my $search_field = Koha::SearchFields->find_or_create({  name =>  $params->{name} ,
131
                                                             label => $params->{label},
132
                                                             type =>  $params->{type} ,},
133
                                                          { key => 'name' });
134
135
    my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $params->{index_name},
136
                                                            marc_type  => $params->{marc_type} ,
137
                                                            marc_field => $params->{marc_field}, });
138
139
    ##update_or_create
140
    my $p = {
141
        search_field_id => $search_field->id,
142
        search_marc_map_id => $marc_field->id,
143
    };
144
    $p->{facet}       = $params->{facet}       if $params->{facet};
145
    $p->{suggestible} = $params->{suggestible} if $params->{suggestible};
146
    $p->{sort}        = $params->{sort}        if $params->{sort};
147
    $p->{search}        = $params->{search}        if $params->{search};
148
    Koha::Database->schema->resultset('SearchMarcToField')->update_or_create($p);
149
}
150
151
1;
(-)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('search'),     '1',   'And the search mapping "search" matches');
88
    is($mapping->get_column('marc_type'),  'marc21', 'And the search mapping "marc_type" matches');
89
    is($mapping->get_column('marc_field'), '007_/0', 'And the search mapping "marc_field" matches');
90
91
    ok(1, 'Scenario: Get all search mappings');
92
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios'});
93
    ok($mappings, 'When search mappings are fetched');
94
    ok($mappings->count() > 10, 'Then we have "'.$mappings->count().'" search mappings :)')
95
}
96
97
subtest "Add a search mapping", \&add_mapping;
98
sub add_mapping {
99
    my ($rv, $mappings, $mapping, $count);
100
101
    ok(1, "Scenario: Add the same mapping twice and hope for no duplicate mappings");
102
    $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
103
                                                   label => 'original language',
104
                                                   type => 'keyword',
105
                                                   index_name => 'biblios',
106
                                                   marc_type => 'marc21',
107
                                                   marc_field => '024a',
108
                                                   facet => 1,
109
                                                   suggestible => 1,
110
                                                   sort => 1});
111
    $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
112
                                                   label => 'original language',
113
                                                   type => 'keyword',
114
                                                   index_name => 'biblios',
115
                                                   marc_type => 'marc21',
116
                                                   marc_field => '024a',
117
                                                   facet => 1,
118
                                                   suggestible => 1,
119
                                                   sort => 1});
120
    ok(1, "When the same search mapping is added twice");
121
122
    $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ln-test'});
123
    $count = $mappings->count();
124
    is($count, 1, "Then we received only one mapping from the database");
125
}
126
127
done_testing;

Return to bug 17897