Bug 19502: Retrieve index.max_result_window from ES
[koha.git] / Koha / SearchEngine / Elasticsearch / Search.pm
index 9f0e2a5..6b2f2a9 100644 (file)
@@ -148,8 +148,10 @@ sub search_compat {
         $branches, $query_type,       $scan
     ) = @_;
     my %options;
+    if ( !defined $offset or $offset < 0 ) {
+        $offset = 0;
+    }
     $options{offset} = $offset;
-    $offset = 0 if $offset < 0;
     $options{expanded_facet} = $expanded_facet;
     my $results = $self->search($query, undef, $results_per_page, %options);
 
@@ -198,9 +200,9 @@ sub search_auth_compat {
 
             # I wonder if these should be real values defined in the mapping
             # rather than hard-coded conversions.
-            # Our results often come through as nested arrays, to fix this
-            # requires changes in catmandu.
-            my $authid = $record->{ 'Local-number' }[0][0];
+            # Handle legacy nested arrays indexed with splitting enabled.
+            my $authid = $record->{ 'Local-number' }[0];
+            $authid = @$authid[0] if (ref $authid eq 'ARRAY');
             $result{authid} = $authid;
 
             # TODO put all this info into the record at index time so we
@@ -215,7 +217,7 @@ sub search_auth_compat {
             # with the record. It's not documented why this is the case, so
             # it's not reproduced here yet.
             my $authtype           = $rs->single;
-            my $auth_tag_to_report = $authtype->auth_tag_to_report;
+            my $auth_tag_to_report = $authtype ? $authtype->auth_tag_to_report : "";
             my $marc               = $self->json2marc($marc_json);
             my $mainentry          = $marc->field($auth_tag_to_report);
             my $reported_tag;
@@ -225,11 +227,7 @@ sub search_auth_compat {
                 }
             }
             # Turn the resultset into a hash
-            my %authtype_cols;
-            foreach my $col ($authtype->result_source->columns) {
-                $authtype_cols{$col} = $authtype->get_column($col);
-            }
-            $result{authtype}     = $authtype->authtypetext;
+            $result{authtype}     = $authtype ? $authtype->authtypetext : $authtypecode;
             $result{reported_tag} = $reported_tag;
 
             # Reimplementing BuildSummary is out of scope because it'll be hard
@@ -327,7 +325,8 @@ sub simple_search_compat {
     return ('No query entered', undef, undef) unless $query;
 
     my %options;
-    $options{offset} = $offset // 0;
+    $offset = 0 if not defined $offset or $offset < 0;
+    $options{offset} = $offset;
     $max_results //= 100;
 
     unless (ref $query) {
@@ -400,6 +399,25 @@ sub json2marc {
     return $marc;
 }
 
+sub max_result_window {
+    my ($self) = @_;
+
+    $self->store(
+        Catmandu::Store::ElasticSearch->new(%{ $self->get_elasticsearch_params })
+    ) unless $self->store;
+
+    my $index_name = $self->store->index_name;
+    my $settings = $self->store->es->indices->get_settings(
+        index  => $index_name,
+        params => { include_defaults => 1, flat_settings => 1 },
+    );
+
+    my $max_result_window = $settings->{$index_name}->{settings}->{'index.max_result_window'};
+    $max_result_window //= $settings->{$index_name}->{defaults}->{'index.max_result_window'};
+
+    return $max_result_window;
+}
+
 =head2 _convert_facets
 
     my $koha_facets = _convert_facets($es_facets, $expanded_facet);