3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common WebPAC::Output Class::Accessor/; |
7 |
|
__PACKAGE__->mk_accessors(qw( |
8 |
use KinoSearch::InvIndexer; |
path |
9 |
use KinoSearch::Analysis::PolyAnalyzer; |
database |
10 |
use Encode qw/from_to/; |
input |
11 |
use Data::Dumper; |
encoding |
12 |
|
clean |
13 |
|
|
14 |
|
index |
15 |
|
)); |
16 |
|
|
17 |
|
use KinoSearch::Simple; |
18 |
|
use File::Path; |
19 |
|
use Encode qw/decode/; |
20 |
|
use Data::Dump qw/dump/; |
21 |
use Storable; |
use Storable; |
22 |
|
|
23 |
=head1 NAME |
=head1 NAME |
26 |
|
|
27 |
=head1 VERSION |
=head1 VERSION |
28 |
|
|
29 |
Version 0.03 |
Version 0.05 |
30 |
|
|
31 |
=cut |
=cut |
32 |
|
|
33 |
our $VERSION = '0.03'; |
our $VERSION = '0.05'; |
34 |
|
|
35 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
36 |
|
|
43 |
|
|
44 |
Open KinoSearch index |
Open KinoSearch index |
45 |
|
|
46 |
my $est = new WebPAC::Output::KinoSearch( |
my $out = new WebPAC::Output::KinoSearch({ |
47 |
index_path => '/path/to/invindex', |
path => '/path/to/invindex', |
|
fields => qw/name of all filelds used/, |
|
48 |
database => 'demo', |
database => 'demo', |
|
label => 'node label', |
|
49 |
encoding => 'iso-8859-2', |
encoding => 'iso-8859-2', |
50 |
clean => 1, |
clean => 1, |
51 |
); |
}); |
52 |
|
|
53 |
Options are: |
Options are: |
54 |
|
|
55 |
=over 4 |
=over 4 |
56 |
|
|
57 |
=item index_path |
=item path |
58 |
|
|
59 |
path to KinoSearch index to use |
path to KinoSearch index to use |
60 |
|
|
|
=item fields |
|
|
|
|
|
name of all fields used in this index |
|
|
|
|
61 |
=item database |
=item database |
62 |
|
|
63 |
name of database from which data comes |
name of database from which data comes |
64 |
|
|
|
=item label |
|
|
|
|
|
label for node (optional) |
|
|
|
|
65 |
=item encoding |
=item encoding |
66 |
|
|
67 |
character encoding of C<data_structure> if it's differenet than C<ISO-8859-2> |
character encoding of C<data_structure> if it's differenet than C<ISO-8859-2> |
70 |
|
|
71 |
=back |
=back |
72 |
|
|
73 |
|
=head2 init |
74 |
|
|
75 |
|
$out->init; |
76 |
|
|
77 |
=cut |
=cut |
78 |
|
|
79 |
sub new { |
sub init { |
80 |
my $class = shift; |
my $self = shift; |
|
my $self = {@_}; |
|
|
bless($self, $class); |
|
81 |
|
|
82 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
83 |
|
|
84 |
#$log->debug("self: ", sub { Dumper($self) }); |
#$log->debug("self: ", sub { dump($self) }); |
85 |
|
|
86 |
foreach my $p (qw/index_path fields database/) { |
foreach my $p (qw/path database/) { |
87 |
$log->logdie("need $p") unless ($self->{$p}); |
$log->logdie("need $p") unless ($self->$p); |
88 |
} |
} |
89 |
|
|
90 |
$log->logdie("fields is not ARRAY") unless (ref($self->{fields}) eq 'ARRAY'); |
# $log->logdie("fields is not ARRAY") unless (ref($self->{fields}) eq 'ARRAY'); |
91 |
|
|
92 |
$self->{encoding} ||= 'ISO-8859-2'; |
$self->encoding( 'ISO-8859-2' ) unless $self->encoding; |
93 |
|
|
94 |
$self->{index_path} .= '/' . $self->{database}; |
## FIXME we shouldn't re-create whole KinoSearch index every time! |
95 |
|
# $self->clean( 1 ); |
96 |
|
|
97 |
$self->{clean} = 1 if (! -e $self->{index_path} . '/segments'); |
if ( ! -e $self->path ) { |
98 |
|
mkpath $self->path || $log->logdie("can't create ", $self->path,": $!"); |
99 |
|
$log->info("created ", $self->path); |
100 |
|
} elsif ( $self->clean ) { |
101 |
|
$log->info("removing existing ", $self->path); |
102 |
|
rmtree $self->path || $log->logdie("can't remove ", $self->path,": $!"); |
103 |
|
mkpath $self->path || $log->logdie("can't create ", $self->path,": $!"); |
104 |
|
} |
105 |
|
|
106 |
$log->info("using", $self->{clean} ? ' new' : '', " index $self->{index_path} with encoding $self->{encoding}"); |
my $path = $self->path . '/' . $self->database; |
107 |
|
|
108 |
my $analyzer = KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' ); |
$log->info("using index $path with encoding ", $self->encoding); |
109 |
|
|
110 |
$self->{invindex} = KinoSearch::InvIndexer->new( |
my $index = KinoSearch::Simple->new( |
111 |
invindex => $self->{index_path}, |
path => $path, |
112 |
create => $self->{clean}, |
language => 'en', |
|
analyzer => $analyzer, |
|
113 |
); |
); |
114 |
|
|
115 |
my $fields_path = $self->{index_path} . '/fields.storable'; |
$log->logdie("can't open $path: $!") unless $index; |
|
$fields_path =~ s#//#/#g; |
|
|
if (-e $fields_path) { |
|
|
$self->{fields} = retrieve($fields_path) || |
|
|
$log->warn("can't open $fields_path: $!"); |
|
|
} else { |
|
|
$log->error("This will be dummy run since no fields statistics are found!"); |
|
|
$log->error("You will have to re-run indexing to get search results!"); |
|
|
$self->{dummy_run} = 1; |
|
|
} |
|
|
$self->{fields_path} = $fields_path; |
|
116 |
|
|
117 |
foreach my $f (@{ $self->{fields} }) { |
$self->index( $index ); |
|
$self->{invindex}->spec_field( |
|
|
name => $f, |
|
|
# boost => 10, |
|
|
stored => 1, |
|
|
indexed => 1, |
|
|
vectorized => 0, |
|
|
); |
|
|
} |
|
118 |
|
|
|
$self ? return $self : return undef; |
|
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
=head2 add |
=head2 add |
123 |
|
|
124 |
Adds one entry to database. |
Adds one entry |
125 |
|
|
126 |
$est->add( |
$out->add( 42, $ds ); |
|
id => 42, |
|
|
ds => $ds, |
|
|
type => 'display', |
|
|
text => 'optional text from which snippet is created', |
|
|
); |
|
|
|
|
|
This function will create entries in index using following URI format: |
|
|
|
|
|
C<file:///type/database%20name/000> |
|
|
|
|
|
Each tag in C<data_structure> with specified C<type> will create one |
|
|
attribute and corresponding hidden text (used for search). |
|
127 |
|
|
128 |
=cut |
=cut |
129 |
|
|
130 |
sub add { |
sub add { |
131 |
my $self = shift; |
my $self = shift; |
132 |
|
|
133 |
my $args = {@_}; |
my ( $id, $ds ) = @_; |
134 |
|
|
135 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
136 |
|
$log->logdie("need id") unless defined $id; |
137 |
|
$log->logdie("need ds") unless $ds; |
138 |
|
|
139 |
my $database = $self->{'database'} || $log->logconfess('no database in $self'); |
my $hash = $self->ds_to_hash( $ds, 'search' ) || return; |
|
$log->logconfess('need invindex in object') unless ($self->{'invindex'}); |
|
|
|
|
|
foreach my $p (qw/id ds type/) { |
|
|
$log->logdie("need $p") unless ($args->{$p}); |
|
|
} |
|
|
|
|
|
my $type = $args->{'type'}; |
|
|
my $id = $args->{'id'}; |
|
|
|
|
|
my $uri = "file:///$type/$database/$id"; |
|
|
$log->debug("creating $uri"); |
|
|
|
|
|
my $doc = $self->{invindex}->new_doc( $uri ) || $log->logdie("can't create new_doc( $uri )"); |
|
140 |
|
|
141 |
sub _add_value($$$$$) { |
$hash->{id} ||= $id; |
142 |
my ($self,$log,$doc,$n,$v) = @_; |
$hash->{database} ||= $self->database; |
143 |
return unless ($v); |
$hash->{input} ||= $self->input; |
144 |
|
|
145 |
$self->{value_usage}->{$n}++; |
foreach my $f ( keys %$hash ) { |
146 |
return if ($self->{dummy_run}); |
if ( ref($hash->{$f}) eq 'ARRAY' ) { |
147 |
|
$hash->{$f} = join(' <*> ', @{ $hash->{$f} }); |
148 |
eval { $doc->set_value($n, $self->convert($v) ) }; |
} |
149 |
$log->warn("can't insert: $n = $v") if ($@); |
# $hash->{$f} = decode( $self->encoding, $hash->{$f} ); |
150 |
|
$self->{field_count}->{$f}++; |
151 |
} |
} |
152 |
|
|
153 |
_add_value($self,$log,$doc, 'uri', $uri); |
$log->debug("add( $id, ", sub { dump($ds) }," ) => ", sub { dump( $hash ) }); |
|
|
|
|
$log->debug("ds = ", sub { Dumper($args->{'ds'}) } ); |
|
154 |
|
|
155 |
# filter all tags which have type defined |
$self->index->add_doc( $hash ); |
|
my @tags = grep { |
|
|
ref($args->{'ds'}->{$_}) eq 'HASH' && defined( $args->{'ds'}->{$_}->{$type} ) |
|
|
} keys %{ $args->{'ds'} }; |
|
156 |
|
|
157 |
$log->debug("tags = ", join(",", @tags)); |
$self->{count}++; |
|
|
|
|
return unless (@tags); |
|
|
|
|
|
foreach my $tag (@tags) { |
|
|
|
|
|
my $vals = join(" ", @{ $args->{'ds'}->{$tag}->{$type} }); |
|
|
|
|
|
next if (! $vals); |
|
|
|
|
|
$vals = $self->convert( $vals ) or |
|
|
$log->logdie("can't convert '$vals' to UTF-8"); |
|
|
|
|
|
_add_value($self, $log, $doc, $tag, $vals ); |
|
|
} |
|
|
|
|
|
if (my $text = $args->{'text'}) { |
|
|
_add_value($self, $log, $doc, 'bodytext', $text ); |
|
|
} |
|
|
|
|
|
#$log->debug("adding ", sub { $doc->dump_draft } ); |
|
|
$self->{invindex}->add_doc($doc) || $log->warn("can't add document $uri"); |
|
158 |
|
|
159 |
return 1; |
return 1; |
160 |
} |
} |
163 |
|
|
164 |
Close index |
Close index |
165 |
|
|
166 |
$index->finish; |
$out->finish; |
167 |
|
|
168 |
=cut |
=cut |
169 |
|
|
172 |
|
|
173 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
174 |
|
|
175 |
$log->info("finish index writing to disk"); |
$log->info("indexed ", $self->{count}, " records"); |
|
$self->{invindex}->finish; |
|
176 |
|
|
177 |
$log->info("writing value usage file"); |
$log->debug("field usage: ", dump( $self->{field_count} )); |
|
|
|
|
# add fields from last run |
|
|
map { $self->{value_usage}->{$_}++ } @{ $self->{fields} }; |
|
|
|
|
|
my @fields = keys %{ $self->{value_usage} }; |
|
|
store \@fields, $self->{fields_path} || |
|
|
$log->warn("can't write $self->{fields_path}: $!"); |
|
|
|
|
|
} |
|
|
|
|
|
=head2 convert |
|
|
|
|
|
my $utf8_string = $self->convert('string in codepage'); |
|
|
|
|
|
=cut |
|
|
|
|
|
sub convert { |
|
|
my $self = shift; |
|
178 |
|
|
|
my $text = shift || return; |
|
|
from_to($text, $self->{encoding}, 'UTF-8'); |
|
|
return $text; |
|
179 |
} |
} |
180 |
|
|
181 |
=head1 AUTHOR |
=head1 AUTHOR |
184 |
|
|
185 |
=head1 COPYRIGHT & LICENSE |
=head1 COPYRIGHT & LICENSE |
186 |
|
|
187 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
Copyright 2005-2007 Dobrica Pavlinusic, All Rights Reserved. |
188 |
|
|
189 |
This program is free software; you can redistribute it and/or modify it |
This program is free software; you can redistribute it and/or modify it |
190 |
under the same terms as Perl itself. |
under the same terms as Perl itself. |