043f8abc294b1f83d8669088c51b067891f4e9ed
[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 sub search {
101         my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
102
103         warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
104
105         my $sort_field = (split(/_/,$sort,2))[0];
106
107         my $rules = [ KinoSearch::Search::SortRule->new( type => 'score' ) ];
108         $rules->[0] = KinoSearch::Search::SortRule->new( field => $sort_field, reverse => $sort =~ m/_a$/ ? 0 : 1 ) if $sort_field;
109
110         my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
111
112 #       $q =~ s/(.)/$1 /g;
113         my $hits = $self->{searcher}->hits(
114                 query => $q,
115                 sort_spec => $sort_spec,
116         );
117
118
119         warn "# ", $hits->total_hits, " hits for $q\n";
120
121         return (0,[]) if $hits->total_hits == 0;
122
123         my $results;
124         while ( my $hit = $hits->next ) {
125 warn "XXX ",dump($hit);
126                 push @$results, $hit;
127         }
128
129         return ( $hits->total_hits, $results );
130 }
131
132 1;