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