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