View | Details | Raw Unified | Return to bug 27113
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch/Browse.pm (+5 lines)
Lines 17-22 package Koha::SearchEngine::Elasticsearch::Browse; Link Here
17
# You should have received a copy of the GNU General Public License
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Koha::SearchEngine::Elasticsearch::FilterAutocomplete qw ( filterAutocomplete );
21
20
=head1 NAME
22
=head1 NAME
21
23
22
Koha::SearchEngine::ElasticSearch::Browse - browse functions for Elasticsearch
24
Koha::SearchEngine::ElasticSearch::Browse - browse functions for Elasticsearch
Lines 186-191 sub autocomplete_one_idx { Link Here
186
        index => $self->index_name,
188
        index => $self->index_name,
187
        body => $query
189
        body => $query
188
    );
190
    );
191
    if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) {
192
      filterAutocomplete( $res );
193
    }
189
    $res->{'val'} = $cgi_q;
194
    $res->{'val'} = $cgi_q;
190
    $res->{'prefix'} = $prefix;
195
    $res->{'prefix'} = $prefix;
191
    $res->{'token_counter'} = $token_counter;
196
    $res->{'token_counter'} = $token_counter;
(-)a/Koha/SearchEngine/Elasticsearch/FilterAutocomplete.pm (+104 lines)
Line 0 Link Here
1
package Koha::SearchEngine::Elasticsearch::FilterAutocomplete;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
17
use Modern::Perl;
18
19
use vars qw(@ISA @EXPORT);
20
21
BEGIN {
22
    require Exporter;
23
    @ISA    = qw(Exporter);
24
    @EXPORT = qw(
25
      filterAutocomplete
26
    );
27
}
28
29
=head1
30
31
=cut
32
33
sub filterAutocomplete {
34
    my $res = shift;
35
    my $hits = $res->{hits};
36
    my $hitlist = $res->{hits}->{hits};
37
    my $filtered_items;
38
    if (C4::Context->yaml_preference('OpacHiddenItems')) {
39
        my @biblionumbers;
40
        foreach (@{$hitlist}) {
41
            push(@biblionumbers, $_->{ "_id" });
42
        }
43
        my $autocomplete_items = Koha::Items->search({
44
            biblionumber => { -in => \@biblionumbers }
45
        });
46
        $filtered_items = $autocomplete_items->filter_by_visible_in_opac({
47
            patron => undef
48
        });
49
    }
50
    my $maxscore = 0;
51
    for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) {
52
        my $biblio = Koha::Biblios->find($hitlist->[$i]->{ "_id" });
53
        my $record = $biblio->metadata->record;
54
        # FIXME hardcoded; the suppression flag ought to be materialized
55
        # as a column on biblio or the like
56
        my $opacsuppressionfield = '942';
57
        my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
58
        if (C4::Context->preference('OpacSuppression')){
59
            if ( $opacsuppressionfieldvalue &&
60
            $opacsuppressionfieldvalue->subfield("n") &&
61
            $opacsuppressionfieldvalue->subfield("n") == 1) {
62
                if (C4::Context->preference('OpacSuppressionByIPRange')) {
63
                    my $IPAddress = $ENV{'REMOTE_ADDR'};
64
                    my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
65
                    if ($IPAddress !~ /^$IPRange/)  {
66
                        splice(@{$hitlist}, $i, 1);
67
                        $i--;
68
                        $hits->{ total }--;
69
                    }
70
                }
71
                else{
72
                    splice(@{$hitlist}, $i, 1);
73
                    $i--;
74
                    $hits->{ total }--;
75
                }
76
            }
77
        }
78
        # Remove item inside hits in elasticsearch response if the item is
79
        # declared hidden in OPACHiddenItems preference
80
        if (C4::Context->yaml_preference('OpacHiddenItems') && length $hitlist->[$i]) {
81
            my $item = $filtered_items->find({
82
              biblionumber => $hitlist->[$i]->{ "_id" }
83
            });
84
            if (!$item) {
85
              splice(@{$hitlist}, $i, 1);
86
              $i--;
87
              $hits->{ total }--;
88
89
            }
90
        }
91
        if(length $hitlist->[$i]){
92
          my $score = $hitlist->[$i]->{"_score"};
93
          $maxscore = $score if ($maxscore < $score);
94
        }
95
    }
