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