Bug 19502: Retrieve index.max_result_window from ES
[koha.git] / Koha / SearchEngine / Elasticsearch / Search.pm
index 26f0dff..6b2f2a9 100644 (file)
@@ -19,12 +19,14 @@ package Koha::SearchEngine::Elasticsearch::Search;
 
 =head1 NAME
 
-Koha::SearchEngine::ElasticSearch::Search - search functions for Elasticsearch
+Koha::SearchEngine::Elasticsearch::Search - search functions for Elasticsearch
 
 =head1 SYNOPSIS
 
-    my $searcher = Koha::SearchEngine::ElasticSearch::Search->new();
-    my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new();
+    my $searcher =
+      Koha::SearchEngine::Elasticsearch::Search->new( { index => $index } );
+    my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new(
+        { index => $index } );
     my $query = $builder->build_query('perl');
     my $results = $searcher->search($query);
     print "There were " . $results->total . " results.\n";
@@ -36,9 +38,15 @@ Koha::SearchEngine::ElasticSearch::Search - search functions for Elasticsearch
 
 =cut
 
-use base qw(Koha::ElasticSearch);
-use Koha::ItemTypes;
+use Modern::Perl;
 
+use base qw(Koha::SearchEngine::Elasticsearch);
+use C4::Context;
+use Koha::ItemTypes;
+use Koha::AuthorisedValues;
+use Koha::SearchEngine::QueryBuilder;
+use Koha::SearchEngine::Search;
+use MARC::Record;
 use Catmandu::Store::ElasticSearch;
 
 use Data::Dumper; #TODO remove
@@ -85,10 +93,15 @@ sub search {
     }
     $self->store(
         Catmandu::Store::ElasticSearch->new(
-            %$params, trace_calls => 1,
+            %$params,
         )
     ) unless $self->store;
-    my $results = $self->store->bag->search( %$query, %paging );
+    my $results = eval {
+        $self->store->bag->search( %$query, %paging );
+    };
+    if ($@) {
+        die $self->process_error($@);
+    }
     return $results;
 }
 
@@ -97,7 +110,7 @@ sub search {
     my $count = $searcher->count($query);
 
 This mimics a search request, but just gets the result count instead. That's
-faster than pulling all the data in, ususally.
+faster than pulling all the data in, usually.
 
 =cut
 
@@ -109,8 +122,8 @@ sub count {
         Catmandu::Store::ElasticSearch->new( %$params, trace_calls => 0, ) )
       unless $self->store;
 
-    my $searcher = $self->store->bag->searcher(query => $query);
-    my $count = $searcher->count();
+    my $search = $self->store->bag->search( %$query);
+    my $count = $search->total() || 0;
     return $count;
 }
 
@@ -134,9 +147,12 @@ 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);
 
     # Convert each result into a MARC::Record
