e42cc38e29a470f7502dc1b4b3dd615b98f477f9
[koha.git] / admin / searchengine / elasticsearch / mappings.pl
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 use CGI;
20 use Scalar::Util qw(looks_like_number);
21 use C4::Koha;
22 use C4::Output;
23 use C4::Auth;
24
25 use Koha::SearchEngine::Elasticsearch;
26 use Koha::SearchMarcMaps;
27 use Koha::SearchFields;
28
29 my $input = new CGI;
30 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
31     {   template_name   => 'admin/searchengine/elasticsearch/mappings.tt',
32         query           => $input,
33         type            => 'intranet',
34         authnotrequired => 0,
35         flagsrequired   => { parameters => 'manage_search_engine_config' },
36     }
37 );
38
39 my $index = $input->param('index') || 'biblios';
40 my $op    = $input->param('op')    || 'list';
41 my @messages;
42
43 my $database = Koha::Database->new();
44 my $schema   = $database->schema;
45
46 my $marc_type = lc C4::Context->preference('marcflavour');
47
48 if ( $op eq 'edit' ) {
49
50     $schema->storage->txn_begin;
51
52     my @field_name = $input->param('search_field_name');
53     my @field_label = $input->param('search_field_label');
54     my @field_type = $input->param('search_field_type');
55     my @field_weight = $input->param('search_field_weight');
56
57     my @index_name          = $input->param('mapping_index_name');
58     my @search_field_name  = $input->param('mapping_search_field_name');
59     my @mapping_sort        = $input->param('mapping_sort');
60     my @mapping_facet       = $input->param('mapping_facet');
61     my @mapping_suggestible = $input->param('mapping_suggestible');
62     my @mapping_marc_field  = $input->param('mapping_marc_field');
63
64     eval {
65
66         for my $i ( 0 .. scalar(@field_name) - 1 ) {
67             my $field_name = $field_name[$i];
68             my $field_label = $field_label[$i];
69             my $field_type = $field_type[$i];
70             my $field_weight = $field_weight[$i];
71
72             my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
73             $search_field->label($field_label);
74             $search_field->type($field_type);
75
76             if (!length($field_weight)) {
77                 $search_field->weight(undef);
78             }
79             elsif ($field_weight <= 0 || !looks_like_number($field_weight)) {
80                 push @messages, { type => 'error', code => 'invalid_field_weight', 'weight' => $field_weight };
81             }
82             else {
83                 $search_field->weight($field_weight);
84             }
85
86             $search_field->store;
87         }
88
89         Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
90
91         for my $i ( 0 .. scalar(@index_name) - 1 ) {
92             my $index_name          = $index_name[$i];
93             my $search_field_name  = $search_field_name[$i];
94             my $mapping_marc_field  = $mapping_marc_field[$i];
95             my $mapping_facet       = $mapping_facet[$i];
96             my $mapping_suggestible = $mapping_suggestible[$i];
97             my $mapping_sort        = $mapping_sort[$i];
98             $mapping_sort = undef if $mapping_sort eq 'undef';
99
100             my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
101             # TODO Check mapping format
102             my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $marc_type, marc_field => $mapping_marc_field });
103             $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping_facet, suggestible => $mapping_suggestible, sort => $mapping_sort } );
104
105         }
106     };
107     if ($@) {
108         push @messages, { type => 'error', code => 'error_on_update', message => $@, };
109         $schema->storage->txn_rollback;
110     } else {
111         push @messages, { type => 'message', code => 'success_on_update' };
112         $schema->storage->txn_commit;
113     }
114 }
115 elsif( $op eq 'reset_confirmed' ) {
116     Koha::SearchMarcMaps->delete;
117     Koha::SearchFields->delete;
118     Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
119     push @messages, { type => 'message', code => 'success_on_reset' };
120 }
121 elsif( $op eq 'reset_confirm' ) {
122     $template->param( reset_confirm => 1 );
123 }
124
125
126 my @indexes;
127
128 for my $index_name (qw| biblios authorities |) {
129     my $search_fields = Koha::SearchFields->search(
130         { 'search_marc_map.index_name' => $index_name, 'search_marc_map.marc_type' => $marc_type, },
131         {   join => { search_marc_to_fields => 'search_marc_map' },
132             '+select' => [ 'search_marc_to_fields.facet', 'search_marc_to_fields.suggestible', 'search_marc_to_fields.sort', 'search_marc_map.marc_field' ],
133             '+as'     => [ 'facet',                       'suggestible',                       'sort',                       'marc_field' ],
134             order_by => { -asc => [qw/name marc_field/] }
135         }
136     );
137
138     my @mappings;
139     while ( my $s = $search_fields->next ) {
140         push @mappings,
141           { search_field_name  => $s->name,
142             search_field_label => $s->label,
143             search_field_type  => $s->type,
144             marc_field         => $s->get_column('marc_field'),
145             sort               => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
146             suggestible        => $s->get_column('suggestible'),
147             facet              => $s->get_column('facet'),
148           };
149     }
150
151     push @indexes, { index_name => $index_name, mappings => \@mappings };
152 }
153
154 my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
155 my @all_search_fields;
156 while ( my $search_field = $search_fields->next ) {
157     my $search_field_unblessed = $search_field->unblessed;
158     $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
159     push @all_search_fields, $search_field_unblessed;
160 }
161
162 $template->param(
163     indexes           => \@indexes,
164     all_search_fields => \@all_search_fields,
165     messages          => \@messages,
166 );
167
168 output_html_with_http_headers $input, $cookie, $template->output;