From a721c50e4f1752c1f102172135b48fc5887c053a Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 24 Apr 2018 13:10:38 +0000 Subject: [PATCH] Bug 20607: Make Koha::SearchEngine::Elasticsearch::reset_elasticsearch_mappings take weight into account Test plan: - apply this patch, - edit admin/searchengine/elasticsearch/mappings.yaml to add weight for some fields, - go to admin > search engine configuration, - reset your mappings (add ?op=reset&i_know_what_i_am_doing=1 to url), - check that weights you've added are set Signed-off-by: Nicolas Legrand Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize (cherry picked from commit d8830da219f110aabd28fb65bd03bc882b847173) Signed-off-by: Fridolin Somers --- Koha/SearchEngine/Elasticsearch.pm | 21 ++---- .../Koha/SearchEngine/Elasticsearch/Reset.t | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 8a52d574ec..4435015d81 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -284,23 +284,12 @@ sub reset_elasticsearch_mappings { while ( my ( $index_name, $fields ) = each %$indexes ) { while ( my ( $field_name, $data ) = each %$fields ) { - my $field_type = $data->{type}; - my $field_label = $data->{label}; + my %sf_params = map { $_ => $data->{$_} } grep { exists $data->{$_} } qw/ type label weight facet_order /; + $sf_params{name} = $field_name; + + my $search_field = Koha::SearchFields->find_or_create( \%sf_params, { key => 'name' } ); + my $mappings = $data->{mappings}; - my $facet_order = $data->{facet_order}; - my $search_field = Koha::SearchFields->find_or_create({ - name => $field_name, - label => $field_label, - type => $field_type, - }, - { - key => 'name' - }); - $search_field->update( - { - facet_order => $facet_order - } - ); for my $mapping ( @$mappings ) { my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $mapping->{marc_type}, marc_field => $mapping->{marc_field} }); $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping->{facet} || 0, suggestible => $mapping->{suggestible} || 0, sort => $mapping->{sort} } ); diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t new file mode 100644 index 0000000000..9989533c07 --- /dev/null +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t @@ -0,0 +1,66 @@ +#!/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 Test::More tests => 4; +use Test::MockModule; + +use Koha::Database; + +my $indexes = { + 'authorities' => { + 'Match' => { + 'label' => 'Match', + 'type' => '', + 'weight' => 15, + 'mappings' => [] + } + }, + 'biblios' => { + 'title' => { + 'label' => 'title', + 'type' => '', + 'weight' => 20, + 'mapping' => [] + } + } +}; + +my $yaml = Test::MockModule->new('YAML::Syck'); +$yaml->mock( 'LoadFile', sub { return $indexes; } ); + +use_ok('Koha::SearchEngine::Elasticsearch'); + +my $schema = Koha::Database->new->schema; + +Koha::SearchFields->search->delete; +Koha::SearchMarcMaps->search->delete; +$schema->resultset('SearchMarcToField')->search->delete; + +Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; + +my $search_fields = Koha::SearchFields->search({}); +is($search_fields->count, 2, 'There is 2 search fields after reset'); + +my $match_sf = Koha::SearchFields->search({ name => 'Match' })->next; +is($match_sf->weight, '15.00', 'Match search field is weighted with 15'); + +my $title_sf = Koha::SearchFields->search({ name => 'title' })->next; +is($title_sf->weight, '20.00', 'Title search field is weighted with 20'); + +$schema->storage->txn_begin; -- 2.39.5