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