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";
20 my $self = bless { index => $index_path }, $class;
21 warn "# ",dump($self);
27 return $self->{_indexer} if defined $self->{_indexer};
29 my $schema = KinoSearch::Plan::Schema->new;
32 my $case_folder = KinoSearch::Analysis::CaseFolder->new;
33 my $tokenizer = KinoSearch::Analysis::Tokenizer->new;
34 my $polyanalyzer = KinoSearch::Analysis::PolyAnalyzer->new(
35 analyzers => [ $case_folder, $tokenizer ],
38 my $ft_type = KinoSearch::Plan::FullTextType->new(
39 analyzer => $polyanalyzer,
41 my $blob_type = KinoSearch::Plan::BlobType->new( stored => 1 );
42 my $string_type = KinoSearch::Plan::StringType->new; # non-tokenized
43 my $num_type = KinoSearch::Plan::Int64Type->new;
45 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
55 $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
56 _uri _file_path_split filepath hname sname
59 # $schema->spec_field( name => '_doc', type => $blob_type );
61 my $indexer = KinoSearch::Index::Indexer->new(
63 index => $self->{index},
67 $indexer->commit; # make sure that index exists
69 warn "# created indexer";
71 return $self->{_indexer} = $indexer;
78 return $self->{_searcher} if $self->{_searcher};
80 KinoSearch::Search::IndexSearcher->new( index => $self->{index} )
86 my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
87 my $hits = $self->searcher->hits( query => "_uri:$uri" );
88 return $hits->total_hits;
94 $row->{_uri} = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
95 my $path = $row->{filepath};
96 $path =~ s/(.)/$1 /g; # XXX our tokenize
97 $row->{_file_path_split} = $path;
99 warn "XXX ",dump($row) if $ENV{DEBUG};
101 $self->indexer->add_doc( $row );
107 $self->indexer->commit;
108 warn "# commit index";
112 my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
114 warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
116 my $sort_field = (split(/_/,$sort,2))[0];
118 my $rules = [ KinoSearch::Search::SortRule->new( type => 'score' ) ];
119 $rules->[0] = KinoSearch::Search::SortRule->new( field => $sort_field, reverse => $sort =~ m/_a$/ ? 0 : 1 ) if $sort_field;
121 my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
124 my $hits = $self->searcher->hits(
126 sort_spec => $sort_spec,
130 warn "# ", $hits->total_hits, " hits for $q\n";
132 return (0,[]) if $hits->total_hits == 0;
135 while ( my $hit = $hits->next ) {
136 warn "XXX ",dump($hit);
137 push @$results, $hit;
140 return ( $hits->total_hits, $results );