/[webpac2]/Webpacus/lib/Webpacus/Controller/Search.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /Webpacus/lib/Webpacus/Controller/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 116 by dpavlin, Wed Nov 23 21:52:20 2005 UTC revision 153 by dpavlin, Sat Nov 26 01:54:31 2005 UTC
# Line 4  use strict; Line 4  use strict;
4  use warnings;  use warnings;
5  use base 'Catalyst::Controller';  use base 'Catalyst::Controller';
6    
7    use Data::Dumper;
8    
9  use lib '/data/webpac2/lib';  use lib '/data/webpac2/lib';
10  use WebPAC::Search::Estraier;  use WebPAC::Search::Estraier;
11    
12    
13  =head1 NAME  =head1 NAME
14    
15  Webpacus::Controller::Search - Catalyst Controller  Webpacus::Controller::Search - Catalyst Controller
# Line 39  Returns results for URLs like C<search/s Line 42  Returns results for URLs like C<search/s
42  =cut  =cut
43    
44  sub suggest : Regex('^search/suggest/*([^/]*)') {  sub suggest : Regex('^search/suggest/*([^/]*)') {
45      my ( $self, $c ) = @_;          my ( $self, $c ) = @_;
46    
47            my $what = $c->request->snippets->[0];
48    
49      my $what = $c->request->snippets->[0];          my $log = $c->log;
50    
51      my $log = $c->log;          my $est_conf = $c->config->{hyperestraier};
52            $est_conf->{log} = $c->log;
53    
54   my $est = new WebPAC::Search::Estraier(          my $est = new WebPAC::Search::Estraier( %{ $est_conf } );
55          url => 'http://localhost:1978/node/webpac2',  
56          user => 'admin',          my $q = $c->req->params->{$what} ||
57          passwd => 'admin',                  $c->req->params->{all} || $c->res->output("no results");
         encoding => 'UTF-8',  
         log => $c->log,  
  );  
       
58    
     my $q = $c->req->params->{$what};  
     my @suggestions;  
59    
60          $log->info("search for '$q' in $what\n");          $log->info("search for '$q' in $what\n");
     my @hits = $est->search( phrase => $q, max => 10, get_attr => [ $what ] );  
61    
62      my $used;          my $max = $est_conf->{'hits_for_suggest'};
63            if (! $max) {
64                    $log->info("hits_for_suggest isn't defined, defaulting to 10");
65                    $c->config->{'hits_for_suggest'} = 10;
66                    $max = 10;
67            }
68    
69            my @hits = $est->search( phrase => $q, max => $max, get_attr => [ $what ] );
70    
71            my $used;
72            my @suggestions;
73    
74            foreach my $res (@hits) {
75                    my $v = $res->{ $what } || next;
76                    next if ($used->{ $v }++);
77                    push @suggestions, $v;
78            }
79    
80            $log->debug( ($#suggestions + 1) . " unique hits returned");
81    
82            $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) );
83    }
84    
85    
86    # specify all Hyper Estraier operators which should stop this module
87    # from splitting search query and joining it with default operator
88    my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/;
89    
90    =item results
91    
92    Mothod which uses C<Model::WebPAC> and returns results for search query
93    
94    =cut
95    
96    sub results : Local {
97            my ( $self, $c ) = @_;
98    
99            my $webpac = $c->comp('Model::WebPAC');
100            my $params = $c->req->params;
101            my $log = $c->log;
102    
103            $log->debug("results got params: " . Dumper( $params ) );
104    
105            my @attr;
106            my @words;
107            # default operator to join fields/words
108            my $operator = 'AND';
109    
110            foreach my $f (keys %{ $params }) {
111    
112                    next if ($f =~ m/^_/o);
113    
114                    my $v = $params->{$f} || next;
115    
116                    if (my $op = $params->{ '_' . $f}) {
117                            if ($v =~ $hest_op_regex) {
118                                    # don't split words if there is Hyper Estraier
119                                    # operator in them
120                                    push @words, $v;
121                            } else {
122                                    push @words, join(" $op ", split(/\s+/, $v) );
123                            }
124                    } else {
125                            push @words, $v;
126                    }
127    
128                    next if ($f eq 'all');  # don't add_attr for magic field all
129    
130                    if ($v !~ /\s/) {
131                            push @attr, "$f ISTRINC $v";
132                    } else {
133                            map {
134                                    push @attr, "$f ISTRINC $_";
135                            } grep { ! $hest_op_regex } split(/\s+/, $v);
136                    }
137            }
138    
139            my $q = join(" $operator ", @words);
140    
141            my $template = $params->{'_template'};
142            $log->debug("using template $template");
143    
144            $c->stash->{html_results} = sub {
145                    my $res = $webpac->search(
146                            phrase => $q,
147                            template => $template,
148                            add_attr => \@attr
149                    );
150            #       $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res ));
151                    return $res;
152            };
153    
154      foreach my $res (@hits) {          $c->stash->{phrase} = $q;
155          my $v = $res->{ $what } || next;          $c->stash->{attr} = \@attr;
         next if ($used->{ $v }++);  
         push @suggestions, $v;  
     }  
156    
157      $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) );          $c->stash->{template} = 'results.tt';
158  }  }
159    
160  =back  =back
# Line 76  sub suggest : Regex('^search/suggest/*([ Line 162  sub suggest : Regex('^search/suggest/*([
162    
163  =head1 AUTHOR  =head1 AUTHOR
164    
165  Dobrica Pavlinusic,,,  Dobrica Pavlinusic C<< <dpavlin@rot13.org> >>
166    
167  =head1 LICENSE  =head1 LICENSE
168    

Legend:
Removed from v.116  
changed lines
  Added in v.153

  ViewVC Help
Powered by ViewVC 1.1.26