From e21f37f88856222975588d942a504255a3982ba2 Mon Sep 17 00:00:00 2001
From: David Gustafsson <david.gustafsson@ub.gu.se>
Date: Mon, 21 May 2018 14:24:36 +0200
Subject: [PATCH] Bug 18316: Change search field weight field to decimal

Signed-off-by: Ere Maijala <ere.maijala@helsinki.fi>
---
 admin/searchengine/elasticsearch/mappings.pl               |  3 ++-
 .../mysql/atomicupdate/bug_18316_add-weight-column.perl    |  4 ++--
 installer/data/mysql/kohastructure.sql                     |  2 +-
 .../modules/admin/searchengine/elasticsearch/mappings.tt   | 14 +++++---------
 4 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl
index 8108c1e..60ee173 100755
--- a/admin/searchengine/elasticsearch/mappings.pl
+++ b/admin/searchengine/elasticsearch/mappings.pl
@@ -17,6 +17,7 @@
 
 use Modern::Perl;
 use CGI;
+use Scalar::Util qw(looks_like_number);
 use C4::Koha;
 use C4::Output;
 use C4::Auth;
@@ -71,7 +72,7 @@ if ( $op eq 'edit' ) {
             my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
             $search_field->label($field_label);
             $search_field->type($field_type);
-            $search_field->weight($field_weight || '');
+            $search_field->weight($field_weight) if looks_like_number($field_weight) && $field_weight > 0;
             $search_field->store;
         }
 
diff --git a/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl b/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl
index 185a2b5..a076d8b 100644
--- a/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl
+++ b/installer/data/mysql/atomicupdate/bug_18316_add-weight-column.perl
@@ -1,9 +1,9 @@
 $DBversion = 'XXX';
 if( CheckVersion( $DBversion ) ) {
     if( !column_exists( 'search_field', 'weight' ) ) {
-        $dbh->do( "ALTER TABLE search_field ADD COLUMN weight int" );
+        $dbh->do( "ALTER TABLE `search_field` ADD COLUMN `weight` decimal(5,2) DEFAULT NULL" );
     }
 
     SetVersion( $DBversion );
     print "Upgrade to $DBversion done (Bug 18316 - Add column search_field.weight)\n";
-}
\ No newline at end of file
+}
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index f02b9fe..8d0d7e6 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -1473,7 +1473,7 @@ CREATE TABLE `search_field` (
   `name` varchar(255) NOT NULL COMMENT 'the name of the field as it will be stored in the search engine',
   `label` varchar(255) NOT NULL COMMENT 'the human readable name of the field, for display',
   `type` ENUM('', 'string', 'date', 'number', 'boolean', 'sum', 'isbn', 'stdno') NOT NULL COMMENT 'what type of data this holds, relevant when storing it in the search engine',
-  `weight` int default NULL,
+  `weight` decimal(5,2) DEFAULT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY (`name` (191))
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
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
index d83a5f1..ec482ad 100644
--- 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
@@ -93,7 +93,7 @@ a.add, a.delete {
     <div class="warning">
         Warning: Any changes to the configuration will only take effect after a full reindex. Until then searching may not work correctly.
 
-        <p>Weight: define weight between 1 and 99. Higher numbers indicate increased relevancy.
+        <p>Weight: define weight as a positive number. Higher numbers indicate increased relevancy.
         <strong>Note that fields weighting works only for simple search.</strong></p>
         <ol>
           <li>only search fields mapped with biblios can be weighted</li>
@@ -198,17 +198,13 @@ a.add, a.delete {
                           [% END %]
                         </select>
                       </td>
+                      <td>
                       [% IF search_field.mapped_biblios %]
-                        <td>
-                          [% IF search_field.weight %]
-                            <input type="number" name="search_field_weight" value="[% search_field.weight %]" min="1" max="99" />
-                          [% ELSE %]
-                            <input type="number" name="search_field_weight" min="1" max="99" />
-                          [% END %]
-                        </td>
+                        <input type="number" step="0.01" min="0" max="999" name="search_field_weight" value="[% search_field.weight %]" />
                       [% ELSE %]
-                        <td><input type="hidden" name="search_field_weight" value=""></td>
+                        <input type="hidden" name="search_field_weight" value="">
                       [% END %]
+                      </td>
                     </tr>
                   [% END %]
                 </tbody>
-- 
2.7.4