From 83b1f1cad78de2552e8629ac1066798d66ebd088 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Wed, 14 Feb 2018 13:55:57 +0000
Subject: [PATCH] Bug 18969: ES6 - replace use of field include_in_all by
 copy_to

Test plan:

  1) apply this patch,
  2) update your elasticsearch server to the version 6 (6.2?),
  3) reinstall icu plugin,
  4) reindex your authorities and biblios,
  5) check that there is no error in
    /var/log/elasticsearch/elasticsearch.log,
  6) try a search on biblios,
  7) check that facet work,
  8) try a search on authorities

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Bouzid Fergani <bouzid.fergani@inlibro.com>
---
 Koha/SearchEngine/Elasticsearch.pm              | 15 ++++++++++-----
 Koha/SearchEngine/Elasticsearch/QueryBuilder.pm |  6 +++---
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm
index d859a39..a5847d5 100644
--- a/Koha/SearchEngine/Elasticsearch.pm
+++ b/Koha/SearchEngine/Elasticsearch.pm
@@ -173,13 +173,16 @@ sub get_elasticsearch_mappings {
     # TODO cache in the object?
     my $mappings = {
         data => {
-            _all => {type => "string", analyzer => "analyser_standard"},
             properties => {
                 record => {
                     store          => "true",
-                    include_in_all => JSON::false,
+                    copy_to        => "_all_fields",
                     type           => "text",
                 },
+                _all_fields => {
+                    type => "text",
+                    analyzer => "analyser_standard"
+                },
             }
         }
     };
@@ -226,7 +229,7 @@ sub get_elasticsearch_mappings {
                     search_analyzer => "analyser_phrase",
                     analyzer  => "analyser_phrase",
                     type            => "text",
-                    include_in_all  => JSON::false,
+                    copy_to         => "_all_fields",
                     fields          => {
                         phrase => {
                             type            => "keyword",
@@ -254,7 +257,7 @@ sub _elasticsearch_mapping_for_boolean {
 
     return {
         type            => $type,
-        null_value      => 0,
+        null_value      => 'false',
     };
 }
 
@@ -344,7 +347,9 @@ sub get_fixer_rules {
                 # boolean gets special handling, basically if it doesn't exist,
                 # it's added and set to false. Otherwise we can't query it.
                 push @rules,
-                  "unless exists('$name') add_field('$name', 0) end";
+                  "unless exists('$name') add_field('$name', 'false') end";
+                push @rules,
+                  "if exists('$name') add_field('$name', 'true') end";
             }
             if ($type eq 'sum' ) {
                 push @rules, "sum('$name')";
diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
index 8a0ead1..b330e28 100644
--- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
+++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
@@ -90,7 +90,7 @@ sub build_query {
             query            => $query,
             fuzziness        => $fuzzy_enabled ? 'auto' : '0',
             default_operator => 'AND',
-            default_field    => '_all',
+            default_field    => '_all_fields',
             lenient          => JSON::true,
         }
     };
@@ -293,7 +293,7 @@ sub build_authorities_query {
     my @filter_parts;
     foreach my $s ( @{ $search->{searches} } ) {
         my ( $wh, $op, $val ) = @{$s}{qw(where operator value)};
-        $wh = '_all' if $wh eq '';
+        $wh = '_all_fields' if $wh eq '';
         if ( $op eq 'is' || $op eq '=' ) {
 
             # look for something that matches completely
@@ -514,7 +514,7 @@ types.
 =cut
 
 our %index_field_convert = (
-    'kw'      => '_all',
+    'kw'      => '_all_fields',
     'ti'      => 'title',
     'au'      => 'author',
     'su'      => 'subject',
-- 
2.7.4