/[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 190 - (show annotations)
Fri May 23 21:05:42 2008 UTC (15 years, 11 months ago) by dpavlin
File size: 3416 byte(s)
make proper split between top-level Grep::Search package
and Grep::Search::KinoSearch
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 $full_q = "($q)";
132
133 my $uid = Jifty->web->current_user->id;
134 $full_q .= ' AND _owner_id:' . $uid if (defined $uid);
135
136 $self->log->debug("searching for '$q' using $full_q");
137
138 my $next_hit = $self->search( $full_q );
139
140 $self->log->debug("found ", $self->hits, " results");
141
142 my $collection = Grep::Model::ItemCollection->new();
143
144 my @results;
145
146 my $i = 0;
147 while ( my $hit = $next_hit->() ) {
148
149 my $score = $hit->{score};
150 my $title = $hit->{title};
151 my $id = $hit->{id};
152
153 $self->log->debug("result $i [$id] $title $score");
154
155 my $item = Grep::Model::Item->new();
156 my ($ok,$msg) = $item->load_by_cols( id => $id );
157
158 if ( $ok ) {
159 $collection->add_record( $item );
160 } else {
161 warn "can't load item $id\n";
162 }
163
164 }
165
166 $self->log->debug("finished search");
167
168 return $collection;
169 }
170
171 =head2 snippet
172
173 my $short = $self->snippet( 50, $text );
174
175 =cut
176
177 sub snippet {
178 my $self = shift;
179
180 my $len = shift or die "no len?";
181 my $m = join(" ", @_);
182
183 $m =~ s/\s+/ /gs;
184
185 if (length($m) > $len) {
186 return substr($m,0,$len) . '...';
187 } else {
188 return $m;
189 }
190 }
191
192
193 1;

  ViewVC Help
Powered by ViewVC 1.1.26