/[Grep]/lib/Grep/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

Annotation of /lib/Grep/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 198 - (hide annotations)
Sun Oct 12 11:48:45 2008 UTC (15 years, 7 months ago) by dpavlin
File size: 3388 byte(s)
eval around add document to catch dies from search plugins
1 dpavlin 190 package Grep::Search;
2     use strict;
3     use warnings;
4    
5     use base qw(
6     Class::Accessor
7 dpavlin 194 Grep::Search::Estraier
8 dpavlin 190 );
9     =for alternative
10     Grep::Search::Estraier
11     Grep::Search::KinoSearch
12     =cut
13     Grep::Search->mk_accessors( qw( create hits ) );
14    
15     sub log { Jifty->web->log }
16     use Data::Dump qw/dump/;
17     use Jifty::Util;
18    
19     my $debug = 0;
20    
21     =head1 NAME
22    
23     Grep::Search - full text search L<Grep::Model::Item>
24    
25     =head1 METHODS
26    
27     =head2 new
28    
29     my $search = Grep::Search->new();
30    
31     my $search = Grep::Search->new( create => 1 );
32    
33     =cut
34    
35     =head2 add
36    
37     $search->add( $record, $owner_id );
38    
39     =cut
40    
41     sub add {
42     my $self = shift;
43    
44     my $i = shift or die "no record to add";
45     my $uid = shift;
46    
47     die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));
48    
49     my $pk = { $i->primary_keys };
50    
51     my $doc;
52    
53     my @columns = map { $_->name } $i->columns;
54    
55     foreach my $c ( @columns ) {
56    
57     my $v = $i->$c;
58    
59     if ( ref($v) ne '' ) {
60    
61     foreach my $f_c ( qw/id name title/ ) {
62     if ( $i->$c->can( $f_c ) ) {
63     my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
64     my $col = $c . '_' . $f_c;
65     if ( $f_v ) {
66     warn " # $col = $f_v\n" if ($debug);
67     $doc->{ $col } = $f_v;
68     } else {
69     warn " . $col is NULL\n" if ($debug);
70     }
71     }
72     }
73    
74     if ($v->isa('Jifty::DateTime')) {
75     warn " d $c = $v\n" if ($debug);
76     $doc->{$c} = $v;
77     } else {
78     warn " s $c = $v [",ref($v),"]\n" if ($debug);
79     }
80     next;
81     }
82    
83     next if (! defined($v) || $v eq '');
84    
85     eval { $v =~ s/<[^>]+>/ /gs; };
86     if ($@) {
87     Jifty->log->error("can't strip html from $c in item ", $i->id);
88     next;
89     }
90    
91     if ( defined( $pk->{$c} ) ) {
92     $doc->{ $c } = $v;
93     warn " * $c = $v\n" if ($debug);
94     } else {
95     $doc->{ $c } = $v;
96     warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
97     }
98     }
99    
100     # add _owner_id to speed up filtering of search results
101     $uid ||= Jifty->web->current_user->id;
102     $doc->{ '_owner_id' } = $uid;
103    
104 dpavlin 198 eval { $self->SUPER::add( $doc ); };
105     $self->log->error( $@ ) if $@;
106 dpavlin 190
107    
108     $self->log->debug("added ", $i->id, " for user $uid to index");
109    
110     return 1;
111     }
112    
113     =head2 collection
114    
115     Return C<Grep::Model::ItemCollection> which is result of C<search query>
116    
117     my $ItemCollection = $search->collection( 'search query' );
118    
119     =head2 hits
120    
121     Return number of results from last C<collection> call
122    
123     my $num_results = $search->hits;
124    
125     =cut
126    
127     sub collection {
128     my $self = shift;
129    
130     my $q = shift or die "no q?";
131    
132 dpavlin 192 my $next_hit = $self->search( $q );
133 dpavlin 190
134     $self->log->debug("found ", $self->hits, " results");
135    
136     my $collection = Grep::Model::ItemCollection->new();
137    
138     my @results;
139    
140     my $i = 0;
141     while ( my $hit = $next_hit->() ) {
142    
143     my $score = $hit->{score};
144     my $title = $hit->{title};
145     my $id = $hit->{id};
146    
147     $self->log->debug("result $i [$id] $title $score");
148    
149     my $item = Grep::Model::Item->new();
150     my ($ok,$msg) = $item->load_by_cols( id => $id );
151    
152     if ( $ok ) {
153     $collection->add_record( $item );
154     } else {
155     warn "can't load item $id\n";
156     }
157    
158     }
159    
160     $self->log->debug("finished search");
161    
162     return $collection;
163     }
164    
165     =head2 snippet
166    
167     my $short = $self->snippet( 50, $text );
168    
169     =cut
170    
171     sub snippet {
172     my $self = shift;
173    
174     my $len = shift or die "no len?";
175     my $m = join(" ", @_);
176    
177     $m =~ s/\s+/ /gs;
178    
179     if (length($m) > $len) {
180     return substr($m,0,$len) . '...';
181     } else {
182     return $m;
183     }
184     }
185    
186 dpavlin 194 =head2 finish
187 dpavlin 190
188 dpavlin 194 $search->finish;
189    
190     =cut
191    
192     sub finish {
193     my $self = shift;
194     eval { $self->SUPER::finish( @_ ) };
195     }
196    
197 dpavlin 190 1;

  ViewVC Help
Powered by ViewVC 1.1.26