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

Annotation of /lib/Grep/Search/KinoSearch.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26