Bugzilla – Attachment 159775 Details for
Bug 27113
Elasticsearch: Autocomplete in search
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 27113: Move filterAutocomplete to a new module and filter the result at search time
Bug-27113-Move-filterAutocomplete-to-a-new-module-.patch (text/plain), 9.12 KB, created by
Salah Ghedda
on 2023-12-12 18:56:31 UTC
(
hide
)
Description:
Bug 27113: Move filterAutocomplete to a new module and filter the result at search time
Filename:
MIME Type:
Creator:
Salah Ghedda
Created:
2023-12-12 18:56:31 UTC
Size:
9.12 KB
patch
obsolete
>From 1bf97c59abfb1e821145f774ab384e63226ac3fc Mon Sep 17 00:00:00 2001 >From: Hammat Wele <hammat.wele@inlibro.com> >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 <http://www.gnu.org/licenses>. > >+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 <http://www.gnu.org/licenses>. >+ >+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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 27113
:
114168
|
114169
|
114945
|
115117
|
116293
|
119979
|
120028
|
120029
|
120084
|
120088
|
120109
|
120140
|
120141
|
120806
|
121497
|
121498
|
121555
|
121556
|
121557
|
121558
|
131207
|
131208
|
131209
|
131210
|
131211
|
132360
|
132361
|
132362
|
132363
|
132364
|
132808
|
132945
|
132946
|
133042
|
133045
|
133407
|
133409
|
133410
|
136312
|
136456
|
136457
|
136464
|
136465
|
136653
|
136657
|
136658
|
136659
|
136660
|
137436
|
137437
|
137438
|
137439
|
137780
|
137781
|
137782
|
137783
|
139624
|
141466
|
145167
|
146015
|
159769
|
159770
|
159771
|
159772
|
159773
|
159774
|
159775
|
160134
|
160135
|
160136
|
160137
|
160138
|
160139
|
160140
|
160141
|
160142
|
161076
|
161077
|
163826
|
165895
|
165896
|
165897
|
165898
|
165899
|
165900
|
165901
|
165902
|
165903
|
165904
|
165913
|
165922