less output
[BackupPC.git] / lib / BackupPC / Search / Estraier.pm
index ff2b287..afbaaad 100644 (file)
@@ -2,12 +2,16 @@ package BackupPC::Search::Estraier;
 use warnings;
 use strict;
 
-use Search::Estraier;
+use Search::Estraier 0.04;
+use Data::Dump qw(dump);
 
 my $debug = $ENV{DEBUG} || 0;
 
 sub new {
-       my ( $class, $index_node_url ) = @_;
+       my $class = shift @_;
+       my %Conf = @_;
+
+       my $index_node_url = $Conf{HyperEstraierIndex} || die "no HyperEstraierIndex in config ",dump(%Conf);
 
        warn "# using $index_node_url";
 
@@ -67,4 +71,76 @@ sub commit {
        warn "# commit not needed";
 }
 
+my $sort_mapping = {
+       share_d => 'sname STRD',
+       share_a => 'sname STRA',
+       path_d => 'filepath STRD',
+       path_a => 'filepath STRA',
+       num_d => 'backupnum NUMD',
+       num_a => 'backupnum NUMA',
+       size_d => 'size NUMD',
+       size_a => 'size NUMA',
+       date_d => 'date NUMD',
+       date_a => 'date NUMA',
+};
+
+sub search {
+       my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
+
+       warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
+
+       # create a search condition object
+       my $cond = Search::Estraier::Condition->new();
+
+       if (length($q) > 0) {
+               # exact match
+               $cond->add_attr("filepath ISTRINC $q");
+
+               $q =~ s/(.)/$1 /g;
+               # set the search phrase to the search condition object
+               $cond->set_phrase($q);
+       }
+
+       $cond->add_attr("backup_date NUMGE $backup_from") if ($backup_from);
+       $cond->add_attr("backup_date NUMLE $backup_to") if ($backup_to);
+
+       $cond->add_attr("date NUMGE $files_from") if ($files_from);
+       $cond->add_attr("date NUMLE $files_to") if ($files_to);
+
+       $cond->add_attr("shareid NUMEQ $shareid") if ($shareid);
+
+       $cond->set_max( $offset + $on_page );
+       $cond->set_options( 'SURE' );
+       $cond->set_order( $sort_mapping->{$sort} );
+
+       # get the result of search
+       my @res;
+       my ($result, $hits);
+
+       $result = $self->node->search($cond, 0);
+       if ($result) {
+               $hits = $result->hits;
+       } else {
+               return (0,[]);
+       }
+
+       # for each document in result
+       for my $i ($offset .. ($offset + $on_page - 1)) {
+               last if ($i >= $result->doc_num);
+
+               my $doc = $result->get_doc($i);
+
+               my $row;
+               foreach my $c (qw/fid hname sname backupnum filepath date type size/) {
+                       $row->{$c} = $doc->attr($c);
+               }
+               push @res, $row;
+       }
+
+       warn "# $hits hits";
+
+       return ($hits, \@res);
+
+}
+
 1;