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>.
20 use Scalar
::Util
qw(looks_like_number);
25 use Koha
::SearchEngine
::Elasticsearch
;
26 use Koha
::SearchEngine
::Elasticsearch
::Indexer
;
27 use Koha
::SearchMarcMaps
;
28 use Koha
::SearchFields
;
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
34 { template_name
=> 'admin/searchengine/elasticsearch/mappings.tt',
38 flagsrequired
=> { parameters
=> 'manage_search_engine_config' },
42 my $index = $input->param('index') || 'biblios';
43 my $op = $input->param('op') || 'list';
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 });
57 $indexer->update_mappings();
59 my $conf = $indexer->get_elasticsearch_params();
62 code
=> 'error_on_update_es_mappings',
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');
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 };
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 } );
130 push @messages, { type
=> 'error', code
=> 'error_on_update', message
=> $@
, };
131 $schema->storage->txn_rollback;
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 );
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) {
158 code
=> 'reindex_required',
159 index => $conf->{index_name
},
162 elsif($indexer->index_status_recreate_required) {
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/] }
183 while ( my $s = $search_fields->next ) {
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;
207 indexes
=> \
@indexes,
208 all_search_fields
=> \
@all_search_fields,
209 messages
=> \
@messages,
212 output_html_with_http_headers
$input, $cookie, $template->output;