From de41bca161ec175a1692d4e749a98ca129417c85 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 5 Jul 2017 12:11:35 +0000 Subject: [PATCH] Bug 18213 - Add language facets to Elasticsearch 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 --- C4/Languages.pm | 14 +++++++ Koha/SearchEngine/Elasticsearch.pm | 16 ++++++++ Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 1 + Koha/SearchEngine/Elasticsearch/Search.pm | 17 ++++---- admin/searchengine/elasticsearch/mappings.yaml | 7 ++-- .../intranet-tmpl/prog/en/includes/facets.inc | 5 ++- .../admin/searchengine/elasticsearch/mappings.tt | 46 ++++++++++++++++++++++ .../bootstrap/en/includes/opac-facets.inc | 5 ++- t/db_dependent/Languages.t | 8 +++- 9 files changed, 105 insertions(+), 14 deletions(-) diff --git a/C4/Languages.pm b/C4/Languages.pm index 5e4dabd..dc97cd5 100644 --- a/C4/Languages.pm +++ b/C4/Languages.pm @@ -612,6 +612,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__ diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index de4b18c..87e351f 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -479,6 +479,22 @@ sub process_error { return "Unable to perform your search. Please try again.\n"; } +sub get_facetable_fields { + my ($self) = @_; + + # 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 se subject ccode holdingbranch homebranch ln); + my @faceted_fields = Koha::SearchFields->search( + { name => { -in => \@search_field_names }, facet_order => { '!=' => undef } }, { order_by => ['facet_order'] } + ); + my @not_faceted_fields = Koha::SearchFields->search( + { name => { -in => \@search_field_names }, facet_order => undef }, { order_by => ['facet_order'] } + ); + # This could certainly be improved + return ( @faceted_fields, @not_faceted_fields ); +} + 1; __END__ diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index cdd0fc6..fca9cd5 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -119,6 +119,7 @@ sub build_query { 'su-geo' => { terms => { field => "su-geo__facet" } }, se => { terms => { field => "se__facet" } }, ccode => { terms => { field => "ccode__facet" } }, + ln => { terms => { field => "ln__facet" } }, }; my $display_library_facets = C4::Context->preference('DisplayLibraryFacets'); diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index 4d584f7..11cc05b 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -425,15 +425,16 @@ sub _convert_facets { # things that zebra uses. # TODO let the library define the order using the interface. my %type_to_label = ( - 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',}, + 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' } + homebranch => { order => 9, label => 'HomeLibrary' }, + ln => { order => 10, label => 'Language' } ); # We also have some special cases, e.g. itypes that need to show the diff --git a/admin/searchengine/elasticsearch/mappings.yaml b/admin/searchengine/elasticsearch/mappings.yaml index 4c16489..ae0eb7d 100644 --- a/admin/searchengine/elasticsearch/mappings.yaml +++ b/admin/searchengine/elasticsearch/mappings.yaml @@ -1715,22 +1715,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 local-classification: label: local-classification mappings: diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc index 572be95..9abeef7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/facets.inc @@ -1,4 +1,5 @@ [% USE AuthorisedValues %] +[% USE Languages %] [% IF ( opacfacets ) %] [% IF ( facets_loop ) %]
@@ -21,7 +22,8 @@ [% IF ( facets_loo.type_label_HomeLibrary ) %]Home libraries[% END %] [% IF ( facets_loo.type_label_HoldingLibrary ) %]Holding libraries[% END %] [% IF facets_loo.type_label_Location %]Locations[% END %] -[% IF facets_loo.type_label_CollectionCodes %]Collections[% END %] +[% IF facets_loo.type_label_CollectionCodes %]Collections[% END %] +[% IF facets_loo.type_label_Language %]Languages[% END %]
[% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-facets.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-facets.inc index e89a693..7128037 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-facets.inc +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-facets.inc @@ -1,4 +1,5 @@ [% USE AuthorisedValues %] +[% USE Languages %] [% IF ( opacfacets && facets_loop && total ) %]

Refine your search

@@ -25,7 +26,8 @@ [% IF facets_loo.type_label_Places %]
Places
[% END %] [% IF facets_loo.type_label_Series %]
Series
[% END %] [% IF facets_loo.type_label_ItemTypes %]
Item types
[% END %] - [% IF facets_loo.type_label_CollectionCodes %]
Collections
[% END %] + [% IF facets_loo.type_label_CollectionCodes %]
Collections
[% END %] + [% IF facets_loo.type_label_Language %]
Languages
[% END %] [% UNLESS singleBranchMode %] [% IF ( facets_loo.type_label_HomeLibrary ) %]Home libraries[% END %] [% IF ( facets_loo.type_label_HoldingLibrary ) %]Holding libraries[% END %] @@ -38,6 +40,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 %]
  • [% IF facet.active %] [% SET local_url = url _ "&nolimit=" _ facet.type_link_value _ ":" _ facet.facet_link_value %] diff --git a/t/db_dependent/Languages.t b/t/db_dependent/Languages.t index 8e8d372..6a8f147 100755 --- a/t/db_dependent/Languages.t +++ b/t/db_dependent/Languages.t @@ -6,7 +6,7 @@ use strict; use warnings; -use Test::More tests => 17; +use Test::More tests => 18; use List::Util qw(first); use Data::Dumper; use Test::Warn; @@ -95,4 +95,10 @@ 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."); + $dbh->rollback; -- 2.1.4