/[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 58 - (show annotations)
Wed Feb 21 19:10:20 2007 UTC (17 years, 2 months ago) by dpavlin
File size: 4050 byte(s)
Another mish-mash of different changes rolled into one commit: added feed owners,
better support for bootstraping without data (actaully, fixes to be able to do so...)
1 package Grep::Search;
2
3 use strict;
4 use warnings;
5
6 use Data::Dump qw/dump/;
7 use Lucene;
8 use Jifty::Util;
9
10 my $index_path = Jifty::Util->app_root . '/var/lucene';
11
12 my ( $analyzer, $store, $writer );
13
14 my $debug = 1;
15 my $create;
16
17 sub create {
18
19 if (defined( $create )) {
20 Jifty->log->debug("using previous create $create");
21 return $create;
22 }
23
24 if (! -e "$index_path/segments") {
25 $create = 1;
26 Jifty->log->debug("create index $index_path");
27 } else {
28 $create = 0;
29 Jifty->log->debug("open index: $index_path");
30 }
31 return $create;
32 }
33
34 sub analyzer {
35 my $self = shift;
36 $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();
37 return $analyzer;
38 }
39
40 sub store {
41 my $self = shift;
42
43 $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );
44 return $store;
45 }
46
47 sub writer {
48 my $self = shift;
49 $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );
50 return $writer;
51 }
52
53 =head2 add
54
55 Grep::Search->add( $record );
56
57 =cut
58
59 sub add {
60 my $self = shift;
61
62 my $i = shift or die "no record to add";
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 $self->writer->addDocument($doc);
114
115 Jifty->log->debug("added ", $i->id, " to index");
116 }
117
118 =head2
119
120 my $ItemCollection = Grep::Search->collection( 'search query' );
121
122 =cut
123
124 sub collection {
125 my $self = shift;
126
127 my $q = shift or die "no q?";
128
129 return if ( $self->create );
130
131 my $searcher = new Lucene::Search::IndexSearcher($self->store);
132 my $parser = new Lucene::QueryParser("content", $self->analyzer);
133 my $query = $parser->parse( $q );
134
135 Jifty->log->debug("searching for '$q'");
136
137 my $hits = $searcher->search($query);
138 my $num_hits = $hits->length();
139
140 Jifty->log->debug("found $num_hits results");
141
142 my $collection = Grep::Model::ItemCollection->new();
143
144 my @results;
145
146 for ( my $i = 0; $i < $num_hits; $i++ ) {
147
148 my $doc = $hits->doc( $i );
149
150 my $score = $hits->score($i);
151 my $title = $doc->get("title");
152 my $id = $doc->get("id");
153
154 warn "## $i $score $title\n";
155
156 my $item = Grep::Model::Item->new();
157 my ($ok,$msg) = $item->load_by_cols( id => $id );
158
159 if ( $ok ) {
160 $collection->add_record( $item );
161 } else {
162 warn "can't load item $id\n";
163 }
164
165 }
166
167 undef $hits;
168 undef $query;
169 undef $parser;
170 undef $searcher;
171
172 return $collection;
173 }
174
175 =head2 finish
176
177 Grep::Search->finish
178
179 =cut
180
181 sub finish {
182 my $self = shift;
183 if ($writer) {
184 warn "closing index\n";
185 $writer->close;
186 }
187 undef $writer;
188 undef $create;
189
190 return;
191 }
192
193 =for TODO
194
195 sub _signal {
196 my $s = shift;
197 warn "catched SIG $s\n";
198 finish();
199 exit(0);
200 }
201
202 $SIG{'__DIE__'} = \&_signal;
203 $SIG{'INT'} = \&_signal;
204 $SIG{'QUIT'} = \&_signal;
205
206 =cut
207
208 =head2 snippet
209
210 my $short = $self->snippet( 50, $text );
211
212
213 =cut
214
215 sub snippet {
216 my $self = shift;
217
218 my $len = shift or die "no len?";
219 my $m = join(" ", @_);
220
221 $m =~ s/\s+/ /gs;
222
223 if (length($m) > $len) {
224 return substr($m,0,$len) . '...';
225 } else {
226 return $m;
227 }
228 }
229
230 1;

  ViewVC Help
Powered by ViewVC 1.1.26