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