96
    # Adjust the max_score inside hits in elasticsearch response
97
    if ($maxscore == 0) {
98
        $hits->{ "max_score" } = undef;
99
    } else {
100
        $hits->{ "max_score" } = $maxscore;
101
    }
102
}
103
104
1;
(-)a/opac/svc/elasticsearch/opac-elasticsearch.pl (-81 lines)
Lines 34-41 if ($session->param("key") eq "autocomplete") { Link Here
34
  if ($length >= 1) {
34
  if ($length >= 1) {
35
    my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter"));
35
    my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter"));
36
36
37
    filterAutocomplete($res);
38
39
    print $cgi->header("application/json;charset=UTF-8");
37
    print $cgi->header("application/json;charset=UTF-8");
40
    print to_json($res, {utf8 => 1});
38
    print to_json($res, {utf8 => 1});
41
  }
39
  }
Lines 47-130 if ($session->param("key") eq "autocomplete") { Link Here
47
  response404JSON();
45
  response404JSON();
48
}
46
}
49
47
50
sub filterAutocomplete {
51
  if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) {
52
    my $res = shift;
53
    my @prefix = $res->{ "prefix" };
54
    @prefix = split(',', $prefix[0]);
55
56
    for (my $i = 0; $i < scalar @prefix; $i++) {
57
      my $hits = $res->{ $i }->{ 'hits' };
58
      my $hitlist = $hits->{ "hits" };
59
      if (@{$hitlist}) {
60
        # Remove item inside hits in elasticsearch response if the item has
61
        # marc field 942$n set to true and OpacSuppression preference on
62
        if (C4::Context->preference('OpacSuppression')) {
63
          for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) {
64
            my $biblio = Koha::Biblios->find($hitlist->[$i]->{ "_id" });
65
            my $record = $biblio->metadata->record;
66
            my $opacsuppressionfield = '942';
67
            my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
68
            if ( $opacsuppressionfieldvalue &&
69
                $opacsuppressionfieldvalue->subfield("n") &&
70
                $opacsuppressionfieldvalue->subfield("n") == 1) {
71
              # if OPAC suppression by IP address
72
              if (C4::Context->preference('OpacSuppressionByIPRange')) {
73
                my $IPAddress = $ENV{'REMOTE_ADDR'};
74
                my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
75
                if ($IPAddress !~ /^$IPRange/)  {
76
                    splice(@{$hitlist}, $i, 1);
77
                    $i--;
78
                    $hits->{ "total" }--;
79
                }
80
              } else {
81
                splice(@{$hitlist}, $i, 1);
82
                $i--;
83
                $hits->{ "total" }--;
84
              }
85
            }
86
          }
87
        }
88
        # Remove item inside hits in elasticsearch response if the item is
89
        # declared hidden in OPACHiddenItems preference
90
        if (C4::Context->yaml_preference('OpacHiddenItems')) {
91
          my @biblionumbers;
92
          foreach (@{$hitlist}) {
93
            push(@biblionumbers, $_->{ "_id" });
94
          }
95
          my $autocomplete_items = Koha::Items->search({
96
            biblionumber => { -in => \@biblionumbers }
97
          });
98
          my $filtered_items = $autocomplete_items->filter_by_visible_in_opac({
99
            patron => undef
100
          });
101
          for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) {
102
            my $item = $filtered_items->find({
103
              biblionumber => $hitlist->[$i]->{ "_id" }
104
            });
105
            if (!$item) {
106
              splice(@{$hitlist}, $i, 1);
107
              $i--;
108
              $hits->{ "total" }--;
109
            }
110
          }
111
        }
112
        # Adjust the max_score inside hits in elasticsearch response
113
        my $maxscore = 0;
114
        foreach ( @{$hitlist} ) {
115
          my $score = $_->{"_score"};
116
          $maxscore = $score if ($maxscore < $score);
117
        }
118
        if ($maxscore == 0) {
119
          $hits->{ "max_score" } = undef;
120
        } else {
121
          $hits->{ "max_score" } = $maxscore;
122
        }
123
      }
124
    }
125
  }
126
}
127
128
sub response404JSON {
48
sub response404JSON {
129
  my $res = CGI->new;
49
  my $res = CGI->new;
130
  my $json = JSON->new->utf8;
50
  my $json = JSON->new->utf8;
131
- 

Return to bug 27113