Bug 20607: Make Koha::SearchEngine::Elasticsearch::reset_elasticsearch_mappings take...
authorAlex Arnaud <alex.arnaud@gmail.com>
Tue, 24 Apr 2018 13:10:38 +0000 (13:10 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 19 Jul 2019 09:11:18 +0000 (10:11 +0100)
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 <nicolas.legrand@bulac.fr>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/SearchEngine/Elasticsearch.pm
t/db_dependent/Koha/SearchEngine/Elasticsearch/Reset.t [new file with mode: 0644]

index 8a52d57..4435015 100644 (file)
@@ -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 (file)
index 0000000..9989533
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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;