/[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

Contents of /lib/Grep/Search.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (show annotations)
Sat Jun 9 10:27:03 2007 UTC (16 years, 10 months ago) by dpavlin
File size: 5588 byte(s)
return success, don't include uid if 0 or undef in query
1 package Grep::Search;
2
3 use strict;
4 use warnings;
5 use base qw( Class::Accessor );
6 Grep::Search->mk_accessors( qw( invindexer create index_path hits ) );
7
8 use Data::Dump qw/dump/;
9 use KinoSearch::InvIndexer;
10 use KinoSearch::Searcher;
11 use Jifty::Util;
12
13 my $debug = 0;
14
15 =head1 NAME
16
17 Grep::Search - full text search using L<KinoSearch>
18
19 =head1 METHODS
20
21 =head2 new
22
23 my $search = Grep::Search->new();
24
25 my $search = Grep::Search->new( create => 1 );
26
27 =head2 invindexer
28
29 Accessor to call any method defined on L<KinoSearch::InvIndexer>
30
31 $search->invindexer->delete_by_term( 'id', 42 );
32
33 =cut
34
35 sub log { Jifty->web->log }
36
37 sub new {
38 my $class = shift;
39 my $self = $class->SUPER::new(@_);
40
41 my $index_path = Jifty::Util->app_root . '/var/invindex';
42
43 $self->index_path( $index_path );
44
45 if ( ! -e "$index_path" || $self->create ) {
46 $self->log->debug("Creating new index $index_path");
47 $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->clobber( $index_path ) ) );
48 } else {
49 $self->log->debug("Opening index: $index_path");
50 $self->invindexer( KinoSearch::InvIndexer->new( invindex => Grep::Search::Schema->open( $index_path ) ) );
51 }
52
53 return $self;
54 }
55
56 =head2 add
57
58 $search->add( $record, $owner_id );
59
60 =cut
61
62 sub add {
63 my $self = shift;
64
65 my $i = shift or die "no record to add";
66 my $uid = shift;
67
68 die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));
69
70 my $pk = { $i->primary_keys };
71
72 my $doc;
73
74 my @columns = map { $_->name } $i->columns;
75
76 foreach my $c ( @columns ) {
77
78 my $v = $i->$c;
79
80 if ( ref($v) ne '' ) {
81
82 foreach my $f_c ( qw/id name title/ ) {
83 if ( $i->$c->can( $f_c ) ) {
84 my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
85 my $col = $c . '_' . $f_c;
86 if ( $f_v ) {
87 warn " # $col = $f_v\n" if ($debug);
88 $doc->{ $col } = $f_v;
89 } else {
90 warn " . $col is NULL\n" if ($debug);
91 }
92 }
93 }
94
95 if ($v->isa('Jifty::DateTime')) {
96 warn " d $c = $v\n" if ($debug);
97 $doc->{$c} = $v;
98 } else {
99 warn " s $c = $v [",ref($v),"]\n" if ($debug);
100 }
101 next;
102 }
103
104 next if (! defined($v) || $v eq '');
105
106 $v =~ s/<[^>]+>/ /gs;
107
108 if ( defined( $pk->{$c} ) ) {
109 $doc->{ $c } = $v;
110 warn " * $c = $v\n" if ($debug);
111 } else {
112 $doc->{ $c } = $v;
113 warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
114 }
115 }
116
117 # add _owner_id to speed up filtering of search results
118 $uid ||= Jifty->web->current_user->id;
119 $doc->{ '_owner_id' } = $uid;
120
121 $self->invindexer->add_doc( $doc );
122
123 $self->log->debug("added ", $i->id, " for user $uid to index");
124
125 return 1;
126 }
127
128 =head2 collection
129
130 Return C<Grep::Model::ItemCollection> which is result of C<search query>
131
132 my $ItemCollection = $search->collection( 'search query' );
133
134 =head2 hits
135
136 Return number of results from last C<collection> call
137
138 my $num_results = $search->hits;
139
140 =cut
141
142 sub collection {
143 my $self = shift;
144
145 my $q = shift or die "no q?";
146
147 my $searcher = KinoSearch::Searcher->new(
148 invindex => Grep::Search::Schema->open( $self->index_path ), );
149 $self->log->debug("$searcher created");
150
151 my $full_q = "($q)";
152
153 my $uid = Jifty->web->current_user->id;
154 $full_q .= ' AND _owner_id:' . $uid if ($uid);
155
156 $self->log->debug("searching for '$q' using $full_q");
157 my $hits = $searcher->search(
158 query => $full_q,
159 # offset => $offset,
160 # num_wanted => $hits_per_page,
161 );
162
163 $self->hits( $hits->total_hits );
164
165 $self->log->debug("found ", $self->hits, " results");
166
167 my $collection = Grep::Model::ItemCollection->new();
168
169 my @results;
170
171 my $i = 0;
172 while ( my $hit = $hits->fetch_hit_hashref ) {
173
174 my $score = $hit->{score};
175 my $title = $hit->{title};
176 my $id = $hit->{id};
177
178 $self->log->debug("result $i [$id] $title $score");
179
180 my $item = Grep::Model::Item->new();
181 my ($ok,$msg) = $item->load_by_cols( id => $id );
182
183 if ( $ok ) {
184 $collection->add_record( $item );
185 } else {
186 warn "can't load item $id\n";
187 }
188
189 }
190
191 $self->log->debug("finished search");
192
193 return $collection;
194 }
195
196 =head2 finish
197
198 $search->finish
199
200 =cut
201
202 sub finish {
203 my $self = shift;
204 if ($self->invindexer) {
205 $self->log->debug("closing index");
206 $self->invindexer->finish;
207 }
208
209 $self->log->debug("finish");
210
211 undef $self;
212
213 return 1;
214 }
215
216 =head2 snippet
217
218 my $short = $self->snippet( 50, $text );
219
220 =cut
221
222 sub snippet {
223 my $self = shift;
224
225 my $len = shift or die "no len?";
226 my $m = join(" ", @_);
227
228 $m =~ s/\s+/ /gs;
229
230 if (length($m) > $len) {
231 return substr($m,0,$len) . '...';
232 } else {
233 return $m;
234 }
235 }
236
237 package Grep::Search::KeywordField;
238 use base qw( KinoSearch::Schema::FieldSpec );
239 sub analyzed { 0 }
240
241 package Grep::Search::Schema;
242
243 =head1 NAME
244
245 Grep::Search::Schema - schema definition for full-text search
246
247 =cut
248
249 use base 'KinoSearch::Schema';
250 use KinoSearch::Analysis::PolyAnalyzer;
251
252 our %fields = (
253 id => 'Grep::Search::KeywordField',
254
255 in_feed_id => 'Grep::Search::KeywordField',
256 in_feed_url => 'Grep::Search::KeywordField',
257 in_feed_title => 'KinoSearch::Schema::FieldSpec',
258 in_feed_owner => 'Grep::Search::KeywordField',
259 in_feed_created_on => 'Grep::Search::KeywordField',
260
261 title => 'KinoSearch::Schema::FieldSpec',
262 link => 'Grep::Search::KeywordField',
263 content => 'KinoSearch::Schema::FieldSpec',
264 summary => 'KinoSearch::Schema::FieldSpec',
265 category => 'KinoSearch::Schema::FieldSpec',
266 author => 'KinoSearch::Schema::FieldSpec',
267 issued => 'Grep::Search::KeywordField',
268 modified => 'Grep::Search::KeywordField',
269
270 _owner_id => 'Grep::Search::KeywordField',
271 );
272
273 sub analyzer {
274 return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' );
275 }
276
277 1;

  ViewVC Help
Powered by ViewVC 1.1.26