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);
21 use List
::Util
qw( first );
26 use Koha
::SearchEngine
::Elasticsearch
;
27 use Koha
::SearchMarcMaps
;
28 use Koha
::SearchFields
;
32 use Module
::Load
::Conditional
qw(can_load);
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
38 template_name
=> 'admin/searchengine/elasticsearch/mappings.tt',
42 flagsrequired
=> { parameters
=> 'manage_search_engine_config' },
46 unless ( can_load
( modules
=> { 'Koha::SearchEngine::Elasticsearch::Indexer' => undef } ) ) {
47 output_and_exit
( $input, $cookie, $template, 'missing_es_modules');
51 my $index = $input->param('index') || 'biblios';
52 my $op = $input->param('op') || 'list';
54 push @messages, { type
=> 'message', code
=> 'elasticsearch_disabled' }
55 if ( C4
::Context
->preference('SearchEngine') ne 'Elasticsearch' );
57 my $database = Koha
::Database
->new();
58 my $schema = $database->schema;
60 my $marc_type = lc C4
::Context
->preference('marcflavour');
62 my @index_names = ($Koha::SearchEngine
::Elasticsearch
::BIBLIOS_INDEX
, $Koha::SearchEngine
::Elasticsearch
::AUTHORITIES_INDEX
);
64 my $update_mappings = sub {
65 for my $index_name (@index_names) {
66 my $indexer = Koha
::SearchEngine
::Elasticsearch
::Indexer
->new({ index => $index_name });
68 $indexer->update_mappings();
70 my $conf = $indexer->get_elasticsearch_params();
73 code
=> 'error_on_update_es_mappings',
75 index => $conf->{index_name
},
81 if ( $op eq 'edit' ) {
83 $schema->storage->txn_begin;
85 my @field_name = $input->multi_param('search_field_name');
86 my @field_label = $input->multi_param('search_field_label');
87 my @field_type = $input->multi_param('search_field_type');
88 my @field_weight = $input->multi_param('search_field_weight');
89 my @field_staff_client = $input->multi_param('search_field_staff_client');
90 my @field_opac = $input->multi_param('search_field_opac');
92 my @index_name = $input->multi_param('mapping_index_name');
93 my @search_field_name = $input->multi_param('mapping_search_field_name');
94 my @mapping_sort = $input->multi_param('mapping_sort');
95 my @mapping_facet = $input->multi_param('mapping_facet');
96 my @mapping_suggestible = $input->multi_param('mapping_suggestible');
97 my @mapping_search = $input->multi_param('mapping_search');
98 my @mapping_marc_field = $input->multi_param('mapping_marc_field');
99 my @faceted_field_names = $input->multi_param('display_facet');
103 for my $i ( 0 .. scalar(@field_name) - 1 ) {
104 my $field_name = $field_name[$i];
105 my $field_label = $field_label[$i];
106 my $field_type = $field_type[$i];
107 my $field_weight = $field_weight[$i];
108 my $field_staff_client = $field_staff_client[$i];
109 my $field_opac = $field_opac[$i];
111 my $search_field = Koha
::SearchFields
->find( { name
=> $field_name }, { key
=> 'name' } );
112 $search_field->label($field_label);
113 $search_field->type($field_type);
115 if (!length($field_weight)) {
116 $search_field->weight(undef);
118 elsif ($field_weight <= 0 || !looks_like_number
($field_weight)) {
119 push @messages, { type
=> 'error', code
=> 'invalid_field_weight', 'weight' => $field_weight };
122 $search_field->weight($field_weight);
124 $search_field->staff_client($field_staff_client ?
1 : 0);
125 $search_field->opac($field_opac ?
1 : 0);
127 my $facet_order = first
{ $faceted_field_names[$_] eq $field_name } 0 .. $#faceted_field_names;
128 $search_field->facet_order(defined $facet_order ?
$facet_order + 1 : undef);
129 $search_field->store;
132 Koha
::SearchMarcMaps
->search( { marc_type
=> $marc_type, } )->delete;
133 my @facetable_fields = Koha
::SearchEngine
::Elasticsearch
->get_facetable_fields();
134 my @facetable_field_names = map { $_->name } @facetable_fields;
136 for my $i ( 0 .. scalar(@index_name) - 1 ) {
137 my $index_name = $index_name[$i];
138 my $search_field_name = $search_field_name[$i];
139 my $mapping_marc_field = $mapping_marc_field[$i];
140 my $mapping_facet = $mapping_facet[$i];
141 $mapping_facet = ( grep { $_ eq $search_field_name } @facetable_field_names ) ?
$mapping_facet : 0;
142 my $mapping_suggestible = $mapping_suggestible[$i];
143 my $mapping_sort = $mapping_sort[$i] eq 'undef' ?
undef : $mapping_sort[$i];
144 my $mapping_search = $mapping_search[$i];
146 my $search_field = Koha
::SearchFields
->find({ name
=> $search_field_name }, { key
=> 'name' });
147 # TODO Check mapping format
148 my $marc_field = Koha
::SearchMarcMaps
->find_or_create({
149 index_name
=> $index_name,
150 marc_type
=> $marc_type,
151 marc_field
=> $mapping_marc_field
153 $search_field->add_to_search_marc_maps($marc_field, {
154 facet
=> $mapping_facet,
155 suggestible
=> $mapping_suggestible,
156 sort => $mapping_sort,
157 search
=> $mapping_search
162 push @messages, { type
=> 'error', code
=> 'error_on_update', message
=> $@
, };
163 $schema->storage->txn_rollback;
165 push @messages, { type
=> 'message', code
=> 'success_on_update' };
166 $schema->storage->txn_commit;
168 Koha
::SearchEngine
::Elasticsearch
->clear_search_fields_cache();
170 $update_mappings->();
173 elsif( $op eq 'reset_confirmed' ) {
174 Koha
::SearchEngine
::Elasticsearch
->reset_elasticsearch_mappings;
175 push @messages, { type
=> 'message', code
=> 'success_on_reset' };
177 elsif( $op eq 'reset_confirm' ) {
178 $template->param( reset_confirm
=> 1 );
183 for my $index_name (@index_names) {
184 my $indexer = Koha
::SearchEngine
::Elasticsearch
::Indexer
->new({ index => $index_name });
185 if (!$indexer->is_index_status_ok) {
186 my $conf = $indexer->get_elasticsearch_params();
187 if ($indexer->is_index_status_reindex_required) {
190 code
=> 'reindex_required',
191 index => $conf->{index_name
},
194 elsif($indexer->is_index_status_recreate_required) {
197 code
=> 'recreate_required',
198 index => $conf->{index_name
},
204 my @facetable_fields = Koha
::SearchEngine
::Elasticsearch
->get_facetable_fields();
205 for my $index_name (@index_names) {
206 my $search_fields = Koha
::SearchFields
->search(
208 'search_marc_map.index_name' => $index_name,
209 'search_marc_map.marc_type' => $marc_type,
212 join => { search_marc_to_fields
=> 'search_marc_map' },
214 'search_marc_to_fields.facet',
215 'search_marc_to_fields.suggestible',
216 'search_marc_to_fields.sort',
217 'search_marc_to_fields.search',
218 'search_marc_map.marc_field'
227 order_by
=> { -asc
=> [qw
/name marc_field/] }
232 my @facetable_field_names = map { $_->name } @facetable_fields;
234 while ( my $s = $search_fields->next ) {
237 search_field_name
=> $name,
238 search_field_label
=> $s->label,
239 search_field_type
=> $s->type,
240 marc_field
=> $s->get_column('marc_field'),
241 sort => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
242 suggestible
=> $s->get_column('suggestible'),
243 search
=> $s->get_column('search'),
244 facet
=> $s->get_column('facet'),
245 is_facetable
=> ( grep { $_ eq $name } @facetable_field_names ) ?
1 : 0,
249 push @indexes, { index_name
=> $index_name, mappings
=> \
@mappings };
252 my $search_fields = Koha
::SearchFields
->search( {}, { order_by
=> ['name'] } );
253 my @all_search_fields;
254 while ( my $search_field = $search_fields->next ) {
255 my $search_field_unblessed = $search_field->unblessed;
256 $search_field_unblessed->{mapped_biblios
} = 1 if $search_field->is_mapped_biblios;
257 push @all_search_fields, $search_field_unblessed;
261 indexes
=> \
@indexes,
262 all_search_fields
=> \
@all_search_fields,
263 facetable_fields
=> \
@facetable_fields,
264 messages
=> \
@messages,
267 output_html_with_http_headers
$input, $cookie, $template->output;