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

  ViewVC Help
Powered by ViewVC 1.1.26