From 75c6e3a0b62c0f107f8030e8fa123258127c2d6c Mon Sep 17 00:00:00 2001 From: Hammat Wele Date: Thu, 2 Feb 2023 15:39:59 +0000 Subject: [PATCH] Bug 27113: Move filterAutocomplete to a new module and filter the result at search time --- Koha/SearchEngine/Elasticsearch/Browse.pm | 5 + .../Elasticsearch/FilterAutocomplete.pm | 104 ++++++++++++++++++ opac/svc/elasticsearch/opac-elasticsearch.pl | 80 -------------- 3 files changed, 109 insertions(+), 80 deletions(-) create mode 100644 Koha/SearchEngine/Elasticsearch/FilterAutocomplete.pm diff --git a/Koha/SearchEngine/Elasticsearch/Browse.pm b/Koha/SearchEngine/Elasticsearch/Browse.pm index 78f93832c7..582cb99a68 100644 --- a/Koha/SearchEngine/Elasticsearch/Browse.pm +++ b/Koha/SearchEngine/Elasticsearch/Browse.pm @@ -17,6 +17,8 @@ package Koha::SearchEngine::Elasticsearch::Browse; # You should have received a copy of the GNU General Public License # along with Koha; if not, see . +use Koha::SearchEngine::Elasticsearch::FilterAutocomplete qw ( filterAutocomplete ); + =head1 NAME Koha::SearchEngine::ElasticSearch::Browse - browse functions for Elasticsearch @@ -186,6 +188,9 @@ sub autocomplete_one_idx { index => $self->index_name, body => $query ); + if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) { + filterAutocomplete( $res ); + } $res->{'val'} = $cgi_q; $res->{'prefix'} = $prefix; $res->{'token_counter'} = $token_counter; diff --git a/Koha/SearchEngine/Elasticsearch/FilterAutocomplete.pm b/Koha/SearchEngine/Elasticsearch/FilterAutocomplete.pm new file mode 100644 index 0000000000..0be0c46fb3 --- /dev/null +++ b/Koha/SearchEngine/Elasticsearch/FilterAutocomplete.pm @@ -0,0 +1,104 @@ +package Koha::SearchEngine::Elasticsearch::FilterAutocomplete; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use vars qw(@ISA @EXPORT); + +BEGIN { + require Exporter; + @ISA = qw(Exporter); + @EXPORT = qw( + filterAutocomplete + ); +} + +=head1 + +=cut + +sub filterAutocomplete { + my $res = shift; + my $hits = $res->{hits}; + my $hitlist = $res->{hits}->{hits}; + my $filtered_items; + if (C4::Context->yaml_preference('OpacHiddenItems')) { + my @biblionumbers; + foreach (@{$hitlist}) { + push(@biblionumbers, $_->{ "_id" }); + } + my $autocomplete_items = Koha::Items->search({ + biblionumber => { -in => \@biblionumbers } + }); + $filtered_items = $autocomplete_items->filter_by_visible_in_opac({ + patron => undef + }); + } + my $maxscore = 0; + for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) { + my $biblio = Koha::Biblios->find($hitlist->[$i]->{ "_id" }); + my $record = $biblio->metadata->record; + # FIXME hardcoded; the suppression flag ought to be materialized + # as a column on biblio or the like + my $opacsuppressionfield = '942'; + my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield); + if (C4::Context->preference('OpacSuppression')){ + if ( $opacsuppressionfieldvalue && + $opacsuppressionfieldvalue->subfield("n") && + $opacsuppressionfieldvalue->subfield("n") == 1) { + if (C4::Context->preference('OpacSuppressionByIPRange')) { + my $IPAddress = $ENV{'REMOTE_ADDR'}; + my $IPRange = C4::Context->preference('OpacSuppressionByIPRange'); + if ($IPAddress !~ /^$IPRange/) { + splice(@{$hitlist}, $i, 1); + $i--; + $hits->{ total }--; + } + } + else{ + splice(@{$hitlist}, $i, 1); + $i--; + $hits->{ total }--; + } + } + } + # Remove item inside hits in elasticsearch response if the item is + # declared hidden in OPACHiddenItems preference + if (C4::Context->yaml_preference('OpacHiddenItems') && length $hitlist->[$i]) { + my $item = $filtered_items->find({ + biblionumber => $hitlist->[$i]->{ "_id" } + }); + if (!$item) { + splice(@{$hitlist}, $i, 1); + $i--; + $hits->{ total }--; + + } + } + if(length $hitlist->[$i]){ + my $score = $hitlist->[$i]->{"_score"}; + $maxscore = $score if ($maxscore < $score); + } + } + # Adjust the max_score inside hits in elasticsearch response + if ($maxscore == 0) { + $hits->{ "max_score" } = undef; + } else { + $hits->{ "max_score" } = $maxscore; + } +} + +1; diff --git a/opac/svc/elasticsearch/opac-elasticsearch.pl b/opac/svc/elasticsearch/opac-elasticsearch.pl index 8e38d2b72c..e9b46cd86a 100755 --- a/opac/svc/elasticsearch/opac-elasticsearch.pl +++ b/opac/svc/elasticsearch/opac-elasticsearch.pl @@ -34,8 +34,6 @@ if ($session->param("key") eq "autocomplete") { if ($length >= 1) { my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter")); - filterAutocomplete($res); - print $cgi->header("application/json;charset=UTF-8"); print to_json($res, {utf8 => 1}); } @@ -47,84 +45,6 @@ if ($session->param("key") eq "autocomplete") { response404JSON(); } -sub filterAutocomplete { - if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) { - my $res = shift; - my @prefix = $res->{ "prefix" }; - @prefix = split(',', $prefix[0]); - - for (my $i = 0; $i < scalar @prefix; $i++) { - my $hits = $res->{ $i }->{ 'hits' }; - my $hitlist = $hits->{ "hits" }; - if (@{$hitlist}) { - # Remove item inside hits in elasticsearch response if the item has - # marc field 942$n set to true and OpacSuppression preference on - if (C4::Context->preference('OpacSuppression')) { - for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) { - my $biblio = Koha::Biblios->find($hitlist->[$i]->{ "_id" }); - my $record = $biblio->metadata->record; - my $opacsuppressionfield = '942'; - my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield); - if ( $opacsuppressionfieldvalue && - $opacsuppressionfieldvalue->subfield("n") && - $opacsuppressionfieldvalue->subfield("n") == 1) { - # if OPAC suppression by IP address - if (C4::Context->preference('OpacSuppressionByIPRange')) { - my $IPAddress = $ENV{'REMOTE_ADDR'}; - my $IPRange = C4::Context->preference('OpacSuppressionByIPRange'); - if ($IPAddress !~ /^$IPRange/) { - splice(@{$hitlist}, $i, 1); - $i--; - $hits->{ "total" }--; - } - } else { - splice(@{$hitlist}, $i, 1); - $i--; - $hits->{ "total" }--; - } - } - } - } - # Remove item inside hits in elasticsearch response if the item is - # declared hidden in OPACHiddenItems preference - if (C4::Context->yaml_preference('OpacHiddenItems')) { - my @biblionumbers; - foreach (@{$hitlist}) { - push(@biblionumbers, $_->{ "_id" }); - } - my $autocomplete_items = Koha::Items->search({ - biblionumber => { -in => \@biblionumbers } - }); - my $filtered_items = $autocomplete_items->filter_by_visible_in_opac({ - patron => undef - }); - for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) { - my $item = $filtered_items->find({ - biblionumber => $hitlist->[$i]->{ "_id" } - }); - if (!$item) { - splice(@{$hitlist}, $i, 1); - $i--; - $hits->{ "total" }--; - } - } - } - # Adjust the max_score inside hits in elasticsearch response - my $maxscore = 0; - foreach ( @{$hitlist} ) { - my $score = $_->{"_score"}; - $maxscore = $score if ($maxscore < $score); - } - if ($maxscore == 0) { - $hits->{ "max_score" } = undef; - } else { - $hits->{ "max_score" } = $maxscore; - } - } - } - } -} - sub response404JSON { my $res = CGI->new; my $json = JSON->new->utf8; -- 2.34.1