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

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

  ViewVC Help
Powered by ViewVC 1.1.26