1 package BackupPC::Search::KinoSearch;
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);
12 # my $tokenizer = KinoSearch::Analysis::Tokenizer->new( pattern => '\\w' );
18 my $index_path = $Conf{KinoPath} || die "no KinoPath";
22 first_time_indexing => ! -d $index_path,
24 warn "# ",dump($self);
30 return $self->{_indexer} if defined $self->{_indexer};
32 my $schema = KinoSearch::Plan::Schema->new;
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 ],
41 my $ft_type = KinoSearch::Plan::FullTextType->new(
42 analyzer => $polyanalyzer,
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
50 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
57 # non-tokenized strings
58 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
64 $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
70 $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
76 # tokenized magic columns for infix search
77 $schema->spec_field( name => '_file_path_split', type => $ft_type );
79 # $schema->spec_field( name => '_doc', type => $blob_type );
81 my $indexer = KinoSearch::Index::Indexer->new(
83 index => $self->{index},
87 warn "# created indexer";
89 return $self->{_indexer} = $indexer;
96 return $self->{_searcher} if $self->{_searcher};
98 KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
102 my ($self,$row) = @_;
104 return 0 if $self->{first_time_indexing};
106 my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
107 my $hits = $self->searcher->hits( query => "_uri:$uri" );
110 $self->{stat}->{exists}->{ $hits->total_hits }++;
112 return $hits->total_hits;
116 my ($self,$row) = @_;
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;
123 warn "XXX ",dump($row) if $ENV{DEBUG};
125 $self->{stats}->{add_doc}++;
127 $self->indexer->add_doc( $row );
133 $self->indexer->commit;
134 warn "# commit index ", dump($self->{stats});
137 sub _field_lower_upper_term {
138 my ( $field, $l, $u ) = @_;
141 $range->{lower_term} = $l;
142 $range->{include_lower} = 1;
145 $range->{upper_term} = $u;
146 $range->{include_upper} = 1;
148 $range->{field} = $field if $range;
149 warn "# $field $l - $u ",dump($range);
154 my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
156 warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
158 my $sort_field = (split(/_/,$sort,2))[0];
160 my $rules = [ KinoSearch::Search::SortRule->new( type => 'score' ) ];
161 $rules->[0] = KinoSearch::Search::SortRule->new( field => $sort_field, reverse => $sort =~ m/_a$/ ? 0 : 1 ) if $sort_field;
163 my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
166 $split =~ s/(.)/$1 /g; # _file_path_split
167 my $split_query = KinoSearch::Search::TermQuery->new( field => '_file_path_split', term => $split );
168 #warn "XXX ",dump($split_query);
171 my $query_parser = KinoSearch::Search::QueryParser->new(
172 schema => $self->searcher->get_schema,
173 fields => ['_file_path_split'],
175 my $query = $query_parser->parse( '"' . $split . '"' );
180 push @and_query, KinoSearch::Search::TermQuery->new( field => 'shareid', term => $shareid );
183 if ( my $range = _field_lower_upper_term( 'backup_date', $backup_from, $backup_to ) ) {
184 push @and_query, KinoSearch::Search::RangeQuery->new( %$range );
186 if ( my $range = _field_lower_upper_term( 'date', $files_from, $files_to ) ) {
187 push @and_query, KinoSearch::Search::RangeQuery->new( %$range );
191 push @and_query, $query;
192 $query = KinoSearch::Search::ANDQuery->new( children => [ @and_query ] );
195 my $hits = $self->searcher->hits(
196 query => m/:/ ? $q : $query,
198 num_wanted => $on_page,
199 sort_spec => $sort_spec,
203 warn "# ", $hits->total_hits, " hits for $q\n";
205 return (0,[]) if $hits->total_hits == 0;
208 while ( my $hit = $hits->next ) {
209 warn "## hit = ",dump($hit) if $ENV{DEBUG};
210 push @$results, $hit;
213 return ( $hits->total_hits, $results );