@@ -145,7 +161,7 @@ sub search_compat {
         # right place in the array, according to $offset
     $results->each(sub {
             # The results come in an array for some reason
-            my $marc_json = @_[0]->{record};
+            my $marc_json = $_[0]->{record};
             my $marc = $self->json2marc($marc_json);
             $records[$index++] = $marc;
         });
@@ -154,7 +170,7 @@ sub search_compat {
     my %result;
     $result{biblioserver}{hits} = $results->total;
     $result{biblioserver}{RECORDS} = \@records;
-    return (undef, \%result, $self->_convert_facets($results->{facets}));
+    return (undef, \%result, $self->_convert_facets($results->{aggregations}, $expanded_facet));
 }
 
 =head2 search_auth_compat
@@ -179,14 +195,14 @@ sub search_auth_compat {
     $res->each(
         sub {
             my %result;
-            my $record    = @_[0];
+            my $record    = $_[0];
             my $marc_json = $record->{record};
 
             # 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
@@ -201,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;
@@ -211,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
@@ -244,8 +256,8 @@ sub count_auth_use {
 
     my $query = {
         query => {
-            filtered => {
-                query  => { match_all => {} },
+            bool => {
+#                query  => { match_all => {} },
                 filter => { term      => { an => $authid } }
             }
         }
@@ -253,7 +265,100 @@ sub count_auth_use {
     $bib_searcher->count($query);
 }
 
+=head2 simple_search_compat
+
+    my ( $error, $marcresults, $total_hits ) =
+      $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>.
+
+Arguments:
+
+=over 4
+
+=item C<$query>
+
+A thing to search for. It could be a simple string, or something constructed
+with the appropriate QueryBuilder module.
+
+=item C<$offset>
+
+How many results to skip from the start of the results.
+
+=item C<$max_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:
+
+=over 4
+
+=item C<$error>
+
+if something went wrong, this'll contain some kind of error
+message.
+
+=item C<$marcresults>
+
+an arrayref of MARC::Records (note that this is different from the
+L<C4::Search> version which will return plain XML, but too bad.)
+
+=item C<$total_hits>
+
+the total number of results that this search could have returned.
+
+=back
+
+=cut
 
+sub simple_search_compat {
+    my ($self, $query, $offset, $max_results) = @_;
+
+    return ('No query entered', undef, undef) unless $query;
+
+    my %options;
+    $offset = 0 if not defined $offset or $offset < 0;
+    $options{offset} = $offset;
+    $max_results //= 100;
+
+    unless (ref $query) {
+        # We'll push it through the query builder to sanitise everything.
+        my $qb = Koha::SearchEngine::QueryBuilder->new({index => $self->index});
+        (undef,$query) = $qb->build_query_compat(undef, [$query]);
+    }
+    my $results = $self->search($query, undef, $max_results, %options);
+    my @records;
+    $results->each(sub {
+            # The results come in an array for some reason
+            my $marc_json = $_[0]->{record};
+            my $marc = $self->json2marc($marc_json);
+            push @records, $marc;
+        });
+    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
 
@@ -272,65 +377,116 @@ sub json2marc {
 
     # fields are like:
     # [ '245', '1', '2', 'a' => 'Title', 'b' => 'Subtitle' ]
+    # or
+    # [ '001', undef, undef, '_', 'a value' ]
     # conveniently, this is the form that MARC::Field->new() likes
-    foreach $field (@$marcjson) {
-        next if @$field < 5;    # Shouldn't be possible, but...
+    foreach my $field (@$marcjson) {
+        next if @$field < 5;
         if ( $field->[0] eq 'LDR' ) {
             $marc->leader( $field->[4] );
         }
         else {
-            my $marc_field = MARC::Field->new(@$field);
+            my $tag = $field->[0];
+            my $marc_field;
+            if ( MARC::Field->is_controlfield_tag( $field->[0] ) ) {
+                $marc_field = MARC::Field->new($field->[0], $field->[4]);
+            } else {
+                $marc_field = MARC::Field->new(@$field);
+            }
             $marc->append_fields($marc_field);
         }
     }
     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);
+    my $koha_facets = _convert_facets($es_facets, $expanded_facet);
 
 Converts elasticsearch facets types to the form that Koha expects.
 It expects the ES facet name to match the Koha type, for example C<itype>,
 C<au>, C<su-to>, etc.
 
+C<$expanded_facet> is the facet that we want to show FacetMaxCount entries for, rather
+than just 5 like normal.
+
 =cut
 
 sub _convert_facets {
-    my ( $self, $es ) = @_;
+    my ( $self, $es, $exp_facet ) = @_;
 
-    return undef if !$es;
+    return if !$es;
 
     # These should correspond to the ES field names, as opposed to the CCL
     # things that zebra uses.
+    # TODO let the library define the order using the interface.
     my %type_to_label = (
-        author   => 'Authors',
-        location => 'Location',
-        itype    => 'ItemTypes',
-        se       => 'Series',
-        subject  => 'Topics',
-        'su-geo' => 'Places',
+        author   => { order => 1, label => 'Authors', },
+        itype    => { order => 2, label => 'ItemTypes', },
+        location => { order => 3, label => 'Location', },
+        '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->new();
-    my %special = ( itype => sub { $itypes->get_description_for_code(@_) }, );
-    my @res;
-    while ( ( $type, $data ) = each %$es ) {
+    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 //= '';
+    while ( my ( $type, $data ) = each %$es ) {
         next if !exists( $type_to_label{$type} );
+
+        # We restrict to the most popular $limit !results
+        my $limit = ( $type eq $exp_facet ) ? C4::Context->preference('FacetMaxCount') : 5;
         my $facet = {
-            type_id => $type . '_id',
-            expand  => $type,
-            expandable => 1,    # TODO figure how that's supposed to work
-            "type_label_$type_to_label{$type}" => 1,
+            type_id    => $type . '_id',
+            expand     => $type,
+            expandable => ( $type ne $exp_facet )
+              && ( @{ $data->{buckets} } > $limit ),
+            "type_label_$type_to_label{$type}{label}" => 1,
             type_link_value                    => $type,
+            order      => $type_to_label{$type}{order},
         };
-        foreach my $term ( @{ $data->{terms} } ) {
-            my $t = $term->{term};
-            my $c = $term->{count};
+        $limit = @{ $data->{buckets} } if ( $limit > @{ $data->{buckets} } );
+        foreach my $term ( @{ $data->{buckets} }[ 0 .. $limit - 1 ] ) {
+            my $t = $term->{key};
+            my $c = $term->{doc_count};
+            my $label;
             if ( exists( $special{$type} ) ) {
-                $label = $special{$type}->($t);
+                $label = $special{$type}->{$t} // $t;
             }
             else {
                 $label = $t;
@@ -339,14 +495,16 @@ sub _convert_facets {
                 facet_count       => $c,
                 facet_link_value  => $t,
                 facet_title_value => $t . " ($c)",
-                facet_label_value => $label,    # TODO either truncate this,
+                facet_label_value => $label,        # TODO either truncate this,
                      # or make the template do it like it should anyway
                 type_link_value => $type,
             };
         }
-        push @res, $facet if exists $facet->{facets};
+        push @facets, $facet if exists $facet->{facets};
     }
-    return \@res;
+
+    @facets = sort { $a->{order} cmp $b->{order} } @facets;
+    return \@facets;
 }