numeric_padding values are used in range search, and muse be sortable
[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 # numeric_padding values are used in range search, and muse be sortable
15
16 sub new {
17         my $class = shift @_;
18         my %Conf = @_;
19
20         my $index_path = $Conf{KinoPath} || die "no KinoPath";
21
22         my $self = bless {
23                 index => $index_path,
24                 first_time_indexing => ! -d $index_path,
25                 numeric_padding => [ qw(
26                         backup_date
27                         date
28                 ) ],
29
30         }, $class;
31         warn "# ",dump($self);
32         return $self;
33 }
34
35 sub indexer {
36         my $self = shift;
37         return $self->{_indexer} if defined $self->{_indexer};
38
39         my $schema = KinoSearch::Plan::Schema->new;
40
41
42         my $case_folder  = KinoSearch::Analysis::CaseFolder->new;
43         my $tokenizer    = KinoSearch::Analysis::Tokenizer->new;
44         my $polyanalyzer = KinoSearch::Analysis::PolyAnalyzer->new(
45                 analyzers => [ $case_folder, $tokenizer ], 
46         );
47
48         my $ft_type = KinoSearch::Plan::FullTextType->new(
49             analyzer => $polyanalyzer,
50         );
51         my $blob_type = KinoSearch::Plan::BlobType->new( stored => 1 );
52         my $string_type = KinoSearch::Plan::StringType->new; # non-tokenized
53         my $num_type = KinoSearch::Plan::Int64Type->new( sortable => 1 );
54         my $sort_type = KinoSearch::Plan::StringType->new( sortable => 1 ); # non-tokenized
55
56         # numeric
57         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
58                 fid
59                 shareid
60                 type
61         / );
62
63         # non-tokenized strings
64         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
65                 _uri
66                 hname
67         /);
68
69         # sortable
70         $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
71                 sname
72                 filepath
73         /);
74
75         # sortable numeric
76         $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
77                 backupnum
78                 backup_date
79                 date
80                 size
81         /);
82
83         # tokenized magic columns for infix search
84         $schema->spec_field( name => '_file_path_split', type => $ft_type );
85
86 #       $schema->spec_field( name => '_doc', type => $blob_type );
87
88         my $indexer = KinoSearch::Index::Indexer->new(
89                 schema => $schema,
90                 index  => $self->{index},
91                 create => 1,
92         );
93
94         warn "# created indexer";
95
96         return $self->{_indexer} = $indexer;
97
98 };
99
100 our $searcher;
101 sub searcher {
102         my $self = shift;
103         return $self->{_searcher} if $self->{_searcher};
104         $self->{_searcher} =
105         KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
106 }
107
108 sub exists {
109         my ($self,$row) = @_;
110
111         return 0 if $self->{first_time_indexing};
112
113         my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
114         my $hits = $self->searcher->hits( query => "_uri:$uri" );
115
116
117         $self->{stat}->{exists}->{ $hits->total_hits }++;
118
119         return $hits->total_hits;
120 }
121
122 sub _numeric_padding { sprintf "%010d", $_[0] } # pad up to 32bit number (timestamp)
123
124 sub add_doc {
125         my ($self,$row) = @_;
126
127         $row->{_uri} = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
128         my $path = $row->{filepath};
129         $path =~ s/(.)/$1 /g; # XXX our tokenize
130         $row->{_file_path_split} = $path;
131
132         $self->{stats}->{add_doc}++;
133
134         foreach my $col ( @{ $self->{numeric_padding} } ) {
135                 $row->{$col} = _numeric_padding $row->{$col};
136         }
137
138         warn "XXX ",dump($row) if $ENV{DEBUG};
139
140         $self->indexer->add_doc( $row );
141
142 }
143
144 sub commit {
145         my $self = shift;
146         $self->indexer->commit;
147         warn "# commit index ", dump($self->{stats});
148 }
149
150 sub _field_lower_upper_term {
151         my ( $self, $field, $l, $u ) = @_;
152         my $numeric_padding = grep { /^$field$/ } @{ $self->{numeric_padding} };
153         my $range;
154         if ( $l ) {
155                 $range->{lower_term} = $numeric_padding ? _numeric_padding $l : $l;
156                 $range->{include_lower} = 1;
157         }
158         if ( $u ) {
159                 $range->{upper_term} = $numeric_padding ? _numeric_padding $u : $u;
160                 $range->{include_upper} = 1;
161         }
162         if ( $range ) {
163                 $range->{field} = $field;
164
165                 warn "# $field $l - $u numeric_padding:$numeric_padding ",dump($range);
166         }
167         return $range;
168 }
169
170 sub search {
171         my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
172
173         warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
174
175         my $sort_field = (split(/_/,$sort,2))[0];
176
177         my $rules = [ KinoSearch::Search::SortRule->new( type => 'score' ) ];
178         $rules->[0] = KinoSearch::Search::SortRule->new( field => $sort_field, reverse => $sort =~ m/_a$/ ? 0 : 1 ) if $sort_field;
179
180         my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
181
182         my $split = $q;
183         $split =~ s/(.)/$1 /g; # _file_path_split
184         my $split_query = KinoSearch::Search::TermQuery->new( field => '_file_path_split', term => $split );
185 #warn "XXX ",dump($split_query);
186
187
188         my $query_parser = KinoSearch::Search::QueryParser->new(
189                 schema => $self->searcher->get_schema,
190                 fields => ['_file_path_split'],
191         );
192         my $query = $query_parser->parse( '"' . $split . '"' );
193
194         my @and_query;
195
196         if ( $shareid ) {
197                 push @and_query, KinoSearch::Search::TermQuery->new( field => 'shareid', term => $shareid );
198         }
199
200         if ( my $range = $self->_field_lower_upper_term( 'backup_date', $backup_from, $backup_to ) ) {
201                 push @and_query, KinoSearch::Search::RangeQuery->new( %$range );
202         }
203         if ( my $range = $self->_field_lower_upper_term( 'date', $files_from, $files_to ) ) {
204                 push @and_query, KinoSearch::Search::RangeQuery->new( %$range );
205         }
206
207         if ( @and_query ) {
208                 push @and_query, $query;
209                 $query = KinoSearch::Search::ANDQuery->new( children => [ @and_query ] );
210         }
211
212         my $hits = $self->searcher->hits(
213                 query => m/:/ ? $q : $query,
214                 offset => $offset,
215                 num_wanted => $on_page,
216                 sort_spec => $sort_spec,
217         );
218
219
220         warn "# ", $hits->total_hits, " hits for $q\n";
221
222         return (0,[]) if $hits->total_hits == 0;
223
224         my $results;
225         while ( my $hit = $hits->next ) {
226                 warn "## hit = ",dump($hit) if $ENV{DEBUG};
227                 push @$results, $hit;
228         }
229
230         return ( $hits->total_hits, $results );
231 }
232
233 1;