Bug 19502: Retrieve index.max_result_window from ES
[koha.git] / Koha / SearchEngine / Elasticsearch / Search.pm
index 6e3ca3b..6b2f2a9 100644 (file)
@@ -45,6 +45,7 @@ use C4::Context;
 use Koha::ItemTypes;
 use Koha::AuthorisedValues;
 use Koha::SearchEngine::QueryBuilder;
+use Koha::SearchEngine::Search;
 use MARC::Record;
 use Catmandu::Store::ElasticSearch;
 
@@ -146,8 +147,10 @@ sub search_compat {
         $servers,  $results_per_page, $offset,       $expanded_facet,
         $branches, $query_type,       $scan
     ) = @_;
-
     my %options;
+    if ( !defined $offset or $offset < 0 ) {
+        $offset = 0;
+    }
     $options{offset} = $offset;
     $options{expanded_facet} = $expanded_facet;
     my $results = $self->search($query, undef, $results_per_page, %options);
@@ -197,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
@@ -214,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;
@@ -224,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
@@ -257,8 +256,8 @@ sub count_auth_use {
 
     my $query = {
         query => {
-            filtered => {
-                query  => { match_all => {} },
+            bool => {
+#                query  => { match_all => {} },
                 filter => { term      => { an => $authid } }
             }
         }
@@ -269,7 +268,7 @@ sub count_auth_use {
 =head2 simple_search_compat
 
     my ( $error, $marcresults, $total_hits ) =
-      $searcher->simple_search( $query, $offset, $max_results );
+      $searcher->simple_search( $query, $offset, $max_results, %options );
 
 This is a simpler interface to the searching, intended to be similar enough to
 L<C4::Search::SimpleSearch>.
@@ -292,6 +291,10 @@ How many results to skip from the start of the results.
 The max number of results to return. The default is 100 (because unlimited
 is a pretty terrible thing to do.)
 
+=item C<%options>
+
+These options are unused by Elasticsearch
+
 =back
 
 Returns:
@@ -322,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) {
@@ -341,6 +345,21 @@ sub simple_search_compat {
     return (undef, \@records, $results->total);
 }
 
+=head2 extract_biblionumber
+
+    my $biblionumber = $searcher->extract_biblionumber( $searchresult );
+
+$searchresult comes from simple_search_compat.
+
+Returns the biblionumber from the search result record.
+
+=cut
+
+sub extract_biblionumber {
+    my ( $self, $searchresultrecord ) = @_;
+    return Koha::SearchEngine::Search::extract_biblionumber( $searchresultrecord );
+}
+
 =head2 json2marc
 
     my $marc = $self->json2marc($marc_json);
@@ -380,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);
@@ -408,16 +446,23 @@ sub _convert_facets {
         'su-geo' => { order => 4, label => 'Places', },
         se       => { order => 5, label => 'Series', },
         subject  => { order => 6, label => 'Topics', },
+        ccode    => { order => 7, label => 'CollectionCodes',},
+        holdingbranch => { order => 8, label => 'HoldingLibrary' },
+        homebranch => { order => 9, label => 'HomeLibrary' }
     );
 
     # We also have some special cases, e.g. itypes that need to show the
     # value rather than the code.
     my @itypes = Koha::ItemTypes->search;
+    my @libraries = Koha::Libraries->search;
+    my $library_names = { map { $_->branchcode => $_->branchname } @libraries };
     my @locations = Koha::AuthorisedValues->search( { category => 'LOC' } );
     my $opac = C4::Context->interface eq 'opac' ;
     my %special = (
         itype    => { map { $_->itemtype         => $_->description } @itypes },
         location => { map { $_->authorised_value => ( $opac ? ( $_->lib_opac || $_->lib ) : $_->lib ) } @locations },
+        holdingbranch => $library_names,
+        homebranch => $library_names
     );
     my @facets;
     $exp_facet //= '';