split query to _file_path_split to make it somewhat usable
[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;
47
48         # numeric
49         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
50                 backup_date
51                 backupnum
52                 date
53                 fid
54                 shareid
55                 size
56                 type
57         / );
58
59         # non-tokenized strings
60         $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
61                 _uri filepath hname sname
62         /);
63
64         # tokenized magic columns for infix search
65         $schema->spec_field( name => '_file_path_split', type => $ft_type );
66
67 #       $schema->spec_field( name => '_doc', type => $blob_type );
68
69         my $indexer = KinoSearch::Index::Indexer->new(
70                 schema => $schema,
71                 index  => $self->{index},
72                 create => 1,
73         );
74
75         warn "# created indexer";
76
77         return $self->{_indexer} = $indexer;
78
79 };
80
81 our $searcher;
82 sub searcher {
83         my $self = shift;
84         return $self->{_searcher} if $self->{_searcher};
85         $self->{_searcher} =
86         KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
87 }
88
89 sub exists {
90         my ($self,$row) = @_;
91
92         return 0 if $self->{first_time_indexing};
93
94         my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
95         my $hits = $self->searcher->hits( query => "_uri:$uri" );
96
97
98         $self->{stat}->{exists}->{ $hits->total_hits }++;
99
100         return $hits->total_hits;
101 }
102
103 sub add_doc {
104         my ($self,$row) = @_;
105
106         $row->{_uri} = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
107         my $path = $row->{filepath};
108         $path =~ s/(.)/$1 /g; # XXX our tokenize
109         $row->{_file_path_split} = $path;
110
111         warn "XXX ",dump($row) if $ENV{DEBUG};
112
113         $self->{stats}->{add_doc}++;
114
115         $self->indexer->add_doc( $row );
116
117 }
118
119 sub commit {
120         my $self = shift;
121         $self->indexer->commit;
122         warn "# commit index ", dump($self->{stats});
123 }
124
125 sub search {
126         my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
127
128         warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
129
130         my $sort_field = (split(/_/,$sort,2))[0];
131
132         my $rules = [ KinoSearch::Search::SortRule->new( type => 'score' ) ];
133         $rules->[0] = KinoSearch::Search::SortRule->new( field => $sort_field, reverse => $sort =~ m/_a$/ ? 0 : 1 ) if $sort_field;
134
135         my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
136
137         $q =~ s/(.)/$1 /g; # _file_path_split
138         my $hits = $self->searcher->hits(
139                 query => $q,
140                 sort_spec => $sort_spec,
141         );
142
143
144         warn "# ", $hits->total_hits, " hits for $q\n";
145
146         return (0,[]) if $hits->total_hits == 0;
147
148         my $results;
149         while ( my $hit = $hits->next ) {
150 warn "XXX ",dump($hit);
151                 push @$results, $hit;
152         }
153
154         return ( $hits->total_hits, $results );
155 }
156
157 1;