|
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 |
|
| 12 |
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } ); |
15 |
my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } ); |
| 13 |
my $cgi = CGI->new; |
16 |
my $cgi = CGI->new; |
| 14 |
my $session = CGI::Session->load() or die CGI::Session->errstr(); |
17 |
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 |
36 |
#search by many prefix fields |
| 34 |
if ($length > 1){ |
37 |
if ($length > 1){ |
| 35 |
my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter")); |
38 |
my $res = $browser->autocomplete_idx($ses, \@prefix, $session->param("analyzer"), $session->param("token_counter")); |
|
|
39 |
|
| 40 |
if (C4::Context->yaml_preference('OpacHiddenItems')) { |
| 41 |
my @prefix = $res->{ "prefix" }; |
| 42 |
@prefix = split(',', $prefix[0]); |
| 43 |
|
| 44 |
my %biblionumbers; |
| 45 |
for (my $i = 0; $i < scalar @prefix; $i++) { |
| 46 |
my @hits = @{$res->{ "$i" }->{ "hits" }->{ "hits" }}; |
| 47 |
if (@hits) { |
| 48 |
for (my $j = 0; $j < scalar @hits; $j++) { |
| 49 |
# biblionumber->prefix position = hit position within prefix |
| 50 |
$biblionumbers{ $hits[$j]->{ "_id" } }{ $i } = $j; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (%biblionumbers) { |
| 56 |
my @biblionumbers = keys %biblionumbers; |
| 57 |
my $autocomplete_items = Koha::Items->search({ |
| 58 |
biblionumber => { -in => \@biblionumbers } |
| 59 |
}); |
| 60 |
my $filtered_items = $autocomplete_items->filter_by_visible_in_opac({ |
| 61 |
patron => undef |
| 62 |
}); |
| 63 |
|
| 64 |
foreach (@biblionumbers) { |
| 65 |
my $biblionumber = $_; |
| 66 |
my $item = $filtered_items->find({ |
| 67 |
biblionumber => $biblionumber |
| 68 |
}); |
| 69 |
if (!$item) { |
| 70 |
my %removelist = %{$biblionumbers{ $biblionumber }}; |
| 71 |
foreach (keys %removelist) { |
| 72 |
my $hits = $res->{ $_ }->{ "hits" }->{ "hits" }; |
| 73 |
splice(@{$hits}, $removelist{ $_ }, 1); |
| 74 |
$res->{ $_ }->{ "hits" }->{ "total" }--; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
for (my $i = 0; $i < scalar @prefix; $i++) { |
| 79 |
if ($res->{ $i }->{ "hits" }->{ "max_score" }) { |
| 80 |
my $hits = $res->{ $i }->{ "hits" }->{ "hits" }; |
| 81 |
my $maxscore = 0; |
| 82 |
foreach ( @{$hits} ) { |
| 83 |
my $score = $_->{"_score"}; |
| 84 |
$maxscore = $score if ($maxscore < $score); |
| 85 |
} |
| 86 |
$res->{ $i }->{ "hits" }->{ "max_score" } = $maxscore; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 36 |
print $cgi->header("application/json"); |
92 |
print $cgi->header("application/json"); |
| 37 |
print to_json($res); |
93 |
print to_json($res); |
| 38 |
} |
94 |
} |
| 39 |
#search by one prefix field |
95 |
#search by one prefix field |
| 40 |
elsif ($length == 1) { |
96 |
elsif ($length == 1) { |
| 41 |
my $res = $browser->autocomplete_one_idx($ses, $prefix[0], $session->param("analyzer"), $session->param("token_counter")); |
97 |
my $res = $browser->autocomplete_one_idx($ses, $prefix[0], $session->param("analyzer"), $session->param("token_counter")); |
|
|
98 |
|
| 99 |
if (C4::Context->yaml_preference('OpacHiddenItems')) { |
| 100 |
my $hits = $res->{ "hits" }->{ "hits" }; |
| 101 |
if (@{$hits}) { |
| 102 |
my @biblionumbers; |
| 103 |
foreach ( @{$hits} ) { |
| 104 |
push(@biblionumbers, $_->{"_id"}); |
| 105 |
} |
| 106 |
|
| 107 |
my $autocomplete_items = Koha::Items->search({ |
| 108 |
biblionumber => { -in => \@biblionumbers } |
| 109 |
}); |
| 110 |
my $filtered_items = $autocomplete_items->filter_by_visible_in_opac({ |
| 111 |
patron => undef |
| 112 |
}); |
| 113 |
|
| 114 |
for ( my $i = 0; $i < scalar @biblionumbers; $i++ ) { |
| 115 |
my $item = $filtered_items->find({ |
| 116 |
biblionumber => $biblionumbers[$i] |
| 117 |
}); |
| 118 |
if (!$item) { |
| 119 |
splice(@{$hits}, $i, 1); |
| 120 |
$res->{ "hits" }->{ "total" }--; |
| 121 |
} |
| 122 |
} |
| 123 |
my $maxscore = 0; |
| 124 |
foreach ( @{$hits} ) { |
| 125 |
my $score = $_->{"_score"}; |
| 126 |
$maxscore = $score if ($maxscore < $score); |
| 127 |
} |
| 128 |
$res->{ "hits" }->{ "max_score" } = $maxscore; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 42 |
print $cgi->header("application/json"); |
132 |
print $cgi->header("application/json"); |
| 43 |
print to_json($res); |
133 |
print to_json($res); |
| 44 |
} |
134 |
} |
| 45 |
- |
|
|