X-Git-Url: http://git.rot13.org/?p=BackupPC.git;a=blobdiff_plain;f=lib%2FBackupPC%2FSearch%2FKinoSearch.pm;h=67eff7e8e3022f1af1e8320973c9993b175e516b;hp=043f8abc294b1f83d8669088c51b067891f4e9ed;hb=2630cbae3c9db4d44a7fd754f8a1deaefde0dc5e;hpb=43ad134934a253364299247a1197510cfbba09cd diff --git a/lib/BackupPC/Search/KinoSearch.pm b/lib/BackupPC/Search/KinoSearch.pm index 043f8ab..67eff7e 100644 --- a/lib/BackupPC/Search/KinoSearch.pm +++ b/lib/BackupPC/Search/KinoSearch.pm @@ -15,6 +15,20 @@ sub new { my $class = shift @_; my %Conf = @_; + my $index_path = $Conf{KinoPath} || die "no KinoPath"; + + my $self = bless { + index => $index_path, + first_time_indexing => ! -d $index_path, + }, $class; + warn "# ",dump($self); + return $self; +} + +sub indexer { + my $self = shift; + return $self->{_indexer} if defined $self->{_indexer}; + my $schema = KinoSearch::Plan::Schema->new; @@ -31,6 +45,7 @@ sub new { my $string_type = KinoSearch::Plan::StringType->new; # non-tokenized my $num_type = KinoSearch::Plan::Int64Type->new; + # numeric $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/ backup_date backupnum @@ -41,39 +56,47 @@ sub new { type / ); + # non-tokenized strings $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/ - _uri _file_path_split filepath hname sname + _uri filepath hname sname /); -# $schema->spec_field( name => '_doc', type => $blob_type ); + # tokenized magic columns for infix search + $schema->spec_field( name => '_file_path_split', type => $ft_type ); - my $index_path = $Conf{KinoPath} || die "no KinoPath"; +# $schema->spec_field( name => '_doc', type => $blob_type ); my $indexer = KinoSearch::Index::Indexer->new( schema => $schema, - index => $index_path, + index => $self->{index}, create => 1, ); - warn "# using $index_path"; + warn "# created indexer"; - $indexer->commit; # make sure that index exists + return $self->{_indexer} = $indexer; - my $self = bless { - indexer => $indexer, - searcher => KinoSearch::Search::IndexSearcher->new( - index => $index_path, - ), +}; - }, $class; - return $self; +our $searcher; +sub searcher { + my $self = shift; + return $self->{_searcher} if $self->{_searcher}; + $self->{_searcher} = + KinoSearch::Search::IndexSearcher->new( index => $self->{index} ) } sub exists { my ($self,$row) = @_; + return 0 if $self->{first_time_indexing}; + my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath}; - my $hits = $self->{searcher}->hits( query => "_uri:$uri" ); + my $hits = $self->searcher->hits( query => "_uri:$uri" ); + + + $self->{stat}->{exists}->{ $hits->total_hits }++; + return $hits->total_hits; } @@ -87,14 +110,16 @@ sub add_doc { warn "XXX ",dump($row) if $ENV{DEBUG}; - $self->{indexer}->add_doc( $row ); + $self->{stats}->{add_doc}++; + + $self->indexer->add_doc( $row ); } sub commit { my $self = shift; - $self->{indexer}->commit; - warn "# commit index"; + $self->indexer->commit; + warn "# commit index ", dump($self->{stats}); } sub search { @@ -110,7 +135,7 @@ sub search { my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules ); # $q =~ s/(.)/$1 /g; - my $hits = $self->{searcher}->hits( + my $hits = $self->searcher->hits( query => $q, sort_spec => $sort_spec, );