Bug 12478: Remove Solr occurrences reintroduced by a previous patch
[koha.git] / opac / elasticsearch.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 Catalyst
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use C4::Context;
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Output;
27
28 # TODO this should use the moose thing that auto-picks.
29 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
30 use Koha::ElasticSearch::Search;
31
32 my $cgi = new CGI;
33
34 my $template_name;
35 my $template_type = "basic";
36 if ( $cgi->param("idx") or $cgi->param("q") ) {
37     $template_name = 'search/results.tt';
38 }
39 else {
40     $template_name = 'search/advsearch.tt';
41     $template_type = 'advsearch';
42 }
43
44 # load the template
45 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
46     {
47         template_name   => $template_name,
48         query           => $cgi,
49         type            => "opac",
50         authnotrequired => 1,
51     }
52 );
53 my %template_params;
54 my $format = $cgi->param("format") || 'html';
55
56 # load the Type stuff
57 my $itemtypes = GetItemTypes;
58
59 my $page = $cgi->param("page") || 1;
60 my $count =
61      $cgi->param('count')
62   || C4::Context->preference('OPACnumSearchResults')
63   || 20;
64 my $q = $cgi->param("q");
65
66 my $searcher = Koha::ElasticSearch::Search->new();
67 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new();
68 my $query;
69 if ($cgi->param('type') eq 'browse') {
70     $query = $builder->build_browse_query($cgi->param('browse_field') || undef, $q );
71     $template_params{browse} = 1;
72 } else {
73     $query = $builder->build_query($q);
74 }
75 my $results = $searcher->search( $query, $page, $count );
76 #my $results = $searcher->search( { "match_phrase_prefix" => { "title" => "the" } } );
77
78 # This is temporary, but will do the job for now.
79 my @hits;
80 $results->each(sub {
81         push @hits, { _source => @_[0] };
82     });
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
87 $template->param(
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 ),
93     follower_params => [
94         { var => 'type',  val => $cgi->param('type') },
95         { var => 'q',     val => $q },
96         { var => 'count', val => $count },
97     ],
98     %template_params,
99 );
100
101 my $content_type = ( $format eq 'rss' or $format eq 'atom' ) ? $format : 'html';
102 output_with_http_headers $cgi, $cookie, $template->output, $content_type;