Bug 20484: Implement blocking_errors for ES config page
[srvgit] / 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 List::Util qw( first );
22 use C4::Koha;
23 use C4::Output;
24 use C4::Auth;
25
26 use Koha::SearchEngine::Elasticsearch;
27 use Koha::SearchMarcMaps;
28 use Koha::SearchFields;
29 use Koha::Caches;
30
31 use Try::Tiny;
32 use Module::Load::Conditional qw(can_load);
33
34
35 my $input = new CGI;
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {   template_name   => 'admin/searchengine/elasticsearch/mappings.tt',
38         query           => $input,
39         type            => 'intranet',
40         authnotrequired => 0,
41         flagsrequired   => { parameters => 'manage_search_engine_config' },
42     }
43 );
44
45 unless ( can_load( modules => { 'Koha::SearchEngine::Elasticsearch::Indexer' => undef } ) ) {
46     output_and_exit( $input, $cookie, $template, 'missing_es_modules');
47 }
48
49
50 my $index = $input->param('index') || 'biblios';
51 my $op    = $input->param('op')    || 'list';
52 my @messages;
53
54 my $database = Koha::Database->new();
55 my $schema   = $database->schema;
56
57 my $marc_type = lc C4::Context->preference('marcflavour');
58
59 my @index_names = ($Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX, $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX);
60
61 my $update_mappings = sub {
62     for my $index_name (@index_names) {
63         my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
64         try {
65             $indexer->update_mappings();
66         } catch {
67             my $conf = $indexer->get_elasticsearch_params();
68             push @messages, {
69                 type => 'error',
70                 code => 'error_on_update_es_mappings',
71                 message => $_[0],
72                 index => $conf->{index_name},
73             };
74         };
75     }
76 };
77
78 my $cache = Koha::Caches->get_instance();
79 my $clear_cache = sub {
80     $cache->clear_from_cache('elasticsearch_search_fields_staff_client');
81     $cache->clear_from_cache('elasticsearch_search_fields_opac');
82 };
83
84 if ( $op eq 'edit' ) {
85
86     $schema->storage->txn_begin;
87
88     my @field_name = $input->multi_param('search_field_name');
89     my @field_label = $input->multi_param('search_field_label');
90     my @field_type = $input->multi_param('search_field_type');
91     my @field_weight = $input->multi_param('search_field_weight');
92     my @field_staff_client = $input->multi_param('search_field_staff_client');
93     my @field_opac = $input->multi_param('search_field_opac');
94
95     my @index_name          = $input->multi_param('mapping_index_name');
96     my @search_field_name   = $input->multi_param('mapping_search_field_name');
97     my @mapping_sort        = $input->multi_param('mapping_sort');
98     my @mapping_facet       = $input->multi_param('mapping_facet');
99     my @mapping_suggestible = $input->multi_param('mapping_suggestible');
100     my @mapping_search      = $input->multi_param('mapping_search');
101     my @mapping_marc_field  = $input->multi_param('mapping_marc_field');
102     my @faceted_field_names = $input->multi_param('display_facet');
103
104     eval {
105
106         for my $i ( 0 .. scalar(@field_name) - 1 ) {
107             my $field_name = $field_name[$i];
108             my $field_label = $field_label[$i];
109             my $field_type = $field_type[$i];
110             my $field_weight = $field_weight[$i];
111             my $field_staff_client = $field_staff_client[$i];
112             my $field_opac = $field_opac[$i];
113
114             my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
115             $search_field->label($field_label);
116             $search_field->type($field_type);
117
118             if (!length($field_weight)) {
119                 $search_field->weight(undef);
120             }
121             elsif ($field_weight <= 0 || !looks_like_number($field_weight)) {
122                 push @messages, { type => 'error', code => 'invalid_field_weight', 'weight' => $field_weight };
123             }
124             else {
125                 $search_field->weight($field_weight);
126             }
127             $search_field->staff_client($field_staff_client ? 1 : 0);
128             $search_field->opac($field_opac ? 1 : 0);
129
130             my $facet_order = first { $faceted_field_names[$_] eq $field_name } 0 .. $#faceted_field_names;
131             $search_field->facet_order(defined $facet_order ? $facet_order + 1 : undef);
132             $search_field->store;
133         }
134
135         Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
136         my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
137         my @facetable_field_names = map { $_->name } @facetable_fields;
138
139         for my $i ( 0 .. scalar(@index_name) - 1 ) {
140             my $index_name          = $index_name[$i];
141             my $search_field_name   = $search_field_name[$i];
142             my $mapping_marc_field  = $mapping_marc_field[$i];
143             my $mapping_facet       = $mapping_facet[$i];
144             $mapping_facet = ( grep { $_ eq $search_field_name } @facetable_field_names ) ? $mapping_facet : 0;
145             my $mapping_suggestible = $mapping_suggestible[$i];
146             my $mapping_sort        = $mapping_sort[$i] eq 'undef' ? undef : $mapping_sort[$i];
147             my $mapping_search      = $mapping_search[$i];
148
149             my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
150             # TODO Check mapping format
151             my $marc_field = Koha::SearchMarcMaps->find_or_create({
152                 index_name => $index_name,
153                 marc_type => $marc_type,
154                 marc_field => $mapping_marc_field
155             });
156             $search_field->add_to_search_marc_maps($marc_field, {
157                 facet => $mapping_facet,
158                 suggestible => $mapping_suggestible,
159                 sort => $mapping_sort,
160                 search => $mapping_search
161             });
162         }
163     };
164     if ($@) {
165         push @messages, { type => 'error', code => 'error_on_update', message => $@, };
166         $schema->storage->txn_rollback;
167     } else {
168         push @messages, { type => 'message', code => 'success_on_update' };
169         $schema->storage->txn_commit;
170         $clear_cache->();
171         $update_mappings->();
172     }
173 }
174 elsif( $op eq 'reset_confirmed' ) {
175     Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
176     $clear_cache->();
177     push @messages, { type => 'message', code => 'success_on_reset' };
178 }
179 elsif( $op eq 'reset_confirm' ) {
180     $template->param( reset_confirm => 1 );
181 }
182
183 my @indexes;
184
185 for my $index_name (@index_names) {
186     my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
187     if (!$indexer->is_index_status_ok) {
188         my $conf = $indexer->get_elasticsearch_params();
189         if ($indexer->is_index_status_reindex_required) {
190             push @messages, {
191                 type => 'error',
192                 code => 'reindex_required',
193                 index => $conf->{index_name},
194             };
195         }
196         elsif($indexer->is_index_status_recreate_required) {
197             push @messages, {
198                 type => 'error',
199                 code => 'recreate_required',
200                 index => $conf->{index_name},
201             };
202         }
203     }
204 }
205
206 my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
207 for my $index_name (@index_names) {
208     my $search_fields = Koha::SearchFields->search(
209         {
210             'search_marc_map.index_name' => $index_name,
211             'search_marc_map.marc_type' => $marc_type,
212         },
213         {
214             join => { search_marc_to_fields => 'search_marc_map' },
215             '+select' => [
216                 'search_marc_to_fields.facet',
217                 'search_marc_to_fields.suggestible',
218                 'search_marc_to_fields.sort',
219                 'search_marc_to_fields.search',
220                 'search_marc_map.marc_field'
221             ],
222             '+as' => [
223                 'facet',
224                 'suggestible',
225                 'sort',
226                 'search',
227                 'marc_field'
228             ],
229             order_by => { -asc => [qw/name marc_field/] }
230          }
231      );
232
233     my @mappings;
234     my @facetable_field_names = map { $_->name } @facetable_fields;
235
236     while ( my $s = $search_fields->next ) {
237         my $name = $s->name;
238         push @mappings, {
239             search_field_name  => $name,
240             search_field_label => $s->label,
241             search_field_type  => $s->type,
242             marc_field         => $s->get_column('marc_field'),
243             sort               => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
244             suggestible        => $s->get_column('suggestible'),
245             search             => $s->get_column('search'),
246             facet              => $s->get_column('facet'),
247             is_facetable       => ( grep { $_ eq $name } @facetable_field_names ) ? 1 : 0,
248         };
249     }
250
251     push @indexes, { index_name => $index_name, mappings => \@mappings };
252 }
253
254 my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
255 my @all_search_fields;
256 while ( my $search_field = $search_fields->next ) {
257     my $search_field_unblessed = $search_field->unblessed;
258     $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
259     push @all_search_fields, $search_field_unblessed;
260 }
261
262 $template->param(
263     indexes           => \@indexes,
264     all_search_fields => \@all_search_fields,
265     facetable_fields  => \@facetable_fields,
266     messages          => \@messages,
267 );
268
269 output_html_with_http_headers $input, $cookie, $template->output;