First cut of a search engine using Plucene
[koha.git] / koha-plucene / search.cgi
1 #!/usr/bin/perl
2
3 # script to search the plucene index of the database
4 # most of this will be shifted to a module when it moves out of the proof of concept stage
5
6 # $Id$
7
8 # Copyright 2005 Katipo Communications
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23
24 use strict;
25
26 use Plucene::Search::IndexSearcher;
27 use Plucene::Plugin::Analyzer::PorterAnalyzer;
28 use Plucene::QueryParser;
29 use Plucene::Search::HitCollector;
30
31 use C4::Auth;
32 use C4::Interface::CGI::Output;
33
34 use Data::Dumper;
35
36 use CGI;
37 my $cgi = new CGI;
38
39 # get a template, opac-pluceneresults.tmpl is currently an exact copy of
40 # opac-searchresults.tmpl so just make a copy.
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name   => "opac-pluceneresults.tmpl",
44         query           => $cgi,
45         type            => "opac",
46         authnotrequired => 1,
47     }
48 );
49
50 # the script expects an input called query;
51 my $query = $cgi->param('query');
52
53 # tell the script what index to use (change this to match whatever is in indexer.pl)
54 my $searcher = Plucene::Search::IndexSearcher->new("/tmp/plucene/");
55
56 # the important bit here is default=>"title"
57 # that says if we dont specify what to search, search the title field
58 my $parser = Plucene::QueryParser->new(
59     {
60         analyzer => Plucene::Plugin::Analyzer::PorterAnalyzer->new(),
61         default  => "title"
62     }
63 );
64
65 my $parsed = $parser->parse($query);
66
67 my @docs;
68
69 # build an array of results,
70 # we could use the $score to rank them, but its currently not doing that
71 my $hc = Plucene::Search::HitCollector->new(
72     collect => sub {
73         my ( $self, $doc, $score ) = @_;
74         my $res = eval { $searcher->doc($doc) };
75         push @docs, $res if $res;
76     }
77 );
78
79 # do the searh
80 $searcher->search_hc( $parsed, $hc );
81
82 # map the results into a format our template is expecting
83 my @results = map {
84     {
85         biblionumber => $_->get("filename")->string,
86         title        => $_->get("title")->string,
87         author       => $_->get("author")->string,
88     }
89 } @docs;
90
91 # pass the results to the template
92 my $num_records = @results;
93 $template->param(
94     search_results => \@results,
95     numrecords     => $num_records,
96     searchdesc     => $query
97 );
98 output_html_with_http_headers $cgi, $cookie, $template->output;