3 # Copyright 2013 Catalyst
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 # TODO this should use the moose thing that auto-picks.
29 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
30 use Koha::ElasticSearch::Search;
35 my $template_type = "basic";
36 if ( $cgi->param("idx") or $cgi->param("q") ) {
37 $template_name = 'search/results.tt';
40 $template_name = 'search/advsearch.tt';
41 $template_type = 'advsearch';
45 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
47 template_name => $template_name,
54 my $format = $cgi->param("format") || 'html';
57 my $itemtypes = GetItemTypes;
59 my $page = $cgi->param("page") || 1;
62 || C4::Context->preference('OPACnumSearchResults')
64 my $q = $cgi->param("q");
66 my $searcher = Koha::ElasticSearch::Search->new();
67 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new();
69 if ($cgi->param('type') eq 'browse') {
70 $query = $builder->build_browse_query($cgi->param('browse_field') || undef, $q );
71 $template_params{browse} = 1;
73 $query = $builder->build_query($q);
75 my $results = $searcher->search( $query, $page, $count );
76 #my $results = $searcher->search( { "match_phrase_prefix" => { "title" => "the" } } );
78 # This is temporary, but will do the job for now.
81 push @hits, { _source => @_[0] };
83 # Make a list of the page numbers
84 my @pages = map { { page => $_, current => ($_ == ( $page || 1)) } } 1 .. int($results->total / $count);
85 my $max_page = int($results->total / $count);
86 # Pager template params
88 SEARCH_RESULTS => \@hits,
89 PAGE_NUMBERS => \@pages,
90 total => $results->total,
91 previous_page => ( $page > 1 ? $page - 1 : undef ),
92 next_page => ( $page < $max_page ? $page + 1 : undef ),
94 { var => 'type', val => $cgi->param('type') },
95 { var => 'q', val => $q },
96 { var => 'count', val => $count },
101 my $content_type = ( $format eq 'rss' or $format eq 'atom' ) ? $format : 'html';
102 output_with_http_headers $cgi, $cookie, $template->output, $content_type;