1 package BackupPC::Search::KinoSearch;
5 use KinoSearch::Index::Indexer;
6 use KinoSearch::Plan::Schema;
7 use KinoSearch::Analysis::PolyAnalyzer;
8 use KinoSearch::Plan::FullTextType;
9 use KinoSearch::Search::IndexSearcher;
10 use Data::Dump qw(dump);
12 # my $tokenizer = KinoSearch::Analysis::Tokenizer->new( pattern => '\\w' );
18 my $index_path = $Conf{KinoPath} || die "no KinoPath";
22 first_time_indexing => ! -d $index_path,
24 warn "# ",dump($self);
30 return $self->{_indexer} if defined $self->{_indexer};
32 my $schema = KinoSearch::Plan::Schema->new;
35 my $case_folder = KinoSearch::Analysis::CaseFolder->new;
36 my $tokenizer = KinoSearch::Analysis::Tokenizer->new;
37 my $polyanalyzer = KinoSearch::Analysis::PolyAnalyzer->new(
38 analyzers => [ $case_folder, $tokenizer ],
41 my $ft_type = KinoSearch::Plan::FullTextType->new(
42 analyzer => $polyanalyzer,
44 my $blob_type = KinoSearch::Plan::BlobType->new( stored => 1 );
45 my $string_type = KinoSearch::Plan::StringType->new; # non-tokenized
46 my $num_type = KinoSearch::Plan::Int64Type->new( sortable => 1 );
47 my $sort_type = KinoSearch::Plan::StringType->new( sortable => 1 ); # non-tokenized
50 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
58 # non-tokenized strings
59 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
65 $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
71 $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
76 # tokenized magic columns for infix search
77 $schema->spec_field( name => '_file_path_split', type => $ft_type );
79 # $schema->spec_field( name => '_doc', type => $blob_type );
81 my $indexer = KinoSearch::Index::Indexer->new(
83 index => $self->{index},
87 warn "# created indexer";
89 return $self->{_indexer} = $indexer;
96 return $self->{_searcher} if $self->{_searcher};
98 KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
102 my ($self,$row) = @_;
104 return 0 if $self->{first_time_indexing};
106 my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
107 my $hits = $self->searcher->hits( query => "_uri:$uri" );
110 $self->{stat}->{exists}->{ $hits->total_hits }++;
112 return $hits->total_hits;
116 my ($self,$row) = @_;
118 $row->{_uri} = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
119 my $path = $row->{filepath};
120 $path =~ s/(.)/$1 /g; # XXX our tokenize
121 $row->{_file_path_split} = $path;
123 warn "XXX ",dump($row) if $ENV{DEBUG};
125 $self->{stats}->{add_doc}++;
127 $self->indexer->add_doc( $row );
133 $self->indexer->commit;
134 warn "# commit index ", dump($self->{stats});
138 my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
140 warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
142 my $sort_field = (split(/_/,$sort,2))[0];
144 my $rules = [ KinoSearch::Search::SortRule->new( type => 'score' ) ];
145 $rules->[0] = KinoSearch::Search::SortRule->new( field => $sort_field, reverse => $sort =~ m/_a$/ ? 0 : 1 ) if $sort_field;
147 my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
149 $q =~ s/(.)/$1 /g; # _file_path_split
150 my $hits = $self->searcher->hits(
152 sort_spec => $sort_spec,
156 warn "# ", $hits->total_hits, " hits for $q\n";
158 return (0,[]) if $hits->total_hits == 0;
161 while ( my $hit = $hits->next ) {
162 warn "XXX ",dump($hit);
163 push @$results, $hit;
166 return ( $hits->total_hits, $results );