move KinoSearch index path to KinoPath config var
[BackupPC.git] / lib / BackupPC / Search / KinoSearch.pm
1 package BackupPC::Search::KinoSearch;
2 use warnings;
3 use strict;
4
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);
11
12 # my $tokenizer = KinoSearch::Analysis::Tokenizer->new( pattern => '\\w' );
13
14 sub new {
15         my $class = shift @_;
16         my %Conf = @_;
17
18         my $schema = KinoSearch::Plan::Schema->new;
19
20
21         my $case_folder  = KinoSearch::Analysis::CaseFolder->new;
22         my $tokenizer    = KinoSearch::Analysis::Tokenizer->new;
23         my $polyanalyzer = KinoSearch::Analysis::PolyAnalyzer->new(
24                 analyzers => [ $case_folder, $tokenizer ], 
25         );
26
27         my $ft_type = KinoSearch::Plan::FullTextType->new(
28             analyzer => $polyanalyzer,
29         );
30         my $blob_type = KinoSearch::Plan::BlobType->new( stored => 1 );
31         my $string_type = KinoSearch::Plan::StringType->new; # non-tokenized
32         my $num_type = KinoSearch::Plan::Int64Type->new;
33
34         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
35                 backup_date
36                 backupnum
37                 date
38                 fid
39                 shareid
40                 size
41                 type
42         / );
43
44         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
45                 _uri _file_path_split filepath hname sname
46         /);
47
48 #       $schema->spec_field( name => '_doc', type => $blob_type );
49
50         my $index_path = $Conf{KinoPath} || die "no KinoPath";
51
52         my $indexer = KinoSearch::Index::Indexer->new(
53                 schema => $schema,
54                 index  => $index_path,
55                 create => 1,
56         );
57
58         warn "# using $index_path";
59
60         $indexer->commit; # make sure that index exists
61
62         my $self = bless {
63                 indexer => $indexer,
64                 searcher => KinoSearch::Search::IndexSearcher->new(
65                         index => $index_path,
66                 ),
67
68         }, $class;
69         return $self;
70 }
71
72 sub exists {
73         my ($self,$row) = @_;
74
75         my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
76         my $hits = $self->{searcher}->hits( query => "_uri:$uri" );
77         return $hits->total_hits;
78 }
79
80 sub add_doc {
81         my ($self,$row) = @_;
82
83         $row->{_uri} = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
84         my $path = $row->{filepath};
85         $path =~ s/(.)/$1 /g; # XXX our tokenize
86         $row->{_file_path_split} = $path;
87
88         warn "XXX ",dump($row) if $ENV{DEBUG};
89
90         $self->{indexer}->add_doc( $row );
91
92 }
93
94 sub commit {
95         my $self = shift;
96         $self->{indexer}->commit;
97         warn "# commit index";
98 }
99
100 1;