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