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
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
29 use Koha::SearchEngine::Elasticsearch;
30 use Koha::SearchEngine::Elasticsearch::Browse;
31 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
32 use Koha::SearchEngine::Elasticsearch::Search;
34 use JSON qw( to_json );
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 subtitle => $biblio->subtitle,
108 author => $biblio->author,
111 my @sorted = sort { lc($a->{title}) cmp lc($b->{title}) } @output;