From 2cbc35b3236f679a85f33d0b244af3753a199093 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 25 Sep 2015 16:06:55 +0100 Subject: [PATCH] Bug 14899: Add the mapping configuration page in the admin module This new page (admin/searchengine/elasticsearch/mappings.pl) will permit to manage the ES mappings. For the biblios and authorities indexes, the different mappings can be managed from this single page. The interface let you add, remove and update mappings and search fields. It's also possible to reorder the mappings, as the order can be important in the indexation process. Note that the table can be displayed in a different order that the one it was before saving, but the mappings are grouped by search field and the order inside the search field is preserved. Limitations: - If something went wrong during the insertion/deletion/modification, the users will loose all these changes. TODO: - Add a specific permission (?) - Add some data checks client side (JS) - Use checkboxes for facet and suggestible (lazy today...) - Understand the difference between the 3 values that sortable can have and improve the value for the options in the select box. --- admin/searchengine/elasticsearch/mappings.pl | 135 ++++++++++ .../admin/searchengine/elasticsearch/mappings.tt | 298 +++++++++++++++++++++ 2 files changed, 433 insertions(+) create mode 100755 admin/searchengine/elasticsearch/mappings.pl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl new file mode 100755 index 0000000..5f7712d --- /dev/null +++ b/admin/searchengine/elasticsearch/mappings.pl @@ -0,0 +1,135 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use CGI; +use C4::Koha; +use C4::Output; +use C4::Auth; + +use Koha::SearchMarcMaps; +use Koha::SearchFields; + +my $input = new CGI; +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { template_name => 'admin/searchengine/elasticsearch/mappings.tt', + query => $input, + type => 'intranet', + authnotrequired => 0, + flagsrequired => { superlibrarian => 1 }, # Create a specific permission? + } +); + +my $index = $input->param('index') || 'biblios'; +my $op = $input->param('op') || 'list'; +my @messages; + +my $database = Koha::Database->new(); +my $schema = $database->schema; + +my $marc_type = lc C4::Context->preference('marcflavour'); + +if ( $op eq 'edit' ) { + + $schema->storage->txn_begin; + + my @field_name = $input->param('search_field_name'); + my @field_label = $input->param('search_field_label'); + my @field_type = $input->param('search_field_type'); + + my @index_name = $input->param('mapping_index_name'); + my @search_field_name = $input->param('mapping_search_field_name'); + my @mapping_sort = $input->param('mapping_sort'); + my @mapping_facet = $input->param('mapping_facet'); + my @mapping_suggestible = $input->param('mapping_suggestible'); + my @mapping_marc_field = $input->param('mapping_marc_field'); + + eval { + + for my $i ( 0 .. scalar(@field_name) - 1 ) { + my $field_name = $field_name[$i]; + my $field_label = $field_label[$i]; + my $field_type = $field_type[$i]; + my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } ); + $search_field->label($field_label); + $search_field->type($field_type); + $search_field->store; + } + + Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete; + + for my $i ( 0 .. scalar(@index_name) - 1 ) { + my $index_name = $index_name[$i]; + my $search_field_name = $search_field_name[$i]; + my $mapping_marc_field = $mapping_marc_field[$i]; + my $mapping_facet = $mapping_facet[$i]; + my $mapping_suggestible = $mapping_suggestible[$i]; + my $mapping_sort = $mapping_sort[$i]; + $mapping_sort = undef if $mapping_sort eq 'undef'; + + my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' }); + # TODO Check mapping format + my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $marc_type, marc_field => $mapping_marc_field }); + $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping_facet, suggestible => $mapping_suggestible, sort => $mapping_sort } ); + + } + }; + if ($@) { + push @messages, { type => 'error', code => 'error_on_update', message => $@, }; + $schema->storage->txn_rollback; + } else { + push @messages, { type => 'message', code => 'success_on_update' }; + $schema->storage->txn_commit; + } +} + +my @indexes; + +for my $index_name (qw| biblios authorities |) { + my $search_fields = Koha::SearchFields->search( + { 'search_marc_map.index_name' => $index_name, 'search_marc_map.marc_type' => $marc_type, }, + { join => { search_marc_to_fields => 'search_marc_map' }, + '+select' => [ 'search_marc_to_fields.facet', 'search_marc_to_fields.suggestible', 'search_marc_to_fields.sort', 'search_marc_map.marc_field' ], + '+as' => [ 'facet', 'suggestible', 'sort', 'marc_field' ], + } + ); + + my @mappings; + while ( my $s = $search_fields->next ) { + push @mappings, + { search_field_name => $s->name, + search_field_label => $s->label, + search_field_type => $s->type, + marc_field => $s->get_column('marc_field'), + sort => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc" + suggestible => $s->get_column('suggestible'), + facet => $s->get_column('facet'), + }; + } + + push @indexes, { index_name => $index_name, mappings => \@mappings }; +} + +my $search_fields = $schema->resultset('SearchField')->search; +my @all_search_fields = $search_fields->search( {}, { order_by => ['name'] } ); +$template->param( + indexes => \@indexes, + all_search_fields => \@all_search_fields, + messages => \@messages, +); + +output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt new file mode 100644 index 0000000..477f8a1 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt @@ -0,0 +1,298 @@ +[% INCLUDE 'doc-head-open.inc' %] +Koha › Administration › Elastic Search mappings +[% INCLUDE 'doc-head-close.inc' %] + + + + + +[% INCLUDE 'header.inc' %] +[% INCLUDE 'cat-search.inc' %] + + + +
+ +
+
+
+ + [% FOR m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_update' %] + An error occurred when updateing mappings ([% m.message %]). + [% CASE 'error_on_delete' %] + An error occurred when deleting the existing mappings. Nothing has been changed! + (search field [% m.values.field_name %] with mapping [% m.values.marc_field %].) + [% CASE 'success_on_update' %] + Mapping updated successfully. + [% CASE %] + [% m.code %] + [% END %] +
+ [% END %] + +

Search engine configuration

+
+ Warning: Any modification in these configurations will need a total reindexation to be fully taken into account ! +
+ [% IF errors %] +
+ Errors occurred, Modifications does not apply. Please check following values: +
    + [% FOREACH e IN errors %] +
  • + [% IF ( e.type == "malformed_mapping" ) %] + The value "[% e.value %]" is not supported for mappings + [% ELSIF ( e.type == "no_mapping" ) %] + There is no mapping for the index [% e.value %] + [% END %] +
  • + [% END %] +
+
+ [% END %] + +
+ +
+
    +
  • Search fields
  • + [% FOREACH index IN indexes %] + [% SWITCH index.index_name %] + [% CASE 'biblios' %]
  • Biblios
  • + [% CASE 'authorities' %]
  • Authorities
  • + [% END %] + [% END %] +
+
+ + + + + + + + + + [% FOREACH search_field IN all_search_fields %] + + + + + [% END %] + +
NameLabelType
+ + + + +
+
+ [% FOREACH index IN indexes %] +
+ + + + + + + + + + + + + [% FOREACH mapping IN index.mappings %] + + + + + + + + + [% END %] + + + + + + + + + + + +
Search fieldSortableFacetableSuggestibleMapping
+ + + [% mapping.search_field_label %] + + + + + + + Delete
+ + + + + + Add
+
+ [% END %] +
+

+
+
+ +
+
+[% INCLUDE 'admin-menu.inc' %] +
+
+[% INCLUDE 'intranet-bottom.inc' %] -- 2.8.1