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 $schema = KinoSearch::Plan::Schema->new;
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 ],
27 my $ft_type = KinoSearch::Plan::FullTextType->new(
28 analyzer => $polyanalyzer,
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;
34 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
44 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
45 _uri _file_path_split filepath hname sname
48 # $schema->spec_field( name => '_doc', type => $blob_type );
50 my $index_path = $Conf{KinoPath} || die "no KinoPath";
52 my $indexer = KinoSearch::Index::Indexer->new(
58 warn "# using $index_path";
60 $indexer->commit; # make sure that index exists
64 searcher => KinoSearch::Search::IndexSearcher->new(
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;
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;
88 warn "XXX ",dump($row) if $ENV{DEBUG};
90 $self->{indexer}->add_doc( $row );
96 $self->{indexer}->commit;
97 warn "# commit index";
101 my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
103 warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
105 my $sort_field = (split(/_/,$sort,2))[0];
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;
110 my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
113 my $hits = $self->{searcher}->hits(
115 sort_spec => $sort_spec,
119 warn "# ", $hits->total_hits, " hits for $q\n";
121 return (0,[]) if $hits->total_hits == 0;
124 while ( my $hit = $hits->next ) {
125 warn "XXX ",dump($hit);
126 push @$results, $hit;
129 return ( $hits->total_hits, $results );