implement numeric padding
[BackupPC.git] / lib / BackupPC / Search / KinoSearch.pm
index 67eff7e..e6f7c6a 100644 (file)
@@ -43,22 +43,46 @@ sub indexer {
        );
        my $blob_type = KinoSearch::Plan::BlobType->new( stored => 1 );
        my $string_type = KinoSearch::Plan::StringType->new; # non-tokenized
-       my $num_type = KinoSearch::Plan::Int64Type->new;
+       my $num_type = KinoSearch::Plan::Int64Type->new( sortable => 1 );
+       my $sort_type = KinoSearch::Plan::StringType->new( sortable => 1 ); # non-tokenized
+
+=for numeric-no-padding
+               fid
+               shareid
+               type
+               backupnum
+=cut
+       $self->{numeric_padding} = [ qw/
+               backup_date
+               date
+               size
+       / ];
 
        # numeric
        $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
                backup_date
-               backupnum
-               date
                fid
                shareid
-               size
                type
        / );
 
        # non-tokenized strings
        $schema->spec_field( name => $_, type => $string_type ) foreach ( qw/
-               _uri filepath hname sname
+               _uri
+               hname
+       /);
+
+       # sortable
+       $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
+               sname
+               filepath
+       /);
+
+       # sortable numeric
+       $schema->spec_field( name => $_, type => $sort_type ) foreach (qw/
+               backupnum
+               date
+               size
        /);
 
        # tokenized magic columns for infix search
@@ -108,10 +132,14 @@ sub add_doc {
        $path =~ s/(.)/$1 /g; # XXX our tokenize
        $row->{_file_path_split} = $path;
 
-       warn "XXX ",dump($row) if $ENV{DEBUG};
-
        $self->{stats}->{add_doc}++;
 
+       foreach my $col ( @{ $self->{numeric_padding} } ) {
+               $row->{$col} = sprintf "%011d", $row->{$col};
+       }
+
+       warn "XXX ",dump($row) if $ENV{DEBUG};
+
        $self->indexer->add_doc( $row );
 
 }
@@ -122,6 +150,22 @@ sub commit {
        warn "# commit index ", dump($self->{stats});
 }
 
+sub _field_lower_upper_term {
+       my ( $field, $l, $u ) = @_;
+       my $range;
+       if ( $l ) {
+               $range->{lower_term} = $l;
+               $range->{include_lower} = 1;
+       }
+       if ( $u ) {
+               $range->{upper_term} = $u;
+               $range->{include_upper} = 1;
+       }
+       $range->{field} = $field if $range;
+       warn "# $field $l - $u ",dump($range);
+       return $range;
+}
+
 sub search {
        my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
 
@@ -134,9 +178,40 @@ sub search {
 
        my $sort_spec = KinoSearch::Search::SortSpec->new( rules => $rules );
 
-#      $q =~ s/(.)/$1 /g;
+       my $split = $q;
+       $split =~ s/(.)/$1 /g; # _file_path_split
+       my $split_query = KinoSearch::Search::TermQuery->new( field => '_file_path_split', term => $split );
+#warn "XXX ",dump($split_query);
+
+
+       my $query_parser = KinoSearch::Search::QueryParser->new(
+               schema => $self->searcher->get_schema,
+               fields => ['_file_path_split'],
+       );
+       my $query = $query_parser->parse( '"' . $split . '"' );
+
+       my @and_query;
+
+       if ( $shareid ) {
+               push @and_query, KinoSearch::Search::TermQuery->new( field => 'shareid', term => $shareid );
+       }
+
+       if ( my $range = _field_lower_upper_term( 'backup_date', $backup_from, $backup_to ) ) {
+               push @and_query, KinoSearch::Search::RangeQuery->new( %$range );
+       }
+       if ( my $range = _field_lower_upper_term( 'date', $files_from, $files_to ) ) {
+               push @and_query, KinoSearch::Search::RangeQuery->new( %$range );
+       }
+
+       if ( @and_query ) {
+               push @and_query, $query;
+               $query = KinoSearch::Search::ANDQuery->new( children => [ @and_query ] );
+       }
+
        my $hits = $self->searcher->hits(
-               query => $q,
+               query => m/:/ ? $q : $query,
+               offset => $offset,
+               num_wanted => $on_page,
                sort_spec => $sort_spec,
        );
 
@@ -147,7 +222,7 @@ sub search {
 
        my $results;
        while ( my $hit = $hits->next ) {
-warn "XXX ",dump($hit);
+               warn "## hit = ",dump($hit) if $ENV{DEBUG};
                push @$results, $hit;
        }