/[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 110 - (show annotations)
Wed Mar 14 20:02:19 2007 UTC (17 years, 1 month ago) by dpavlin
File size: 4845 byte(s)
another bunch of various tweaks, but Lucene still doesn't lock index right
1 package Grep::Search;
2
3 use strict;
4 use warnings;
5 use base qw( Class::Accessor Jifty::Object );
6 Grep::Search->mk_accessors( qw( analyzer store writer create index_path ) );
7
8 use Data::Dump qw/dump/;
9 use Lucene;
10 use Jifty::Util;
11
12 my $debug = 0;
13
14 =head1 NAME
15
16 Grep::Search - full text search
17
18 =head1 METHODS
19
20 =head2 new
21
22 my $search = Grep::Search->new();
23
24 =cut
25
26 sub new {
27 my $class = shift;
28 my $self = $class->SUPER::new(@_);
29
30 my $index_path = Jifty::Util->app_root . '/var/lucene';
31
32 $self->index_path( $index_path );
33
34 if (! -e "$index_path/segments") {
35 $self->create( 1 );
36 $self->log->debug("Creating new index $index_path");
37 } else {
38 $self->create( 0 );
39 $self->log->debug("Opening index: $index_path");
40 }
41
42 $self->analyzer( new Lucene::Analysis::Standard::StandardAnalyzer() );
43 $self->log->debug($self->analyzer . " created");
44
45 $self->store( Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create ) );
46 $self->log->debug($self->store, " created");
47
48 return $self;
49 }
50
51
52 =head2 add
53
54 $search->add( $record, $owner_id );
55
56 =cut
57
58 sub add {
59 my $self = shift;
60
61 my $i = shift or die "no record to add";
62 my $uid = shift;
63
64 die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));
65
66 my $pk = { $i->primary_keys };
67
68 my $doc = new Lucene::Document;
69
70 my @columns = map { $_->name } $i->columns;
71
72 foreach my $c ( @columns ) {
73
74 my $v = $i->$c;
75
76 if ( ref($v) ne '' ) {
77
78 foreach my $f_c ( qw/id name title/ ) {
79 if ( $i->$c->can( $f_c ) ) {
80 my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
81 my $col = $c . '_' . $f_c;
82 if ( $f_v ) {
83 warn " # $col = $f_v\n" if ($debug);
84 $doc->add(Lucene::Document::Field->Text( $col, $f_v ));
85 } else {
86 warn " . $col is NULL\n" if ($debug);
87 }
88 }
89 }
90
91 if ($v->isa('Jifty::DateTime')) {
92 warn " d $c = $v\n" if ($debug);
93 $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));
94 } else {
95 warn " s $c = $v [",ref($v),"]\n" if ($debug);
96 }
97 next;
98 }
99
100 next if (! defined($v) || $v eq '');
101
102 $v =~ s/<[^>]+>/ /gs;
103
104 if ( defined( $pk->{$c} ) ) {
105 $doc->add(Lucene::Document::Field->Keyword( $c, $v ));
106 warn " * $c = $v\n" if ($debug);
107 } else {
108 $doc->add(Lucene::Document::Field->Text( $c, $v ));
109 warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
110 }
111 }
112
113 # add _owner_id to speed up filtering of search results
114 $uid ||= Jifty->web->current_user->id;
115 $doc->add(Lucene::Document::Field->Keyword( '_owner_id', $uid ));
116
117 if (! defined( $self->writer )) {
118 $self->writer( new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create ) );
119 $self->log->debug($self->writer, " created");
120 }
121
122 $self->writer->addDocument($doc);
123
124 $self->log->debug("added ", $i->id, " for user $uid to index");
125 }
126
127 =head2 collection
128
129 my $ItemCollection = $search->collection( 'search query' );
130
131 =cut
132
133 sub collection {
134 my $self = shift;
135
136 my $q = shift or die "no q?";
137
138 return if ( $self->create );
139
140 my $searcher = new Lucene::Search::IndexSearcher($self->store);
141 $self->log->debug("$searcher created");
142 my $parser = new Lucene::QueryParser("content", $self->analyzer);
143 $self->log->debug("$parser created");
144
145 my $full_q = "($q) AND _owner_id:" . Jifty->web->current_user->id;
146
147 my $query = $parser->parse( $full_q );
148
149 $self->log->debug("searching for '$q' using ", $query->toString);
150
151 my $hits = $searcher->search($query);
152 my $num_hits = $hits->length();
153
154 $self->log->debug("found $num_hits results");
155
156 my $collection = Grep::Model::ItemCollection->new();
157
158 my @results;
159
160 for ( my $i = 0; $i < $num_hits; $i++ ) {
161
162 my $doc = $hits->doc( $i );
163
164 my $score = $hits->score($i);
165 my $title = $doc->get("title");
166 my $id = $doc->get("id");
167
168 $self->log->debug("result $i $score $title");
169
170 my $item = Grep::Model::Item->new();
171 my ($ok,$msg) = $item->load_by_cols( id => $id );
172
173 if ( $ok ) {
174 $collection->add_record( $item );
175 } else {
176 warn "can't load item $id\n";
177 }
178
179 }
180
181 undef $hits;
182 undef $query;
183 undef $parser;
184 $searcher->close;
185 undef $searcher;
186
187 return $collection;
188 }
189
190 =head2 finish
191
192 $search->finish
193
194 =cut
195
196 sub finish {
197 my $self = shift;
198 if ($self->writer) {
199 $self->log->debug("closing index");
200 $self->writer->close;
201 }
202
203 $self->writer( undef );
204 $self->store( undef );
205 $self->create( undef );
206 $self->analyzer( undef );
207
208 $self->log->debug("finish");
209
210 return;
211 }
212
213 =for TODO
214
215 sub _signal {
216 my $s = shift;
217 warn "catched SIG $s\n";
218 finish();
219 exit(0);
220 }
221
222 $SIG{'__DIE__'} = \&_signal;
223 $SIG{'INT'} = \&_signal;
224 $SIG{'QUIT'} = \&_signal;
225
226 =cut
227
228 =head2 snippet
229
230 my $short = $self->snippet( 50, $text );
231
232 =cut
233
234 sub snippet {
235 my $self = shift;
236
237 my $len = shift or die "no len?";
238 my $m = join(" ", @_);
239
240 $m =~ s/\s+/ /gs;
241
242 if (length($m) > $len) {
243 return substr($m,0,$len) . '...';
244 } else {
245 return $m;
246 }
247 }
248
249 1;

  ViewVC Help
Powered by ViewVC 1.1.26