3 # This is a CGI script that handles the browse feature.
5 # Copyright 2015 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use Koha::SearchEngine::Elasticsearch;
30 use Koha::SearchEngine::Elasticsearch::Browse;
31 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
32 use Koha::SearchEngine::Elasticsearch::Search;
38 binmode STDOUT, ':encoding(UTF-8)';
40 # If calling via JS, 'api' is used to route to correct step in process
41 my $api = $query->param('api');
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46 template_name => "opac-browse.tt",
49 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
53 output_html_with_http_headers $query, $cookie, $template->output;
57 elsif ( $api eq 'GetSuggestions' ) {
58 my $fuzzie = $query->param('fuzziness');
59 my $prefix = $query->param('prefix');
60 my $field = $query->param('field');
62 # Under a persistent environment, we should probably not reinit this every time.
63 my $browser = Koha::SearchEngine::Elasticsearch::Browse->new( { index => 'biblios' } );
64 my $res = $browser->browse( $prefix, $field, { fuzziness => $fuzzie } );
68 grep { !$seen{$_->{text}}++ }
69 sort { lc($a->{text}) cmp lc($b->{text}) } @$res;
71 -type => 'application/json',
74 print to_json( \@sorted );
76 elsif ( $api eq 'GetResults' ) {
77 my $term = $query->param('term');
78 my $field = $query->param('field');
80 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
81 my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
82 { index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX } );
84 my $query = { query => { term => { $field.".raw" => $term } } } ;
85 my $results = $searcher->search( $query, undef, 500 );
86 my @output = _filter_for_output( $results->{hits}->{hits} );
88 -type => 'application/json',
91 print to_json( \@output );
94 # This should probably be done with some templatey gizmo
96 sub _filter_for_output {
99 foreach my $rec (@$records) {
100 my $biblionumber = $rec->{_id};
101 my $biblio = Koha::Biblios->find( $biblionumber );
106 title => $biblio->title,
107 author => $biblio->author,
110 my @sorted = sort { lc($a->{title}) cmp lc($b->{title}) } @output;