Bug 23204: Add script for exporting Elasticsearch mappings
[koha.git] / misc / search_tools / export_elasticsearch_mappings.pl
1 #!/usr/bin/perl
2
3 # This inserts records from a Koha database into elastic search
4
5 # Copyright 2014 Catalyst IT
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 =head1 NAME
23
24 export_elasticsearch_mappings.pl - export search_marc_map, search_marc_to_field and search_field tables to YAML
25
26 =head1 SYNOPSIS
27
28 B<export_elasticsearch_mappings.pl>
29 [B<-t|--type>]
30 [B<--man>]
31
32 =head1 DESCRIPTION
33
34 Export search_marc_map, search_marc_to_field and search_field tables to YAML.
35
36 =head1 OPTIONS
37
38 =over
39
40 =item B<-t|--type>=C<marc21|unimarc|normarc>
41
42 Only export a specific marc type. All if empty.
43
44 =item B<--man>
45
46 Full documentation.
47
48 =back
49
50 =head1 IMPLEMENTATION
51
52 =cut
53
54 use Modern::Perl;
55
56 use Koha::Database;
57 use Koha::SearchFields;
58 use Koha::SearchMarcMaps;
59
60 use YAML;
61 use Getopt::Long;
62 use Pod::Usage;
63
64 my $type = '';
65 my $man;
66
67 GetOptions(
68     't|type=s'  => \$type,
69     'man'       => \$man,
70 );
71
72 pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
73
74 if ( $type && $type !~ /^(marc21|unimarc|normarc)$/ ) {
75     print "Bad marc type provided.\n";
76     pod2usage(1);
77 }
78
79 my $schema = Koha::Database->new()->schema();
80
81 my $search_fields = Koha::SearchFields->search();
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);