Bug 18213: Add language facets to Elasticsearch
authorNick Clemens <nick@bywatersolutions.com>
Wed, 5 Jul 2017 12:11:35 +0000 (12:11 +0000)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 28 Mar 2019 15:57:32 +0000 (15:57 +0000)
This patch add language as a facet to ES results - it adds
a new template plugin for languages to get the appropriate
description given an iso 639-2 code

To test:
1 - Make sure you have records with differing languages (in the MARC21 008
        field characters 35-37 or UNIMARC 101a)
2 - Apply patch
3 - Reload Elasticsearch settings:
http://localhost:8081/cgi-bin/koha/admin/searchengine/elasticsearch/mappings.pl?op=reset&i_know_what_i_am_doing=1
4 - Reindex your records
5 - Search for a phrase that will return results in several languages
6 - Verify you see factes correctly labelled for 'Language'
7 - Verify the facets work
8 - Verify both opac and staff results
9 - prove t/db_dependent/Languages.t

Signed-off-by: Nicolas Legrand <nicolas.legrand@bulac.fr>
Signed-off-by: Alex Arnaud <alex.arnaud@biblibre.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

C4/Languages.pm
Koha/SearchEngine/Elasticsearch.pm
Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Koha/SearchEngine/Elasticsearch/Search.pm
admin/searchengine/elasticsearch/mappings.yaml
koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc
koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-facets.inc
t/db_dependent/Languages.t

index 1958c91..b06e303 100644 (file)
@@ -622,6 +622,20 @@ sub getlanguage {
     return $language;
 }
 
+=head2 get_rfc4646_from_iso639
+
+    Select a language rfc4646 code given an iso639 code
+
+=cut
+
+sub get_rfc4646_from_iso639 {
+
+    my $iso_code = shift;
+    my $rfc_subtag = Koha::Database->new()->schema->resultset('LanguageRfc4646ToIso639')->find({iso639_2_code=>$iso_code})->rfc4646_subtag;
+    return $rfc_subtag;
+
+}
+
 1;
 
 __END__
