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