/[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 190 - (hide annotations)
Fri May 23 21:05:42 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3938 byte(s)
make proper split between top-level Grep::Search package
and Grep::Search::KinoSearch
1 dpavlin 189 package Grep::Search::KinoSearch;
2 dpavlin 47
3     use strict;
4     use warnings;
5    
6     use Data::Dump qw/dump/;
7 dpavlin 127 use KinoSearch::InvIndexer;
8     use KinoSearch::Searcher;
9 dpavlin 47 use Jifty::Util;
10    
11 dpavlin 190 my $debug = 1;
12 dpavlin 47
13 dpavlin 109 =head1 NAME
14 dpavlin 47
15 dpavlin 189 Grep::Search::KinoSearch - full text search using L<KinoSearch>
16 dpavlin 47
17 dpavlin 109 =head1 METHODS
18 dpavlin 47
19 dpavlin 185 =head2 invindexer
20    
21     Accessor to call any method defined on L<KinoSearch::InvIndexer>
22    
23     $search->invindexer->delete_by_term( 'id', 42 );
24    
25     =cut
26    
27 dpavlin 189 our $indexes;
28 dpavlin 185
29     sub invindexer {
30     my $self = shift;
31     my $invindexer;
32 dpavlin 190 my $index_path = Jifty::Util->app_root . '/var/invindex';
33 dpavlin 185
34     if ( $invindexer = $indexes->{$index_path} ) {
35     $self->log->debug("Using cached index $index_path");
36 dpavlin 47 } else {
37 dpavlin 189 if ( $self->create || -e $index_path ) {
38 dpavlin 185 $self->log->debug("Creating new index $index_path");
39 dpavlin 189 $invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::KinoSearch::Schema->clobber( $index_path ) )
40     or die "can't create index $index_path: $!";
41     $self->create( 0 );
42 dpavlin 185 } else {
43     $self->log->debug("Opening index: $index_path");
44 dpavlin 189 $invindexer = KinoSearch::InvIndexer->new( invindex => Grep::Search::KinoSearch::Schema->open( $index_path ) )
45     or die "can't open index $index_path: $!";
46 dpavlin 185 }
47     $indexes->{$index_path} = $invindexer;
48 dpavlin 47 }
49    
50 dpavlin 189 return $invindexer;
51 dpavlin 49 }
52    
53 dpavlin 47 =head2 add
54    
55 dpavlin 190 $search->add( $doc_hash );
56 dpavlin 47
57     =cut
58    
59     sub add {
60     my $self = shift;
61 dpavlin 190 my $doc = shift;
62 dpavlin 127 $self->invindexer->add_doc( $doc );
63 dpavlin 151 return 1;
64 dpavlin 47 }
65    
66 dpavlin 190 =head2 search
67 dpavlin 47
68 dpavlin 190 my $fetch_hit_coderef = $self->search('search query');
69 dpavlin 141
70 dpavlin 47 =cut
71    
72 dpavlin 190 sub search {
73 dpavlin 47 my $self = shift;
74    
75     my $q = shift or die "no q?";
76    
77 dpavlin 190 my $index_path = Jifty::Util->app_root . '/var/invindex';
78 dpavlin 127 my $searcher = KinoSearch::Searcher->new(
79 dpavlin 190 invindex => Grep::Search::KinoSearch::Schema->open( $index_path ), );
80 dpavlin 98 $self->log->debug("$searcher created");
81 dpavlin 47
82 dpavlin 151 my $full_q = "($q)";
83 dpavlin 64
84 dpavlin 151 my $uid = Jifty->web->current_user->id;
85 dpavlin 153 $full_q .= ' AND _owner_id:' . $uid if (defined $uid);
86 dpavlin 151
87 dpavlin 127 $self->log->debug("searching for '$q' using $full_q");
88 dpavlin 153
89     my $query_parser = KinoSearch::QueryParser->new(
90 dpavlin 189 schema => Grep::Search::KinoSearch::Schema->new,
91 dpavlin 153 fields => [ qw/ title link content summary category author / ],
92     );
93     $query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax
94     my $query = $query_parser->parse( $full_q );
95 dpavlin 127 my $hits = $searcher->search(
96 dpavlin 153 query => $query,
97 dpavlin 127 # offset => $offset,
98     # num_wanted => $hits_per_page,
99     );
100 dpavlin 64
101 dpavlin 141 $self->hits( $hits->total_hits );
102 dpavlin 47
103 dpavlin 190 return sub {
104     return $hits->fetch_hit;
105     };
106 dpavlin 47 }
107    
108     =head2 finish
109    
110 dpavlin 109 $search->finish
111 dpavlin 47
112     =cut
113    
114     sub finish {
115     my $self = shift;
116 dpavlin 127 if ($self->invindexer) {
117 dpavlin 102 $self->log->debug("closing index");
118 dpavlin 127 $self->invindexer->finish;
119 dpavlin 47 }
120 dpavlin 58
121 dpavlin 110 $self->log->debug("finish");
122    
123 dpavlin 112 undef $self;
124    
125 dpavlin 151 return 1;
126 dpavlin 47 }
127    
128 dpavlin 189 package Grep::Search::KinoSearch::KeywordField;
129 dpavlin 127 use base qw( KinoSearch::Schema::FieldSpec );
130     sub analyzed { 0 }
131 dpavlin 153 #sub indexed { 1 }
132     #sub stored { 1 }
133     sub vectorized { 0 }
134 dpavlin 127
135 dpavlin 189 package Grep::Search::KinoSearch::Schema;
136 dpavlin 127
137     =head1 NAME
138    
139 dpavlin 189 Grep::Search::KinoSearch::Schema - schema definition for full-text search
140 dpavlin 127
141     =cut
142    
143     use base 'KinoSearch::Schema';
144     use KinoSearch::Analysis::PolyAnalyzer;
145    
146 dpavlin 131 our %fields = (
147 dpavlin 189 id => 'Grep::Search::KinoSearch::KeywordField',
148 dpavlin 127
149 dpavlin 189 in_feed_id => 'Grep::Search::KinoSearch::KeywordField',
150     in_feed_url => 'Grep::Search::KinoSearch::KeywordField',
151 dpavlin 127 in_feed_title => 'KinoSearch::Schema::FieldSpec',
152 dpavlin 189 in_feed_owner => 'Grep::Search::KinoSearch::KeywordField',
153     in_feed_created_on => 'Grep::Search::KinoSearch::KeywordField',
154 dpavlin 127
155     title => 'KinoSearch::Schema::FieldSpec',
156 dpavlin 189 link => 'Grep::Search::KinoSearch::KeywordField',
157 dpavlin 127 content => 'KinoSearch::Schema::FieldSpec',
158     summary => 'KinoSearch::Schema::FieldSpec',
159     category => 'KinoSearch::Schema::FieldSpec',
160     author => 'KinoSearch::Schema::FieldSpec',
161 dpavlin 189 created_on => 'Grep::Search::KinoSearch::KeywordField',
162     last_update => 'Grep::Search::KinoSearch::KeywordField',
163 dpavlin 127
164 dpavlin 189 _owner_id => 'Grep::Search::KinoSearch::KeywordField',
165 dpavlin 127 );
166    
167     sub analyzer {
168     return KinoSearch::Analysis::PolyAnalyzer->new( language => 'en' );
169     }
170    
171 dpavlin 47 1;

  ViewVC Help
Powered by ViewVC 1.1.26