Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

289 lines
11 KiB

  1. #!/usr/bin/perl
  2. # This file is part of Koha.
  3. #
  4. # Koha is free software; you can redistribute it and/or modify it
  5. # under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # Koha is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  16. use Modern::Perl;
  17. use CGI;
  18. use Scalar::Util qw(looks_like_number);
  19. use List::Util qw( first );
  20. use C4::Koha;
  21. use C4::Output;
  22. use C4::Auth;
  23. use C4::Log;
  24. use Koha::SearchEngine::Elasticsearch;
  25. use Koha::SearchEngine::Elasticsearch::QueryBuilder;
  26. use Koha::SearchMarcMaps;
  27. use Koha::SearchFields;
  28. use Koha::Caches;
  29. use Try::Tiny;
  30. use Module::Load::Conditional qw(can_load);
  31. my $input = CGI->new;
  32. my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
  33. {
  34. template_name => 'admin/searchengine/elasticsearch/mappings.tt',
  35. query => $input,
  36. type => 'intranet',
  37. flagsrequired => { parameters => 'manage_search_engine_config' },
  38. }
  39. );
  40. unless ( can_load( modules => { 'Koha::SearchEngine::Elasticsearch::Indexer' => undef } ) ) {
  41. output_and_exit( $input, $cookie, $template, 'missing_es_modules');
  42. }
  43. my $index = $input->param('index') || 'biblios';
  44. my $op = $input->param('op') || 'list';
  45. my @messages;
  46. push @messages, { type => 'message', code => 'elasticsearch_disabled' }
  47. if ( C4::Context->preference('SearchEngine') ne 'Elasticsearch' );
  48. my $database = Koha::Database->new();
  49. my $schema = $database->schema;
  50. my $marc_type = lc C4::Context->preference('marcflavour');
  51. my @index_names = ($Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX, $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX);
  52. my $update_mappings = sub {
  53. for my $index_name (@index_names) {
  54. my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
  55. try {
  56. $indexer->update_mappings();
  57. } catch {
  58. my $conf = $indexer->get_elasticsearch_params();
  59. push @messages, {
  60. type => 'error',
  61. code => 'error_on_update_es_mappings',
  62. message => $_[0],
  63. index => $conf->{index_name},
  64. };
  65. };
  66. }
  67. };
  68. my $search_fields_aliases = {};
  69. while ( my ( $key, $value ) = each(%{Koha::SearchEngine::Elasticsearch::QueryBuilder->get_index_field_convert}) ) {
  70. my $field_aliases = $search_fields_aliases->{$value};
  71. $field_aliases = [] unless $field_aliases;
  72. push @$field_aliases, $key;
  73. $search_fields_aliases->{$value} = $field_aliases;
  74. }
  75. if ( $op eq 'edit' ) {
  76. $schema->storage->txn_begin;
  77. my @field_name = $input->multi_param('search_field_name');
  78. my @field_label = $input->multi_param('search_field_label');
  79. my @field_type = $input->multi_param('search_field_type');
  80. my @field_weight = $input->multi_param('search_field_weight');
  81. my @field_staff_client = $input->multi_param('search_field_staff_client');
  82. my @field_opac = $input->multi_param('search_field_opac');
  83. my @index_name = $input->multi_param('mapping_index_name');
  84. my @search_field_name = $input->multi_param('mapping_search_field_name');
  85. my @mapping_sort = $input->multi_param('mapping_sort');
  86. my @mapping_facet = $input->multi_param('mapping_facet');
  87. my @mapping_suggestible = $input->multi_param('mapping_suggestible');
  88. my @mapping_search = $input->multi_param('mapping_search');
  89. my @mapping_marc_field = $input->multi_param('mapping_marc_field');
  90. my @faceted_field_names = $input->multi_param('display_facet');
  91. eval {
  92. for my $i ( 0 .. scalar(@field_name) - 1 ) {
  93. my $field_name = $field_name[$i];
  94. my $field_label = $field_label[$i];
  95. my $field_type = $field_type[$i];
  96. my $field_weight = $field_weight[$i];
  97. my $field_staff_client = $field_staff_client[$i];
  98. my $field_opac = $field_opac[$i];
  99. my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
  100. $search_field->label($field_label);
  101. $search_field->type($field_type);
  102. if (!length($field_weight)) {
  103. $search_field->weight(undef);
  104. }
  105. elsif ($field_weight <= 0 || !looks_like_number($field_weight)) {
  106. push @messages, { type => 'error', code => 'invalid_field_weight', 'weight' => $field_weight };
  107. }
  108. else {
  109. $search_field->weight($field_weight);
  110. }
  111. $search_field->staff_client($field_staff_client ? 1 : 0);
  112. $search_field->opac($field_opac ? 1 : 0);
  113. my $facet_order = first { $faceted_field_names[$_] eq $field_name } 0 .. $#faceted_field_names;
  114. $search_field->facet_order(defined $facet_order ? $facet_order + 1 : undef);
  115. $search_field->store;
  116. }
  117. Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
  118. my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
  119. my @facetable_field_names = map { $_->name } @facetable_fields;
  120. my $mandatory_before = Koha::SearchFields->search({mandatory=>1})->count;
  121. my $mandatory_after = 0;
  122. my %seen_fields;
  123. for my $i ( 0 .. scalar(@index_name) - 1 ) {
  124. my $index_name = $index_name[$i];
  125. my $search_field_name = $search_field_name[$i];
  126. my $mapping_marc_field = $mapping_marc_field[$i];
  127. my $mapping_facet = $mapping_facet[$i];
  128. $mapping_facet = ( grep { $_ eq $search_field_name } @facetable_field_names ) ? $mapping_facet : 0;
  129. my $mapping_suggestible = $mapping_suggestible[$i];
  130. my $mapping_sort = $mapping_sort[$i] eq 'undef' ? undef : $mapping_sort[$i];
  131. my $mapping_search = $mapping_search[$i];
  132. my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
  133. $mandatory_after++ if $search_field->mandatory && !defined $seen_fields{$search_field_name};
  134. $seen_fields{$search_field_name} = 1;
  135. # TODO Check mapping format
  136. my $marc_field = Koha::SearchMarcMaps->find_or_create({
  137. index_name => $index_name,
  138. marc_type => $marc_type,
  139. marc_field => $mapping_marc_field
  140. });
  141. $search_field->add_to_search_marc_maps($marc_field, {
  142. facet => $mapping_facet,
  143. suggestible => $mapping_suggestible,
  144. sort => $mapping_sort,
  145. search => $mapping_search
  146. });
  147. }
  148. push @messages, { type => 'error', code => 'missing_mandatory_fields' } if $mandatory_after < $mandatory_before;
  149. };
  150. if ($@ || @messages) {
  151. push @messages, { type => 'error', code => 'error_on_update', message => $@, };
  152. $schema->storage->txn_rollback;
  153. } else {
  154. push @messages, { type => 'message', code => 'success_on_update' };
  155. C4::Log::logaction( 'SEARCHENGINE', 'EDIT_MAPPINGS', undef, q{} );
  156. $schema->storage->txn_commit;
  157. Koha::SearchEngine::Elasticsearch->clear_search_fields_cache();
  158. $update_mappings->();
  159. }
  160. }
  161. elsif( $op eq 'reset_confirmed' ) {
  162. Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
  163. push @messages, { type => 'message', code => 'success_on_reset' };
  164. C4::Log::logaction( 'SEARCHENGINE', 'RESET_MAPPINGS', undef, q{} );
  165. }
  166. elsif( $op eq 'reset_confirm' ) {
  167. $template->param( reset_confirm => 1 );
  168. }
  169. my @indexes;
  170. for my $index_name (@index_names) {
  171. my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
  172. if (!$indexer->is_index_status_ok) {
  173. my $conf = $indexer->get_elasticsearch_params();
  174. if ($indexer->is_index_status_reindex_required) {
  175. push @messages, {
  176. type => 'error',
  177. code => 'reindex_required',
  178. index => $conf->{index_name},
  179. };
  180. }
  181. elsif($indexer->is_index_status_recreate_required) {
  182. push @messages, {
  183. type => 'error',
  184. code => 'recreate_required',
  185. index => $conf->{index_name},
  186. };
  187. }
  188. }
  189. }
  190. my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
  191. for my $index_name (@index_names) {
  192. my $search_fields = Koha::SearchFields->search(
  193. {
  194. 'search_marc_map.index_name' => $index_name,
  195. 'search_marc_map.marc_type' => $marc_type,
  196. },
  197. {
  198. join => { search_marc_to_fields => 'search_marc_map' },
  199. '+select' => [
  200. 'search_marc_to_fields.facet',
  201. 'search_marc_to_fields.suggestible',
  202. 'search_marc_to_fields.sort',
  203. 'search_marc_to_fields.search',
  204. 'search_marc_map.marc_field'
  205. ],
  206. '+as' => [
  207. 'facet',
  208. 'suggestible',
  209. 'sort',
  210. 'search',
  211. 'marc_field'
  212. ],
  213. order_by => { -asc => [qw/name marc_field/] }
  214. }
  215. );
  216. my @mappings;
  217. my @facetable_field_names = map { $_->name } @facetable_fields;
  218. while ( my $s = $search_fields->next ) {
  219. my $name = $s->name;
  220. push @mappings, {
  221. search_field_name => $name,
  222. search_field_label => $s->label,
  223. search_field_type => $s->type,
  224. search_field_mandatory => $s->mandatory,
  225. marc_field => $s->get_column('marc_field'),
  226. sort => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
  227. suggestible => $s->get_column('suggestible'),
  228. search => $s->get_column('search'),
  229. facet => $s->get_column('facet'),
  230. is_facetable => ( grep { $_ eq $name } @facetable_field_names ) ? 1 : 0,
  231. };
  232. }
  233. push @indexes, { index_name => $index_name, mappings => \@mappings };
  234. }
  235. my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
  236. my @all_search_fields;
  237. while ( my $search_field = $search_fields->next ) {
  238. my $search_field_unblessed = $search_field->unblessed;
  239. $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
  240. $search_field_unblessed->{aliases} = $search_fields_aliases->{$search_field_unblessed->{name}};
  241. push @all_search_fields, $search_field_unblessed;
  242. }
  243. $template->param(
  244. indexes => \@indexes,
  245. all_search_fields => \@all_search_fields,
  246. facetable_fields => \@facetable_fields,
  247. messages => \@messages,
  248. );
  249. output_html_with_http_headers $input, $cookie, $template->output;