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