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