/[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 183 - (show annotations)
Thu Aug 31 14:43:06 2006 UTC (17 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 2173 byte(s)
separate authorisation for estraier (estuser,estpasswd) and database (dbuser,dbpasswd)

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26