/[Grep]/lib/Grep/Search/KinoSearch.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/KinoSearch.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 49 - (show annotations)
Wed Feb 21 13:01:34 2007 UTC (17 years ago) by dpavlin
Original Path: lib/Grep/Search.pm
File size: 3381 byte(s)
re-wrote Lucene support with fresh eyes
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 = 0;
15
16 sub create {
17
18 my $create = 0;
19 if (! -e "$index_path/segments") {
20 $create = 1;
21 Jifty->log->debug("create index $index_path") unless ($store);
22 } else {
23 Jifty->log->debug("open index: $index_path") unless ($store);
24 }
25 return $create;
26 }
27
28 sub analyzer {
29 my $self = shift;
30 $analyzer ||= new Lucene::Analysis::Standard::StandardAnalyzer();
31 return $analyzer;
32 }
33
34 sub store {
35 my $self = shift;
36
37 $store ||= Lucene::Store::FSDirectory->getDirectory( $index_path, $self->create );
38 return $store;
39 }
40
41 sub writer {
42 my $self = shift;
43 $writer ||= new Lucene::Index::IndexWriter( $self->store, $self->analyzer, $self->create );
44 return $writer;
45 }
46
47 =head2 add
48
49 Grep::Search->add( $record );
50
51 =cut
52
53 sub add {
54 my $self = shift;
55
56 my $i = shift or die "no record to add";
57
58 die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));
59
60 my $pk = { $i->primary_keys };
61
62 my $doc = new Lucene::Document;
63
64 my @columns = map { $_->name } $i->columns;
65
66 foreach my $c ( @columns ) {
67
68 my $v = $i->$c;
69
70 if ( ref($v) ne '' ) {
71 if ($i->$c->can('id')) {
72 $v = $i->$c->id;
73 warn " # $c = $v [id]\n" if ($debug);
74 $doc->add(Lucene::Document::Field->Keyword( $c, $v ));
75 } elsif ($v->isa('Jifty::DateTime')) {
76 warn " d $c = $v\n" if ($debug);
77 $doc->add(Lucene::Document::Field->Keyword( $c, "$v" ));
78 } else {
79 warn " s $c = $v [",ref($v),"]\n" if ($debug);
80 }
81 next;
82 }
83
84 next if (! defined($v) || $v eq '');
85
86 $v =~ s/<[^>]+>/ /gs;
87
88 if ( defined( $pk->{$c} ) ) {
89 $doc->add(Lucene::Document::Field->Keyword( $c, $v ));
90 warn " * $c = $v\n" if ($debug);
91 } else {
92 $doc->add(Lucene::Document::Field->Text( $c, $v ));
93 warn " + $c = $v\n" if ($debug);
94 }
95 }
96
97 $self->writer->addDocument($doc);
98
99 Jifty->log->debug("added ", $i->id, " to index");
100 }
101
102 =head2
103
104 my $ItemCollection = Grep::Search->collection( 'search query' );
105
106 =cut
107
108 sub collection {
109 my $self = shift;
110
111 my $q = shift or die "no q?";
112
113 my $searcher = new Lucene::Search::IndexSearcher($self->store);
114 my $parser = new Lucene::QueryParser("content", $self->analyzer);
115 my $query = $parser->parse( $q );
116
117 Jifty->log->debug("searching for '$q'");
118
119 my $hits = $searcher->search($query);
120 my $num_hits = $hits->length();
121
122 Jifty->log->debug("found $num_hits results");
123
124 my $collection = Grep::Model::ItemCollection->new();
125
126 my @results;
127
128 for ( my $i = 0; $i < $num_hits; $i++ ) {
129
130 my $doc = $hits->doc( $i );
131
132 my $score = $hits->score($i);
133 my $title = $doc->get("title");
134 my $id = $doc->get("id");
135
136 warn "## $i $score $title\n";
137
138 my $item = Grep::Model::Item->new();
139 my ($ok,$msg) = $item->load_by_cols( id => $id );
140
141 if ( $ok ) {
142 $collection->add_record( $item );
143 } else {
144 warn "can't load item $id\n";
145 }
146
147 }
148
149 undef $hits;
150 undef $query;
151 undef $parser;
152 undef $searcher;
153
154 return $collection;
155 }
156
157 =head2 finish
158
159 Grep::Search->finish
160
161 =cut
162
163 sub finish {
164 my $self = shift;
165 if ($writer) {
166 warn "closing index\n";
167 $writer->close;
168 }
169 undef $writer;
170 }
171
172 sub _signal {
173 my $s = shift;
174 warn "catched SIG $s\n";
175 finish();
176 exit(0);
177 }
178
179 $SIG{'__DIE__'} = \&_signal;
180 $SIG{'INT'} = \&_signal;
181 $SIG{'QUIT'} = \&_signal;
182
183 1;

  ViewVC Help
Powered by ViewVC 1.1.26