normalizing API, using biblionumber everywhere
[koha.git] / koha-plucene / indexer.pl
1 #!/usr/bin/perl -w
2
3 # This script will build an index of all the biblios in a koha database
4 # Its using english stemming at the moment. But that can be changed and is only
5 # indexing author and title
6
7 # Combine this with the search.cgi script to search Koha using Plucene
8 # This is still a work in progress, use with caution
9
10 # $Id$
11
12 # Copyright 2005 Katipo Communications
13 #
14 # This file is part of Koha.
15 #
16 # Koha is free software; you can redistribute it and/or modify it under the
17 # terms of the GNU General Public License as published by the Free Software
18 # Foundation; either version 2 of the License, or (at your option) any later
19 # version.
20 #
21 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License along with
26 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
27 # Suite 330, Boston, MA  02111-1307 USA
28
29 use lib '/usr/local/koha/intranet/modules';
30 use strict;
31 use C4::Context;
32 use Plucene::Index::Writer;
33 use Plucene::Plugin::Analyzer::PorterAnalyzer;
34 use Plucene::Document;
35
36 # connect to the database and fetch all the biblios
37 my $dbh = C4::Context->dbh();
38
39 my $query = "SELECT * FROM biblio";
40 my $sth   = $dbh->prepare($query);
41
42 $sth->execute();
43
44 # create an index writer
45 # currently it makes the index in /tmp/plucene
46 # PLEASE change this if you want to use the script in production
47 my $writer = Plucene::Index::Writer->new(
48     "/tmp/plucene",
49     Plucene::Plugin::Analyzer::PorterAnalyzer->new(),
50     1    # Create the index from scratch
51 );
52
53 # For each biblio, add its information to the index
54
55 while ( my $data = $sth->fetchrow_hashref() ) {
56     my $doc = Plucene::Document->new();
57     $doc->add(
58         Plucene::Document::Field->Keyword( filename => $data->{biblionumber} )
59     );
60     $doc->add( Plucene::Document::Field->Text( title  => $data->{'title'} ) );
61     $doc->add( Plucene::Document::Field->Text( author => $data->{'author'} ) );
62     $writer->add_document($doc);
63 }
64