fix tokenization for _file_path_split
[BackupPC.git] / lib / BackupPC / Search / KinoSearch.pm
index 3efcf8c..67eff7e 100644 (file)
@@ -17,7 +17,10 @@ sub new {
 
        my $index_path = $Conf{KinoPath} || die "no KinoPath";
 
-       my $self = bless { index => $index_path }, $class;
+       my $self = bless {
+               index => $index_path,
+               first_time_indexing => ! -d $index_path,
+       }, $class;
        warn "# ",dump($self);
        return $self;
 }
@@ -42,6 +45,7 @@ sub indexer {
        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
@@ -52,10 +56,14 @@ sub indexer {
                type
        / );
 
+       # non-tokenized strings
        $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
-               _uri _file_path_split filepath hname sname
+               _uri filepath hname sname
        /);
 
+       # tokenized magic columns for infix search
+       $schema->spec_field( name => '_file_path_split', type => $ft_type );
+
 #      $schema->spec_field( name => '_doc', type => $blob_type );
 
        my $indexer = KinoSearch::Index::Indexer->new(
@@ -64,8 +72,6 @@ sub indexer {
                create => 1,
        );
 
-       $indexer->commit; # make sure that index exists
-
        warn "# created indexer";
 
        return $self->{_indexer} = $indexer;
@@ -83,8 +89,14 @@ sub searcher {
 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" );
+
+
+       $self->{stat}->{exists}->{ $hits->total_hits }++;
+
        return $hits->total_hits;
 }
 
@@ -98,6 +110,8 @@ sub add_doc {
 
        warn "XXX ",dump($row) if $ENV{DEBUG};
 
+       $self->{stats}->{add_doc}++;
+
        $self->indexer->add_doc( $row );
 
 }
@@ -105,7 +119,7 @@ sub add_doc {
 sub commit {
        my $self = shift;
        $self->indexer->commit;
-       warn "# commit index";
+       warn "# commit index ", dump($self->{stats});
 }
 
 sub search {