1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Search::Estraier; |
use Search::Estraier 0.06; |
5 |
use DBI; |
use DBI; |
6 |
use Data::Dumper; |
use Data::Dumper; |
7 |
use Encode qw/from_to/; |
use Encode qw/from_to/; |
8 |
|
use Time::HiRes qw/time/; |
9 |
|
|
10 |
=head1 NAME |
=head1 NAME |
11 |
|
|
13 |
|
|
14 |
=cut |
=cut |
15 |
|
|
16 |
my $node_url = 'http://localhost:1978/node/dbi'; |
my $c = { |
17 |
my $dbi = 'Pg:dbname=azop'; |
node_url => 'http://localhost:1978/node/dbi', |
18 |
my $sql = qq{ |
dbi => 'Pg:dbname=azop', |
19 |
select * from history_collection_view_cache |
sql => qq{ |
20 |
|
select * from history_collection_view_cache |
21 |
|
}, |
22 |
|
pk_col => '_id', |
23 |
|
db_encoding => 'iso-8859-2', |
24 |
|
debug => 0, |
25 |
}; |
}; |
|
my $debug = 0; |
|
26 |
|
|
27 |
# create and configure node |
# create and configure node |
28 |
my $node = new Search::Estraier::Node( |
my $node = new Search::Estraier::Node( |
29 |
url => $node_url, |
url => $c->{node_url}, |
30 |
user => 'admin', |
user => 'admin', |
31 |
passwd => 'admin', |
passwd => 'admin', |
32 |
croak_on_error => 1, |
croak_on_error => 1, |
33 |
debug => $debug, |
create => 1, |
34 |
|
debug => $c->{debug}, |
35 |
); |
); |
36 |
|
|
37 |
# create DBI connection |
# create DBI connection |
38 |
my $dbh = DBI->connect("DBI:$dbi","","") || die $DBI::errstr; |
my $dbh = DBI->connect("DBI:$c->{dbi}","","") || die $DBI::errstr; |
39 |
|
|
40 |
my $sth = $dbh->prepare($sql) || die $dbh->errstr(); |
my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr(); |
41 |
$sth->execute() || die $sth->errstr(); |
$sth->execute() || die $sth->errstr(); |
42 |
|
|
43 |
warn "# columns: ",join(",",@{ $sth->{NAME} }),"\n" if ($debug); |
warn "# columns: ",join(",",@{ $sth->{NAME} }),"\n" if ($c->{debug}); |
44 |
|
|
45 |
my $total = $sth->rows; |
my $total = $sth->rows; |
46 |
my $i = 1; |
my $i = 1; |
47 |
|
|
48 |
|
my $t = time(); |
49 |
|
my $pk_col = $c->{pk_col} || 'id'; |
50 |
|
|
51 |
while (my $row = $sth->fetchrow_hashref() ) { |
while (my $row = $sth->fetchrow_hashref() ) { |
52 |
|
|
53 |
warn "# row: ",Dumper($row) if ($debug); |
warn "# row: ",Dumper($row) if ($c->{debug}); |
54 |
|
|
55 |
# create document |
# create document |
56 |
my $doc = new Search::Estraier::Document; |
my $doc = new Search::Estraier::Document; |
57 |
|
|
58 |
$doc->add_attr('@uri', $row->{_id}); |
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; |
printf "%4d ",$i; |
65 |
|
|
67 |
|
|
68 |
if ($val) { |
if ($val) { |
69 |
# change encoding? |
# change encoding? |
70 |
from_to($val, 'ISO-8859-2', 'UTF-8'); |
from_to($val, ($c->{db_encoding} || 'ISO-8859-1'), 'UTF-8'); |
71 |
|
|
72 |
# add attributes (make column usable from attribute search) |
# add attributes (make column usable from attribute search) |
73 |
$doc->add_attr($col, $val); |
$doc->add_attr($col, $val); |
82 |
|
|
83 |
} |
} |
84 |
|
|
85 |
print " ", int(( $i++ / $total) * 100), "%\n"; |
warn "# doc draft: ",$doc->dump_draft, "\n" if ($c->{debug}); |
|
|
|
|
warn "# doc draft: ",$doc->dump_draft, "\n" if ($debug); |
|
86 |
|
|
87 |
die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) }); |
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 |
} |
} |