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

(-)a/opac/svc/elasticsearch/opac-elasticsearch.pl (-1 / +91 lines)
Lines 9-14 use Unicode::Normalize; Link Here
9
use CGI::Session;
9
use CGI::Session;
10
use Koha::SearchEngine::Elasticsearch::Browse;
10
use Koha::SearchEngine::Elasticsearch::Browse;
11
11
12
use Koha::Items;
13
use C4::Context;
14
use C4::Biblio qw ( GetMarcBiblio );
15
12
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
16
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
13
my $cgi = CGI->new;
17
my $cgi = CGI->new;
14
my $session = CGI::Session->load() or die CGI::Session->errstr();
18
my $session = CGI::Session->load() or die CGI::Session->errstr();
Lines 33-44 if ($session->param("key") eq "autocomplete") { Link Here
33
    #search by many prefix fields
37
    #search by many prefix fields
34
  if ($length > 1){
38
  if ($length > 1){
35
    my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter"));
39
    my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter"));
40
41
    if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) {
42
      my @prefix = $res->{ "prefix" };
43
      @prefix = split(',', $prefix[0]);
44
45
      for (my $i = 0; $i < scalar @prefix; $i++) {
46
        filterAutocomplete($res->{ $i }->{ 'hits' });
47
      }
48
    }
49
36
    print $cgi->header("application/json");
50
    print $cgi->header("application/json");
37
    print to_json($res);
51
    print to_json($res);
38
  }
52
  }
39
  #search by one prefix field
53
  #search by one prefix field
40
  elsif ($length == 1) {
54
  elsif ($length == 1) {
41
    my $res = $browser->autocomplete_one_idx($ses, $prefix[0], $session->param("analyzer"), $session->param("token_counter"));
55
    my $res = $browser->autocomplete_one_idx($ses, $prefix[0], $session->param("analyzer"), $session->param("token_counter"));
56
57
    if (C4::Context->preference('OpacSuppression') || C4::Context->yaml_preference('OpacHiddenItems')) {
58
      filterAutocomplete($res->{ 'hits' });
59
    }
60
42
    print $cgi->header("application/json");
61
    print $cgi->header("application/json");
43
    print to_json($res);
62
    print to_json($res);
44
  }
63
  }
Lines 52-57 if ($iskey == 0) { Link Here
52
  response404JSON();
71
  response404JSON();
53
}
72
}
54
73
74
sub filterAutocomplete {
75
  my $hits = $_[0];
76
  my $hitlist = $hits->{ "hits" };
77
  if (@{$hitlist}) {
78
    # Remove item inside hits in elasticsearch response if the item has
79
    # marc field 942$n set to true and OpacSuppression preference on
80
    if (C4::Context->preference('OpacSuppression')) {
81
      for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) {
82
        my $record = GetMarcBiblio({
83
          biblionumber => $hitlist->[$i]->{ "_id" },
84
          opac         => 1
85
        });
86
        my $opacsuppressionfield = '942';
87
        my $opacsuppressionfieldvalue = $record->field($opacsuppressionfield);
88
        if ( $opacsuppressionfieldvalue &&
89
             $opacsuppressionfieldvalue->subfield("n") &&
90
             $opacsuppressionfieldvalue->subfield("n") == 1) {
91
          # if OPAC suppression by IP address
92
          if (C4::Context->preference('OpacSuppressionByIPRange')) {
93
            my $IPAddress = $ENV{'REMOTE_ADDR'};
94
            my $IPRange = C4::Context->preference('OpacSuppressionByIPRange');
95
            if ($IPAddress !~ /^$IPRange/)  {
96
                splice(@{$hitlist}, $i, 1);
97
                $i--;
98
                $hits->{ "total" }--;
99
            }
100
          } else {
101
            splice(@{$hitlist}, $i, 1);
102
            $i--;
103
            $hits->{ "total" }--;
104
          }
105
        }
106
      }
107
    }
108
    # Remove item inside hits in elasticsearch response if the item is
109
    # declared hidden in OPACHiddenItems preference
110
    if (C4::Context->yaml_preference('OpacHiddenItems')) {
111
      my @biblionumbers;
112
      foreach (@{$hitlist}) {
113
        push(@biblionumbers, $_->{ "_id" });
114
      }
115
      my $autocomplete_items = Koha::Items->search({
116
        biblionumber => { -in => \@biblionumbers }
117
      });
118
      my $filtered_items = $autocomplete_items->filter_by_visible_in_opac({
119
        patron => undef
120
      });
121
      for ( my $i = 0; $i < scalar @{$hitlist}; $i++ ) {
122
        my $item = $filtered_items->find({
123
          biblionumber => $hitlist->[$i]->{ "_id" }
124
        });
125
        if (!$item) {
126
          splice(@{$hitlist}, $i, 1);
127
          $i--;
128
          $hits->{ "total" }--;
129
        }
130
      }
131
    }
132
    # Adjust the max_score inside hits in elasticsearch response
133
    my $maxscore = 0;
134
    foreach ( @{$hitlist} ) {
135
      my $score = $_->{"_score"};
136
      $maxscore = $score if ($maxscore < $score);
137
    }
138
    if ($maxscore == 0) {
139
      $hits->{ "max_score" } = undef;
140
    } else {
141
      $hits->{ "max_score" } = $maxscore;
142
    }
143
  }
144
}
145
55
sub response404JSON {
146
sub response404JSON {
56
  my $res = CGI->new;
147
  my $res = CGI->new;
57
  my $json = JSON->new->utf8;
148
  my $json = JSON->new->utf8;
58
- 

Return to bug 27113