index bb730d2..3855ce4 100644 (file)
@@ -990,7 +990,7 @@ sub get_facetable_fields {
 
     # These should correspond to the ES field names, as opposed to the CCL
     # things that zebra uses.
-    my @search_field_names = qw( author itype location su-geo title-series subject ccode holdingbranch homebranch );
+    my @search_field_names = qw( author itype location su-geo title-series subject ccode holdingbranch homebranch ln );
     my @faceted_fields = Koha::SearchFields->search(
         { name => { -in => \@search_field_names }, facet_order => { '!=' => undef } }, { order_by => ['facet_order'] }
     );
index 252416c..9033148 100644 (file)
@@ -111,13 +111,14 @@ sub build_query {
     # See _convert_facets in Search.pm for how these get turned into
     # things that Koha can use.
     $res->{aggregations} = {
-        author => { terms => { field => "author__facet" } },
-        subject => { terms => { field => "subject__facet" } },
-        itype => { terms => { field => "itype__facet" } },
-        location => { terms => { field => "location__facet" } },
-        'su-geo' => { terms => { field => "su-geo__facet" } },
+        author         => { terms => { field => "author__facet" } },
+        subject        => { terms => { field => "subject__facet" } },
+        itype          => { terms => { field => "itype__facet" } },
+        location       => { terms => { field => "location__facet" } },
+        'su-geo'       => { terms => { field => "su-geo__facet" } },
         'title-series' => { terms => { field => "title-series__facet" } },
-        ccode => { terms => { field => "ccode__facet" } },
+        ccode          => { terms => { field => "ccode__facet" } },
+        ln             => { terms => { field => "ln__facet" } },
     };
 
     my $display_library_facets = C4::Context->preference('DisplayLibraryFacets');
index f30c139..d8b8469 100644 (file)
@@ -447,6 +447,7 @@ sub _convert_facets {
         ccode          => 'CollectionCodes',
         holdingbranch  => 'HoldingLibrary',
         homebranch     => 'HomeLibrary',
+        ln             => 'Language',
     );
     my @facetable_fields =
       Koha::SearchEngine::Elasticsearch->get_facetable_fields;
index feccc94..8ead46b 100644 (file)
@@ -2599,22 +2599,23 @@ biblios:
   ln:
     label: ln
     mappings:
-      - facet: ''
+      - facet: '1'
         marc_field: '008_/35-37'
         marc_type: marc21
         sort: ~
         suggestible: ''
-      - facet: ''
+      - facet: '1'
         marc_field: '008_/35-37'
         marc_type: normarc
         sort: ~
         suggestible: ''
-      - facet: ''
+      - facet: '1'
         marc_field: 101a
         marc_type: unimarc
         sort: ~
         suggestible: ''
     type: ''
+    facet_order: 10
   ln-audio:
     label: ln-audio
     mappings:
index 4d0e3c4..ea7a46d 100644 (file)
@@ -1,5 +1,6 @@
 [% USE raw %]
 [% USE AuthorisedValues %]
+[% USE Languages %]
 [% IF ( opacfacets ) %]
 [% IF ( facets_loop ) %]
 <div id="search-facets">
@@ -23,6 +24,7 @@
 [% IF ( facets_loo.type_label_HoldingLibrary ) %]<span id="facet-holding-libraries">Holding libraries</span>[% END %]
 [% IF facets_loo.type_label_Location %]<span id="facet-locations">Locations</span>[% END %]
 [% IF facets_loo.type_label_CollectionCodes %]<span id="facet-collections">Collections</span>[% END %]
+[% IF facets_loo.type_label_Language %]<span id="facet-languages">Languages</span>[% END %]
 <ul>
   [% SET url = "/cgi-bin/koha/catalogue/search.pl?" _ query_cgi _ limit_cgi %]
   [% IF ( sort_by ) %]
@@ -30,6 +32,7 @@
   [% END %]
   [% FOREACH facet IN facets_loo.facets %]
   [% IF facets_loo.type_label_CollectionCodes %][% SET facet.facet_label_value = AuthorisedValues.GetByCode('CCODE',facet.facet_label_value,0) || facet.facet_label_value %][% END %]
+  [% IF facets_loo.type_label_Language %][% SET facet.facet_label_value = Languages.GetByISOCode(lang,facet.facet_label_value) || facet.facet_label_value %][% END %]
     <li>
       [% IF facet.active %]
         [% local_url = BLOCK %][% url | $raw %][% "&nolimit=" _  facet.type_link_value _ ":" _ facet.facet_link_value | url %][% END %]
index 2305823..33df1fb 100644 (file)
@@ -369,6 +369,7 @@ a.add, a.delete {
                                                 [% CASE 'ccode' %]Collections
                                                 [% CASE 'holdingbranch' %]Holding libraries
                                                 [% CASE 'homebranch' %]Home libraries
+                                                [% CASE 'ln' %]Language
                                                 [% CASE %][% f | html %]
                                                 [% END %]
                                             </td>
index c840b2d..10ce696 100644 (file)
@@ -1,5 +1,6 @@
 [% USE raw %]
 [% USE AuthorisedValues %]
+[% USE Languages %]
 [% IF ( opacfacets && facets_loop && total ) %]
     <div id="search-facets">
         <h4><a href="#" class="menu-collapse-toggle">Refine your search</a></h4>
@@ -27,6 +28,7 @@
                         [% IF facets_loo.type_label_Series %]<h5 id="facet-series">Series</h5>[% END %]
                         [% IF facets_loo.type_label_ItemTypes %]<h5 id="facet-itemtypes">Item types</h5>[% END %]
                         [% IF facets_loo.type_label_CollectionCodes %]<h5 id="facet-collections">Collections</h5>[% END %]
+                        [% IF facets_loo.type_label_Language %]<h5 id="facet-languages">Languages</h5>[% END %]
                         [% UNLESS singleBranchMode %]
                             [% IF ( facets_loo.type_label_HomeLibrary ) %]<h5 id="facet-home-libraries">Home libraries</h5>[% END %]
                             [% IF ( facets_loo.type_label_HoldingLibrary ) %]<h5 id="facet-holding-libraries">Holding libraries</h5>[% END %]
@@ -39,6 +41,7 @@
                             [% END %]
                             [% FOREACH facet IN facets_loo.facets %]
                             [% IF facets_loo.type_label_CollectionCodes %][% SET facet.facet_label_value = AuthorisedValues.GetByCode('CCODE',facet.facet_label_value,1) || facet.facet_label_value %][% END %]
+                            [% IF facets_loo.type_label_Language %][% SET facet.facet_label_value = Languages.GetByISOCode(lang,facet.facet_label_value) || facet.facet_label_value %][% END %]
                                 <li>
                                   [% IF facet.active %]
                                     [% local_url = BLOCK %][% url | $raw %][% "&nolimit=" _  facet.type_link_value _ ":" _ facet.facet_link_value | url %][% END %]
index 5afdd80..120f1d5 100755 (executable)
@@ -5,7 +5,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 17;
+use Test::More tests => 18;
 use List::Util qw(first);
 use Data::Dumper;
 use Test::Warn;
@@ -94,3 +94,9 @@ $sth->execute();
 my $LangRfc4646 = $sth->fetchall_arrayref({});
 
 is(scalar(@$LangRfc4646),scalar(@$DistinctLangRfc4646),"No unexpected language_rfc4646_to_iso639 duplicates.");
+
+my $i = 0;
+foreach my $pair (@$DistinctLangRfc4646){
+    $i++ if $pair->{rfc4646_subtag} eq C4::Languages::get_rfc4646_from_iso639( $pair->{iso639_2_code} );
+}
+is($i,scalar(@$DistinctLangRfc4646),"get_rfc4646_from_iso639 returns correct rfc for all iso values.");