Bug 12478: Take the FacetMaxCount pref into account
[srvgit] / Koha / SearchEngine / Elasticsearch / Search.pm
index 1a74e95..0950139 100644 (file)
@@ -39,7 +39,9 @@ Koha::SearchEngine::ElasticSearch::Search - search functions for Elasticsearch
 =cut
 
 use base qw(Koha::ElasticSearch);
+use C4::Context;
 use Koha::ItemTypes;
+use Koha::AuthorisedValues;
 use Koha::SearchEngine::QueryBuilder;
 
 use Catmandu::Store::ElasticSearch;
@@ -146,6 +148,7 @@ sub search_compat {
 
     my %options;
     $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
@@ -376,7 +379,7 @@ 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 10 entries for, rather
+C<$expanded_facet> is the facet that we want to show FacetMaxCount entries for, rather
 than just 5 like normal.
 
 =cut
@@ -388,40 +391,47 @@ sub _convert_facets {
 
     # 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', },
     );
 
     # 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;
+    my @itypes = Koha::ItemTypes->search;
+    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 },
+    );
+    my @facets;
     $exp_facet //= '';
     while ( ( $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 ) ? 10 : 5;
-        $limit = $#{ $data->{terms} } if ( $limit > @{ $data->{terms} } );
+        # 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 => ( $type ne $exp_facet )
               && ( @{ $data->{terms} } > $limit ),
-            "type_label_$type_to_label{$type}" => 1,
+            "type_label_$type_to_label{$type}{label}" => 1,
             type_link_value                    => $type,
+            order      => $type_to_label{$type}{order},
         };
+        $limit = @{ $data->{terms} } if ( $limit > @{ $data->{terms} } );
         foreach my $term ( @{ $data->{terms} }[ 0 .. $limit - 1 ] ) {
             my $t = $term->{term};
             my $c = $term->{count};
             if ( exists( $special{$type} ) ) {
-                $label = $special{$type}->($t);
+                $label = $special{$type}->{$t} // $t;
             }
             else {
                 $label = $t;
@@ -435,9 +445,11 @@ sub _convert_facets {
                 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;
 }