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

(-)a/Koha/SearchEngine/Elasticsearch.pm (+45 lines)
Lines 244-249 sub get_elasticsearch_mappings { Link Here
244
    return $all_mappings{$self->index};
244
    return $all_mappings{$self->index};
245
}
245
}
246
246
247
=head2 raw_elasticsearch_mappings
248
249
Return elasticsearch mapping as it is in database.
250
marc_type: marc21|unimarc|normarc
251
252
$raw_mappings = raw_elasticsearch_mappings( $marc_type )
253
254
=cut
255
256
sub raw_elasticsearch_mappings {
257
    my ( $marc_type ) = @_;
258
259
    my $schema = Koha::Database->new()->schema();
260
261
    my $search_fields = Koha::SearchFields->search();
262
263
    my $mappings = {};
264
    while ( my $search_field = $search_fields->next ) {
265
266
        my $marc_to_fields = $schema->resultset('SearchMarcToField')->search( { search_field_id => $search_field->id } );
267
268
        while ( my $marc_to_field = $marc_to_fields->next ) {
269
            my $marc_map = Koha::SearchMarcMaps->find( $marc_to_field->search_marc_map_id );
270
271
            next if $marc_type && $marc_map->marc_type ne $marc_type;
272
273
            $mappings->{ $marc_map->index_name }{ $search_field->name }{label} = $search_field->label;
274
            $mappings->{ $marc_map->index_name }{ $search_field->name }{type} = $search_field->type;
275
            $mappings->{ $marc_map->index_name }{ $search_field->name }{facet_order} = $search_field->facet_order;
276
277
            push (@{ $mappings->{ $marc_map->index_name }{ $search_field->name }{mappings} },
278
                {
279
                    facet   => $marc_to_field->facet || '',
280
                    marc_type => $marc_map->marc_type,
281
                    marc_field => $marc_map->marc_field,
282
                    sort        => $marc_to_field->sort,
283
                    suggestible => $marc_to_field->suggestible || ''
284
                });
285
286
        }
287
    }
288
289
    return $mappings;
290
}
291
247
=head2 _get_elasticsearch_field_config
292
=head2 _get_elasticsearch_field_config
248
293
249
Get the Elasticsearch field config for the given purpose and data type.
294
Get the Elasticsearch field config for the given purpose and data type.
(-)a/misc/search_tools/export_elasticsearch_mappings.pl (-30 / +3 lines)
Lines 56-61 use Modern::Perl; Link Here
56
use Koha::Database;
56
use Koha::Database;
57
use Koha::SearchFields;
57
use Koha::SearchFields;
58
use Koha::SearchMarcMaps;
58
use Koha::SearchMarcMaps;
59
use Koha::SearchEngine::Elasticsearch;
59
60
60
use YAML;
61
use YAML;
61
use Getopt::Long;
62
use Getopt::Long;
Lines 76-109 if ( $type && $type !~ /^(marc21|unimarc|normarc)$/ ) { Link Here
76
    pod2usage(1);
77
    pod2usage(1);
77
}
78
}
78
79
79
my $schema = Koha::Database->new()->schema();
80
my $mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings( $type );
80
81
81
my $search_fields = Koha::SearchFields->search();
82
print Dump($mappings);
82
83
my $yaml = {};
84
while ( my $search_field = $search_fields->next ) {
85
86
    my $marc_to_fields = $schema->resultset('SearchMarcToField')->search( { search_field_id => $search_field->id } );
87
88
    while ( my $marc_to_field = $marc_to_fields->next ) {
89
        my $marc_map = Koha::SearchMarcMaps->find( $marc_to_field->search_marc_map_id );
90
91
        next if $type && $marc_map->marc_type ne $type;
92
93
        $yaml->{ $marc_map->index_name }{ $search_field->name }{label} = $search_field->label;
94
        $yaml->{ $marc_map->index_name }{ $search_field->name }{type} = $search_field->type;
95
        $yaml->{ $marc_map->index_name }{ $search_field->name }{facet_order} = $search_field->facet_order;
96
97
        push (@{ $yaml->{ $marc_map->index_name }{ $search_field->name }{mappings} },
98
            {
99
                facet   => $marc_to_field->facet || '',
100
                marc_type => $marc_map->marc_type,
101
                marc_field => $marc_map->marc_field,
102
                sort        => $marc_to_field->sort,
103
                suggestible => $marc_to_field->suggestible || ''
104
            });
105
106
    }
107
}
108
109
print Dump($yaml);
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/ExportConfig.t (-1 / +116 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
4
#
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.
9
#
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.
14
#
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>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 16;
21
22
use Koha::Database;
23
use Koha::SearchFields;
24
use Koha::SearchMarcMaps;
25
26
use_ok('Koha::SearchEngine::Elasticsearch');
27
28
my $schema = Koha::Database->new->schema;
29
30
$schema->storage->txn_begin;
31
32
Koha::SearchFields->search->delete;
33
Koha::SearchMarcMaps->search->delete;
34
$schema->resultset('SearchMarcToField')->search->delete;
35
36
37
38
my $search_field = Koha::SearchFields->find_or_create(
39
    {
40
        name    => 'title',
41
        label   => 'Title',
42
        type    => 'string',
43
        weight  => 17
44
45
    },
46
    { key => 'name' } );
47
48
my $marc_field = Koha::SearchMarcMaps->find_or_create(
49
    {
50
        index_name => 'biblios',
51
        marc_type => 'marc21',
52
        marc_field => '247'
53
    } );
54
55
$search_field->add_to_search_marc_maps($marc_field,
56
    {
57
        facet => 0,
58
        suggestible => 0,
59
        sort => undef
60
    } );
61
62
$marc_field = Koha::SearchMarcMaps->find_or_create(
63
    {
64
        index_name => 'biblios',
65
        marc_type => 'marc21',
66
        marc_field => '212'
67
    } );
68
69
$search_field->add_to_search_marc_maps($marc_field,
70
    {
71
        facet => 0,
72
        suggestible => 0,
73
        sort => undef
74
    } );
75
76
$marc_field = Koha::SearchMarcMaps->find_or_create(
77
    {
78
        index_name => 'biblios',
79
        marc_type => 'unimarc',
80
        marc_field => '200a'
81
    } );
82
83
$search_field->add_to_search_marc_maps($marc_field,
84
    {
85
        facet => 0,
86
        suggestible => 1,
87
        sort => undef
88
    } );
89
90
my $mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings();
91
92
is( $mappings->{biblios}{title}{type}, 'string', 'Title is of type string');
93
is( $mappings->{biblios}{title}{label}, 'Title', 'title has label Title');
94
is( $mappings->{biblios}{title}{facet_order}, undef, 'Facet order is undef');
95
96
is(scalar(@{ $mappings->{biblios}{title}{mappings} }), 3, 'Title has 3 mappings');
97
98
my $f247_map = $mappings->{biblios}{title}{mappings}[0];
99
is( $f247_map->{marc_field}, 247, 'First mapping is on field 247');
100
is( $f247_map->{marc_type}, 'marc21', 'First mapping is for marc21');
101
is( $f247_map->{facet}, '', 'First mapping facet is empty');
102
is( $f247_map->{suggestible}, '', 'First mapping is not suggestible');
103
is( $f247_map->{sort}, undef, 'First mapping is not sortable');
104
105
my $f212_map = $mappings->{biblios}{title}{mappings}[1];
106
is( $f212_map->{marc_field}, 212, 'Second mapping is on field 247');
107
is( $f212_map->{marc_type}, 'marc21', 'Second mapping is for marc21');
108
is( $f212_map->{facet}, '', 'Second mapping facet is empty');
109
is( $f212_map->{suggestible}, '', 'Second mapping is not suggestible');
110
is( $f212_map->{sort}, undef, 'Second mapping is not sortable');
111
112
$mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings('unimarc');
113
114
is(scalar(@{ $mappings->{biblios}{title}{mappings} }), 1, 'Title has 1 mappings');
115
116
$schema->storage->txn_rollback;

Return to bug 23204