d12577d3d6cf9b2e22489f0a5543f2c85cd9b3af
[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 $index_path = $Conf{KinoPath} || die "no KinoPath";
19
20         my $self = bless {
21                 index => $index_path,
22                 first_time_indexing => ! -d $index_path,
23         }, $class;
24         warn "# ",dump($self);
25         return $self;
26 }
27
28 sub indexer {
29         my $self = shift;
30         return $self->{_indexer} if defined $self->{_indexer};
31
32         my $schema = KinoSearch::Plan::Schema->new;
33
34
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 ], 
39         );
40
41         my $ft_type = KinoSearch::Plan::FullTextType->new(
42             analyzer => $polyanalyzer,
43         );
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
48
49         # numeric
50         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
51                 backup_date
52                 backupnum
53                 fid
54                 shareid
55                 type
56         / );
57
58         # non-tokenized strings
59         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
60                 _uri
61                 hname
62         /);
63
64         # sortable
65         $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
66                 sname
67                 filepath
68         /);
69
70         # sortable numeric
71         $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
72                 date
73                 size
74         /);
75
76         # tokenized magic columns for infix search
77         $schema->spec_field( name => '_file_path_split', type => $ft_type );
78
79 #       $schema->spec_field( name => '_doc', type => $blob_type );
80
81         my $indexer = KinoSearch::Index::Indexer->new(
82                 schema => $schema,
83                 index  => $self->{index},
84                 create => 1,
85         );
86
87         warn "# created indexer";
88
89         return $self->{_indexer} = $indexer;
90
91 };
92
93 our $searcher;
94 sub searcher {
95         my $self = shift;
96         return $self->{_searcher} if $self->{_searcher};
97         $self->{_searcher} =
98         KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
99 }
100
101 sub exists {
102         my ($self,$row) = @_;
103
104         return 0 if $self->{first_time_indexing};
105
106         my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
107         my $hits = $self->searcher->hits( query => "_uri:$uri" );
108
109
110         $self->{stat}->{exists}->{ $hits->total_hits }++;
111
112         return $hits->total_hits;
113 }
114
115 sub add_doc {
116         my ($self,$row) = @_;
117
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;
122
123         warn "XXX ",dump($row) if $ENV{DEBUG};
124
125         $self->{stats}->{add_doc}++;
126
127         $self->indexer->add_doc( $row );
128
129 }
130
131 sub commit {
132         my $self = shift;
133         $self->indexer->commit;
134         warn "# commit index ", dump($self->{stats});
135 }
136
137 sub search {
138         my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
139
140         warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
141
142         my $sort_field = (split(/_/,$sort,2))[0];
143
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;
146
147         my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
148
149         my $split = $q;
150         $split =~ s/(.)/$1 /g; # _file_path_split
151         $split = qq{"$split"}; # exact ordering
152         my $hits = $self->searcher->hits(
153                 query => $split,
154                 offset => $offset,
155                 num_wanted => $on_page,
156                 sort_spec => $sort_spec,
157         );
158
159
160         warn "# ", $hits->total_hits, " hits for $q\n";
161
162         return (0,[]) if $hits->total_hits == 0;
163
164         my $results;
165         while ( my $hit = $hits->next ) {
166 warn "XXX ",dump($hit);
167                 push @$results, $hit;
168         }
169
170         return ( $hits->total_hits, $results );
171 }
172
173 1;