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