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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26