From d9637b9da6bfc79ad19cc7259b8c7ab30f9897d6 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 21 Aug 2019 11:13:29 +0300
Subject: [PATCH] Bug 22592: Rebase, add UNIMARC support and fix tests.

---
 .../Elasticsearch/QueryBuilder.pm             |  7 ++--
 Koha/SearchEngine/Elasticsearch/Search.pm     | 35 ++++++++++++-------
 .../SearchEngine/Elasticsearch/QueryBuilder.t | 24 +++++++++++--
 .../Koha/SearchEngine/Elasticsearch/Search.t  | 12 ++++++-
 4 files changed, 58 insertions(+), 20 deletions(-)

diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
index 0bd756988d..f4234a52c7 100644
--- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
+++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
@@ -209,7 +209,6 @@ sub build_query_compat {
         my %options;
         $options{fields} = \@fields;
         $options{sort} = \@sort_params;
-        $options{expanded_facet} = $params->{expanded_facet};
         $query = $self->build_query( $query_str, %options );
     }
 
@@ -452,7 +451,7 @@ sub build_authorities_query_compat {
 
 =head2 _build_scan_query
 
-    my ($query, $query_str) = $builder->build_scan_query(\@operands, \@indexes)
+    my ($query, $query_str) = $builder->_build_scan_query(\@operands, \@indexes)
 
 This will build an aggregation scan query that can be issued to elasticsearch from
 the provided string input.
@@ -471,7 +470,7 @@ sub _build_scan_query {
     my ( $self, $operands, $indexes ) = @_;
 
     my $term = scalar( @$operands ) == 0 ? '' : $operands->[0];
-    my $index = scalar( @$indexes ) == 0 ? 'title' : $indexes->[0];
+    my $index = scalar( @$indexes ) == 0 ? 'subject' : $indexes->[0];
 
     my ( $f, $d ) = split( /,/, $index);
     $index = $scan_field_convert{$f} || $f;
@@ -496,7 +495,7 @@ sub _build_scan_query {
 
 =head2 _create_regex_filter
 
-    my $filter = $builder->create_regex_filter('term')
+    my $filter = $builder->_create_regex_filter('term')
 
 This will create a regex filter that can be used with an aggregation query.
 
diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm
index 6ec37fe639..609b3c486a 100644
--- a/Koha/SearchEngine/Elasticsearch/Search.pm
+++ b/Koha/SearchEngine/Elasticsearch/Search.pm
@@ -132,8 +132,8 @@ sub count {
 
     my ( $error, $results, $facets ) = $search->search_compat(
         $query,            $simple_query, \@sort_by,       \@servers,
-        $results_per_page, $offset,       $branches,       $query_type,
-        $scan
+        $results_per_page, $offset,       undef,           $item_types,
+        $query_type,       $scan
       )
 
 A search interface somewhat compatible with L<C4::Search->getRecords>. Anything
@@ -144,9 +144,9 @@ get ignored here, along with some other things (like C<@servers>.)
 
 sub search_compat {
     my (
-        $self,     $query,            $simple_query, $sort_by,
-        $servers,  $results_per_page, $offset,       $branches,
-        $query_type,       $scan
+        $self,       $query,            $simple_query, $sort_by,
+        $servers,    $results_per_page, $offset,       $branches,
+        $item_types, $query_type,       $scan
     ) = @_;
 
     if ( $scan ) {
@@ -544,15 +544,26 @@ sub _aggregation_scan {
     my $count = scalar(@{$results->{aggregations}{$field}{buckets}});
     for (my $index = $offset; $index - $offset < $results_per_page && $index < $count; $index++) {
         my $bucket = $results->{aggregations}{$field}{buckets}->[$index];
-        # Scan values are expressed as 100 a (count) and 245a (term)
+        # Scan values are expressed as:
+        # - MARC21: 100a (count) and 245a (term)
+        # - UNIMARC: 200f (count) and 200a (term)
         my $marc = MARC::Record->new;
         $marc->encoding('UTF-8');
-        $marc->append_fields(
-            MARC::Field->new((100, ' ',  ' ', 'a' => $bucket->{doc_count}))
-        );
-        $marc->append_fields(
-            MARC::Field->new((245, ' ',  ' ', 'a' => $bucket->{key}))
-        );
+        if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
+            $marc->append_fields(
+                MARC::Field->new((200, ' ',  ' ', 'f' => $bucket->{doc_count}))
+            );
+            $marc->append_fields(
+                MARC::Field->new((200, ' ',  ' ', 'a' => $bucket->{key}))
+            );
+        } else {
+            $marc->append_fields(
+                MARC::Field->new((100, ' ',  ' ', 'a' => $bucket->{doc_count}))
+            );
+            $marc->append_fields(
+                MARC::Field->new((245, ' ',  ' ', 'a' => $bucket->{key}))
+            );
+        }
         $records[$index] = $marc->as_usmarc();
     };
     # consumers of this expect a namespaced result, we provide the default
diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
index 0ae40d780f..d5c9db65f6 100644
--- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
+++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
@@ -170,7 +170,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
 };
 
 subtest 'build_query tests' => sub {
-    plan tests => 40;
+    plan tests => 48;
 
     my $qb;
 
@@ -419,7 +419,25 @@ subtest 'build_query tests' => sub {
     is($query_desc, 'title:"donald duck"', 'query desc ok');
 
     # Scan queries
-    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['su'], undef, undef, 1 );
+    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
+    is(
+        $query->{query}{query_string}{query},
+        '*',
+        "scan query is properly formed"
+    );
+    is_deeply(
+        $query->{aggregations}{'author'}{'terms'},
+        {
+            field => 'author__facet',
+            order => { '_term' => 'asc' },
+            include => '[nN][eE][wW].*'
+        },
+        "scan aggregation request is properly formed"
+    );
+    is($query_cgi, 'idx=au&q=new&scan=1', 'query cgi');
+    is($query_desc, 'new', 'query desc ok');
+
+    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], [], undef, undef, 1 );
     is(
         $query->{query}{query_string}{query},
         '*',
@@ -434,7 +452,7 @@ subtest 'build_query tests' => sub {
         },
         "scan aggregation request is properly formed"
     );
-    is($query_cgi, 'idx=su&q=new&scan=1', 'query cgi');
+    is($query_cgi, 'idx=&q=new&scan=1', 'query cgi');
     is($query_desc, 'new', 'query desc ok');
 };
 
diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t
index 8d73f14058..19c33bf6ec 100644
--- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t
+++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 11;
+use Test::More tests => 13;
 use t::lib::Mocks;
 
 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
@@ -100,6 +100,16 @@ SKIP: {
 
     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
 
+    my ( undef, $scan_query ) = $builder->build_query_compat( undef, ['easy'], [], undef, undef, 1 );
+    ok ((undef, $results) = $searcher->search_compat( $scan_query, undef, [], [], 20, 0, undef, undef, undef, 1 ), 'Test search_compat scan query' );
+    my $expected = {
+        biblioserver => {
+            hits => 0,
+            RECORDS => []
+        }
+    };
+    is_deeply($results, $expected, 'Scan query results ok');
+
     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
 
     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
-- 
2.17.1