/[Search-Estraier]/trunk/scripts/dbi-indexer.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/scripts/dbi-indexer.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 195 - (show annotations)
Tue Nov 14 16:39:08 2006 UTC (17 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 2287 byte(s)
added new --dbi and --quiet command-line options, saner defaults

1 #!/usr/bin/perl -w
2
3 use strict;
4 use Search::Estraier 0.06;
5 use DBI;
6 use Data::Dumper;
7 use Encode qw/from_to/;
8 use Time::HiRes qw/time/;
9 use Getopt::Long;
10
11 =head1 NAME
12
13 dbi-indexer.pl - example indexer of DBI sources for Search::Estraier
14
15 =cut
16
17 my $c = {
18 node_url => 'http://localhost:1978/node/dbi-template1',
19 dbi => 'Pg:dbname=template1',
20 dbuser => 'postgres',
21 sql => qq{
22 select * from pg_database
23 },
24 pk_col => 'datname',
25 db_encoding => 'iso-8859-2',
26 debug => 0,
27 estuser => 'admin',
28 estpasswd => 'admin',
29 quiet => 0,
30 };
31
32 GetOptions($c, qw/node_url=s dbi=s sql=s pk_col=s eb_encoding=s debug+ quiet+ estuser=s estpasswd=s dbuser=s dbpasswd=s/);
33
34 warn "# c: ", Dumper($c) if ($c->{debug});
35
36 # create and configure node
37 my $node = new Search::Estraier::Node(
38 url => $c->{node_url},
39 user => $c->{estuser},
40 passwd => $c->{estpasswd},
41 croak_on_error => 1,
42 create => 1,
43 debug => $c->{debug} >= 4 ? 1 : 0,
44 );
45
46 # create DBI connection
47 my $dbh = DBI->connect("DBI:$c->{dbi}", $c->{dbuser}, $c->{dbpasswd}) || die $DBI::errstr;
48
49 my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr();
50 $sth->execute() || die $sth->errstr();
51
52 warn "# columns: ",join(",",@{ $sth->{NAME} }),"\n" if ($c->{debug});
53
54 my $total = $sth->rows;
55 my $i = 1;
56
57 my $t = time();
58 my $pk_col = $c->{pk_col} || 'id';
59
60 while (my $row = $sth->fetchrow_hashref() ) {
61
62 warn "# row: ",Dumper($row) if ($c->{debug} >= 3);
63
64 # create document
65 my $doc = new Search::Estraier::Document;
66
67 if (my $id = $row->{$pk_col}) {
68 $doc->add_attr('@uri', $id);
69 } else {
70 die "can't find pk_col column '$pk_col' in results\n";
71 }
72
73 my $out = '';
74 $out .= sprintf("%4d ",$i);
75
76 while (my ($col,$val) = each %{$row}) {
77
78 if ($val) {
79 # change encoding?
80 from_to($val, ($c->{db_encoding} || 'ISO-8859-1'), 'UTF-8');
81
82 # add attributes (make column usable from attribute search)
83 $doc->add_attr($col, $val);
84
85 # add body text to document (make it searchable using full-text index)
86 $doc->add_text($val);
87
88 $out .= "R";
89 } else {
90 $out .= ".";
91 }
92
93 }
94
95 warn "# doc draft: ",$doc->dump_draft, "\n" if ($c->{debug} >= 2);
96
97 die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) });
98
99 printf ("%s %d%% %.1f/s\n", $out, int(( $i++ / $total) * 100), ( $i / (time() - $t) ) ) unless ($c->{quiet});
100
101 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26