/[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 194 - (show annotations)
Fri May 23 22:32:47 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3346 byte(s)
support finish to make API complete
1 package Grep::Search;
2 use strict;
3 use warnings;
4
5 use base qw(
6 Class::Accessor
7 Grep::Search::Estraier
8 );
9 =for alternative
10 Grep::Search::Estraier
11 Grep::Search::KinoSearch
12 =cut
13 Grep::Search->mk_accessors( qw( create hits ) );
14
15 sub log { Jifty->web->log }
16 use Data::Dump qw/dump/;
17 use Jifty::Util;
18
19 my $debug = 0;
20
21 =head1 NAME
22
23 Grep::Search - full text search L<Grep::Model::Item>
24
25 =head1 METHODS
26
27 =head2 new
28
29 my $search = Grep::Search->new();
30
31 my $search = Grep::Search->new( create => 1 );
32
33 =cut
34
35 =head2 add
36
37 $search->add( $record, $owner_id );
38
39 =cut
40
41 sub add {
42 my $self = shift;
43
44 my $i = shift or die "no record to add";
45 my $uid = shift;
46
47 die "record not Jifty::Record but ", ref $i unless ($i->isa('Jifty::Record'));
48
49 my $pk = { $i->primary_keys };
50
51 my $doc;
52
53 my @columns = map { $_->name } $i->columns;
54
55 foreach my $c ( @columns ) {
56
57 my $v = $i->$c;
58
59 if ( ref($v) ne '' ) {
60
61 foreach my $f_c ( qw/id name title/ ) {
62 if ( $i->$c->can( $f_c ) ) {
63 my $f_v = $i->$c->$f_c || $i->$c->{values}->{ $f_c };
64 my $col = $c . '_' . $f_c;
65 if ( $f_v ) {
66 warn " # $col = $f_v\n" if ($debug);
67 $doc->{ $col } = $f_v;
68 } else {
69 warn " . $col is NULL\n" if ($debug);
70 }
71 }
72 }
73
74 if ($v->isa('Jifty::DateTime')) {
75 warn " d $c = $v\n" if ($debug);
76 $doc->{$c} = $v;
77 } else {
78 warn " s $c = $v [",ref($v),"]\n" if ($debug);
79 }
80 next;
81 }
82
83 next if (! defined($v) || $v eq '');
84
85 eval { $v =~ s/<[^>]+>/ /gs; };
86 if ($@) {
87 Jifty->log->error("can't strip html from $c in item ", $i->id);
88 next;
89 }
90
91 if ( defined( $pk->{$c} ) ) {
92 $doc->{ $c } = $v;
93 warn " * $c = $v\n" if ($debug);
94 } else {
95 $doc->{ $c } = $v;
96 warn " + $c = ", $self->snippet( 50, $v ), "\n" if ($debug);
97 }
98 }
99
100 # add _owner_id to speed up filtering of search results
101 $uid ||= Jifty->web->current_user->id;
102 $doc->{ '_owner_id' } = $uid;
103
104 $self->SUPER::add( $doc );
105
106
107 $self->log->debug("added ", $i->id, " for user $uid to index");
108
109 return 1;
110 }
111
112 =head2 collection
113
114 Return C<Grep::Model::ItemCollection> which is result of C<search query>
115
116 my $ItemCollection = $search->collection( 'search query' );
117
118 =head2 hits
119
120 Return number of results from last C<collection> call
121
122 my $num_results = $search->hits;
123
124 =cut
125
126 sub collection {
127 my $self = shift;
128
129 my $q = shift or die "no q?";
130
131 my $next_hit = $self->search( $q );
132
133 $self->log->debug("found ", $self->hits, " results");
134
135 my $collection = Grep::Model::ItemCollection->new();
136
137 my @results;
138
139 my $i = 0;
140 while ( my $hit = $next_hit->() ) {
141
142 my $score = $hit->{score};
143 my $title = $hit->{title};
144 my $id = $hit->{id};
145
146 $self->log->debug("result $i [$id] $title $score");
147
148 my $item = Grep::Model::Item->new();
149 my ($ok,$msg) = $item->load_by_cols( id => $id );
150
151 if ( $ok ) {
152 $collection->add_record( $item );
153 } else {
154 warn "can't load item $id\n";
155 }
156
157 }
158
159 $self->log->debug("finished search");
160
161 return $collection;
162 }
163
164 =head2 snippet
165
166 my $short = $self->snippet( 50, $text );
167
168 =cut
169
170 sub snippet {
171 my $self = shift;
172
173 my $len = shift or die "no len?";
174 my $m = join(" ", @_);
175
176 $m =~ s/\s+/ /gs;
177
178 if (length($m) > $len) {
179 return substr($m,0,$len) . '...';
180 } else {
181 return $m;
182 }
183 }
184
185 =head2 finish
186
187 $search->finish;
188
189 =cut
190
191 sub finish {
192 my $self = shift;
193 eval { $self->SUPER::finish( @_ ) };
194 }
195
196 1;

  ViewVC Help
Powered by ViewVC 1.1.26