Bug 19893: Add index status
[koha.git] / admin / searchengine / elasticsearch / mappings.pl
blobd5fef83c388d3ac1abee82cbd53058f83d49bc75
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
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;
25 use Koha::SearchEngine::Elasticsearch;
26 use Koha::SearchEngine::Elasticsearch::Indexer;
27 use Koha::SearchMarcMaps;
28 use Koha::SearchFields;
30 use Try::Tiny;
32 my $input = new CGI;
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34 { template_name => 'admin/searchengine/elasticsearch/mappings.tt',
35 query => $input,
36 type => 'intranet',
37 authnotrequired => 0,
38 flagsrequired => { parameters => 'manage_search_engine_config' },
42 my $index = $input->param('index') || 'biblios';
43 my $op = $input->param('op') || 'list';
44 my @messages;
46 my $database = Koha::Database->new();
47 my $schema = $database->schema;
49 my $marc_type = lc C4::Context->preference('marcflavour');
51 my @index_names = ('biblios', 'authorities');
53 my $update_mappings = sub {
54 for my $index_name (@index_names) {
55 my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
56 try {
57 $indexer->update_mappings();
58 } catch {
59 my $conf = $indexer->get_elasticsearch_params();
60 push @messages, {
61 type => 'error',
62 code => 'error_on_update_es_mappings',
63 message => $_[0],
64 index => $conf->{index_name},
70 if ( $op eq 'edit' ) {
72 $schema->storage->txn_begin;
74 my @field_name = $input->param('search_field_name');
75 my @field_label = $input->param('search_field_label');
76 my @field_type = $input->param('search_field_type');
77 my @field_weight = $input->param('search_field_weight');
79 my @index_name = $input->param('mapping_index_name');
80 my @search_field_name = $input->param('mapping_search_field_name');
81 my @mapping_sort = $input->param('mapping_sort');
82 my @mapping_facet = $input->param('mapping_facet');
83 my @mapping_suggestible = $input->param('mapping_suggestible');
84 my @mapping_marc_field = $input->param('mapping_marc_field');
86 eval {
88 for my $i ( 0 .. scalar(@field_name) - 1 ) {
89 my $field_name = $field_name[$i];
90 my $field_label = $field_label[$i];
91 my $field_type = $field_type[$i];
92 my $field_weight = $field_weight[$i];
94 my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
95 $search_field->label($field_label);
96 $search_field->type($field_type);
98 if (!length($field_weight)) {
99 $search_field->weight(undef);
101 elsif ($field_weight <= 0 || !looks_like_number($field_weight)) {
102 push @messages, { type => 'error', code => 'invalid_field_weight', 'weight' => $field_weight };
104 else {
105 $search_field->weight($field_weight);
108 $search_field->store;
111 Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
113 for my $i ( 0 .. scalar(@index_name) - 1 ) {
114 my $index_name = $index_name[$i];
115 my $search_field_name = $search_field_name[$i];
116 my $mapping_marc_field = $mapping_marc_field[$i];
117 my $mapping_facet = $mapping_facet[$i];
118 my $mapping_suggestible = $mapping_suggestible[$i];
119 my $mapping_sort = $mapping_sort[$i];
120 $mapping_sort = undef if $mapping_sort eq 'undef';
122 my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
123 # TODO Check mapping format
124 my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $marc_type, marc_field => $mapping_marc_field });
125 $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping_facet, suggestible => $mapping_suggestible, sort => $mapping_sort } );
129 if ($@) {
130 push @messages, { type => 'error', code => 'error_on_update', message => $@, };
131 $schema->storage->txn_rollback;
132 } else {
133 push @messages, { type => 'message', code => 'success_on_update' };
134 $schema->storage->txn_commit;
135 $update_mappings->();
138 elsif( $op eq 'reset_confirmed' ) {
139 Koha::SearchMarcMaps->delete;
140 Koha::SearchFields->delete;
141 Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
142 push @messages, { type => 'message', code => 'success_on_reset' };
144 elsif( $op eq 'reset_confirm' ) {
145 $template->param( reset_confirm => 1 );
149 my @indexes;
151 for my $index_name (@index_names) {
152 my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
153 if (!$indexer->index_status_ok) {
154 my $conf = $indexer->get_elasticsearch_params();
155 if ($indexer->index_status_reindex_required) {
156 push @messages, {
157 type => 'error',
158 code => 'reindex_required',
159 index => $conf->{index_name},
162 elsif($indexer->index_status_recreate_required) {
163 push @messages, {
164 type => 'error',
165 code => 'recreate_required',
166 index => $conf->{index_name},
172 for my $index_name (@index_names) {
173 my $search_fields = Koha::SearchFields->search(
174 { 'search_marc_map.index_name' => $index_name, 'search_marc_map.marc_type' => $marc_type, },
175 { join => { search_marc_to_fields => 'search_marc_map' },
176 '+select' => [ 'search_marc_to_fields.facet', 'search_marc_to_fields.suggestible', 'search_marc_to_fields.sort', 'search_marc_map.marc_field' ],
177 '+as' => [ 'facet', 'suggestible', 'sort', 'marc_field' ],
178 order_by => { -asc => [qw/name marc_field/] }
182 my @mappings;
183 while ( my $s = $search_fields->next ) {
184 push @mappings,
185 { search_field_name => $s->name,
186 search_field_label => $s->label,
187 search_field_type => $s->type,
188 marc_field => $s->get_column('marc_field'),
189 sort => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
190 suggestible => $s->get_column('suggestible'),
191 facet => $s->get_column('facet'),
195 push @indexes, { index_name => $index_name, mappings => \@mappings };
198 my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
199 my @all_search_fields;
200 while ( my $search_field = $search_fields->next ) {
201 my $search_field_unblessed = $search_field->unblessed;
202 $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
203 push @all_search_fields, $search_field_unblessed;
206 $template->param(
207 indexes => \@indexes,
208 all_search_fields => \@all_search_fields,
209 messages => \@messages,
212 output_html_with_http_headers $input, $cookie, $template->output;