added numeric padding to appropriate query values
[BackupPC.git] / lib / BackupPC / Search / Estraier.pm
1 package BackupPC::Search::Estraier;
2 use warnings;
3 use strict;
4
5 use Search::Estraier 0.04;
6 use Data::Dump qw(dump);
7
8 my $debug = $ENV{DEBUG} || 0;
9
10 sub new {
11         my $class = shift @_;
12         my %Conf = @_;
13
14         my $index_node_url = $Conf{HyperEstraierIndex} || die "no HyperEstraierIndex in config ",dump(%Conf);
15
16         warn "# using $index_node_url";
17
18         my $self = bless {
19                 node => Search::Estraier::Node->new(
20                         url => $index_node_url,
21                         user => 'admin',
22                         passwd => 'admin',
23                         croak_on_error => 1,
24                 ),
25         }, $class;
26         return $self;
27 }
28
29 sub node { $_[0]->{node} };
30
31 sub exists {
32         my ( $self, $row ) = @_;
33
34         my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
35         my $id = $self->node->uri_to_id($uri);
36         return $id && $id != -1;
37 }
38
39 sub add_doc {
40         my ( $self, $row ) = @_;
41
42         # create a document object 
43         my $doc = Search::Estraier::Document->new;
44
45         my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
46         # add attributes to the document object 
47         $doc->add_attr('@uri', $uri);
48
49         foreach my $c (keys %$row) {
50                 print STDERR "attr $c = $row->{$c}\n" if ($debug > 2);
51                 $doc->add_attr($c, $row->{$c}) if defined($row->{$c});
52         }
53
54         #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
55
56         # add the body text to the document object 
57         my $path = $row->{'filepath'};
58         $doc->add_text($path);
59         $path =~ s/(.)/$1 /g;
60         $doc->add_hidden_text($path);
61
62         print STDERR $doc->dump_draft,"\n" if ($debug > 1);
63
64         # register the document object to the database
65         $self->node->put_doc($doc);
66
67 }
68
69 sub commit {
70         my $self = shift;
71         warn "# commit not needed";
72 }
73
74 my $sort_mapping = {
75         share_d => 'sname STRD',
76         share_a => 'sname STRA',
77         path_d => 'filepath STRD',
78         path_a => 'filepath STRA',
79         num_d => 'backupnum NUMD',
80         num_a => 'backupnum NUMA',
81         size_d => 'size NUMD',
82         size_a => 'size NUMA',
83         date_d => 'date NUMD',
84         date_a => 'date NUMA',
85 };
86
87 sub search {
88         my ( $self, $offset, $on_page, $sort, $q, $shareid, $backup_from, $backup_to, $files_from, $files_to ) = @_;
89
90         warn "# search $offset/$on_page [$q] shareid: $shareid backup: $backup_from - $backup_to files: $files_from - $files_to";
91
92         # create a search condition object
93         my $cond = Search::Estraier::Condition->new();
94
95         if (length($q) > 0) {
96                 # exact match
97                 $cond->add_attr("filepath ISTRINC $q");
98
99                 $q =~ s/(.)/$1 /g;
100                 # set the search phrase to the search condition object
101                 $cond->set_phrase($q);
102         }
103
104         $cond->add_attr("backup_date NUMGE $backup_from") if ($backup_from);
105         $cond->add_attr("backup_date NUMLE $backup_to") if ($backup_to);
106
107         $cond->add_attr("date NUMGE $files_from") if ($files_from);
108         $cond->add_attr("date NUMLE $files_to") if ($files_to);
109
110         $cond->add_attr("shareid NUMEQ $shareid") if ($shareid);
111
112         $cond->set_max( $offset + $on_page );
113         $cond->set_options( 'SURE' );
114         $cond->set_order( $sort_mapping->{$sort} );
115
116         # get the result of search
117         my @res;
118         my ($result, $hits);
119
120         $result = $self->node->search($cond, 0);
121         if ($result) {
122                 $hits = $result->hits;
123         } else {
124                 return (0,[]);
125         }
126
127         # for each document in result
128         for my $i ($offset .. ($offset + $on_page - 1)) {
129                 last if ($i >= $result->doc_num);
130
131                 my $doc = $result->get_doc($i);
132
133                 my $row;
134                 foreach my $c (qw/fid hname sname backupnum filepath date type size/) {
135                         $row->{$c} = $doc->attr($c);
136                 }
137                 push @res, $row;
138         }
139
140         warn "# $hits hits";
141
142         return ($hits, \@res);
143
144 }
145
146 1;