]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/different_search_engine.mdwn
0eab6a8b8f75e8d9a5544b0743293628bb4e1ad3
[ikiwiki.git] / doc / todo / different_search_engine.mdwn
1 After using it for a while, my feeling is that hyperestradier, as used in
2 the [[plugins/search]] plugin, is not robust enough for ikiwiki. It doesn't
3 upgrade well, and it has a habit of sig-11 on certian input from time to
4 time.
5
6 So some other engine should be found and used instead. 
7
8 Enrico had one that he was using for debtags stuff that looked pretty good.
9 That was [Xapian](http://www.xapian.org/), which has perl bindings in
10 libsearch-xapian-perl. The nice thing about xapian is that it does a ranked
11 search so it understands what words are most important in a search. (So
12 does Lucene..) Another nice thing is it supports "more documents like this
13 one" kind of search. --[[Joey]]
14
15 >> I've done a bit of prototyping on this. The current hip search library is [Lucene](http://lucene.apache.org/java/docs/). There's a Perl port called [Plucene](http://search.cpan.org/~tmtm/Plucene-1.25/). Given that it's already packaged, as `libplucene-perl`, I assumed it would be a good starting point. I've written a **very rough** patch against `IkiWiki/Plugin/search.pm` to handle the indexing side (there's no facility to view the results yet, although I have a command-line interface working). That's below, and should apply to SVN trunk.
16
17 >> Of course, there are problems. ;-)
18
19 >> * Plucene throws up a warning when running under Taint mode. There's a patch on the mailing list, but I haven't tried applying it yet. So for now you'll have to build IkiWiki with `NOTAINT=1 make install`.
20 >> * If I kill `ikiwiki` while it's indexing, I can screw up Plucene's locks. I suspect that this will be an easy fix.
21
22 >> There is a [C++ port of Lucene](http://sourceforge.net/projects/clucene/) which is packaged as `libclucene0`. The Perl interface to this is called [Lucene](http://search.cpan.org/~tbusch/Lucene-0.09/lib/Lucene.pm). This is supposed to be significantly faster, and presumably won't have the taint bug. The API is virtually the same, so it will be easy to switch over. I'd use this now, were it not for the lack of package. (I assume you won't want to make core functionality depend on installing a module from CPAN). I've never built a Debian package before, so I can either learn then try building this, or somebody else could do the honours. ;-)
23
24 >> If this seems a sensible approach, I'll write the CGI interface, and clean up the plugin. -- Ben
25
26 >>> The weird thing about lucene is that these are all reimplmentations of
27 >>> it. Thank you java.. The C++ version seems like a better choice to me
28 >>> (packages are trivial). --[[Joey]]
29
30 <pre>
31 Index: IkiWiki/Plugin/search.pm
32 ===================================================================
33 --- IkiWiki/Plugin/search.pm    (revision 2755)
34 +++ IkiWiki/Plugin/search.pm    (working copy)
35 @@ -1,33 +1,55 @@
36  #!/usr/bin/perl
37 -# hyperestraier search engine plugin
38  package IkiWiki::Plugin::search;
39  
40  use warnings;
41  use strict;
42  use IkiWiki;
43  
44 +use Plucene::Analysis::SimpleAnalyzer;
45 +use Plucene::Document;
46 +use Plucene::Document::Field;
47 +use Plucene::Index::Reader;
48 +use Plucene::Index::Writer;
49 +use Plucene::QueryParser;
50 +use Plucene::Search::HitCollector;
51 +use Plucene::Search::IndexSearcher;
52 +
53 +#TODO: Run the Plucene optimiser after a rebuild
54 +#TODO: CGI query interface
55 +
56 +my $PLUCENE_DIR;
57 +# $config{wikistatedir} may not be defined at this point, so we delay setting $PLUCENE_DIR
58 +# until a subroutine actually needs it.
59 +sub init () {
60 +  error("Plucene: Statedir <$config{wikistatedir}> does not exist!") 
61 +    unless -e $config{wikistatedir};
62 +  $PLUCENE_DIR = $config{wikistatedir}.'/plucene';  
63 +}
64 +
65  sub import { #{{{
66 -       hook(type => "getopt", id => "hyperestraier",
67 -               call => \&getopt);
68 -       hook(type => "checkconfig", id => "hyperestraier",
69 +       hook(type => "checkconfig", id => "plucene",
70                 call => \&checkconfig);
71 -       hook(type => "pagetemplate", id => "hyperestraier",
72 -               call => \&pagetemplate);
73 -       hook(type => "delete", id => "hyperestraier",
74 +       hook(type => "delete", id => "plucene",
75                 call => \&delete);
76 -       hook(type => "change", id => "hyperestraier",
77 +       hook(type => "change", id => "plucene",
78                 call => \&change);
79 -       hook(type => "cgi", id => "hyperestraier",
80 -               call => \&cgi);
81  } # }}}
82  
83 -sub getopt () { #{{{
84 -        eval q{use Getopt::Long};
85 -       error($@) if $@;
86 -        Getopt::Long::Configure('pass_through');
87 -        GetOptions("estseek=s" => \$config{estseek});
88 -} #}}}
89  
90 +sub writer {
91 +  init();
92 +  return Plucene::Index::Writer->new(
93 +      $PLUCENE_DIR, Plucene::Analysis::SimpleAnalyzer->new(), 
94 +      (-e "$PLUCENE_DIR/segments" ? 0 : 1));
95 +}
96 +
97 +#TODO: Better name for this function.
98 +sub src2rendered_abs (@) {
99 +  return map { Encode::encode_utf8($config{destdir}."/$_") } 
100 +    map { @{$renderedfiles{pagename($_)}} } 
101 +    grep { defined pagetype($_) } @_;
102 +}
103 +
104  sub checkconfig () { #{{{
105         foreach my $required (qw(url cgiurl)) {
106                 if (! length $config{$required}) {
107 @@ -36,112 +58,55 @@
108         }
109  } #}}}
110  
111 -my $form;
112 -sub pagetemplate (@) { #{{{
113 -       my %params=@_;
114 -       my $page=$params{page};
115 -       my $template=$params{template};
116 +#my $form;
117 +#sub pagetemplate (@) { #{{{
118 +#      my %params=@_;
119 +#      my $page=$params{page};
120 +#      my $template=$params{template};
121 +#
122 +#      # Add search box to page header.
123 +#      if ($template->query(name => "searchform")) {
124 +#              if (! defined $form) {
125 +#                      my $searchform = template("searchform.tmpl", blind_cache => 1);
126 +#                      $searchform->param(searchaction => $config{cgiurl});
127 +#                      $form=$searchform->output;
128 +#              }
129 +#
130 +#              $template->param(searchform => $form);
131 +#      }
132 +#} #}}}
133  
134 -       # Add search box to page header.
135 -       if ($template->query(name => "searchform")) {
136 -               if (! defined $form) {
137 -                       my $searchform = template("searchform.tmpl", blind_cache => 1);
138 -                       $searchform->param(searchaction => $config{cgiurl});
139 -                       $form=$searchform->output;
140 -               }
141 -
142 -               $template->param(searchform => $form);
143 -       }
144 -} #}}}
145 -
146  sub delete (@) { #{{{
147 -       debug(gettext("cleaning hyperestraier search index"));
148 -       estcmd("purge -cl");
149 -       estcfg();
150 +       debug("Plucene: purging: ".join(',',@_));
151 +       init();
152 +  my $reader = Plucene::Index::Reader->open($PLUCENE_DIR);
153 +  my @files = src2rendered_abs(@_);
154 +  for (@files) {
155 +    $reader->delete_term( Plucene::Index::Term->new({ field => "id", text => $_ }));
156 +  }
157 +  $reader->close;
158  } #}}}
159  
160  sub change (@) { #{{{
161 -       debug(gettext("updating hyperestraier search index"));
162 -       estcmd("gather -cm -bc -cl -sd",
163 -               map {
164 -                       Encode::encode_utf8($config{destdir}."/".$_)
165 -                               foreach @{$renderedfiles{pagename($_)}};
166 -               } @_
167 -       );
168 -       estcfg();
169 +       debug("Plucene: updating search index");
170 +  init();
171 +  #TODO: Do we want to index source or rendered files?
172 +  #TODO: Store author, tags, etc. in distinct fields; may need new API hook.
173 +  my @files = src2rendered_abs(@_);
174 +  my $writer = writer();    
175 +   
176 +  for my $file (@files) {
177 +    my $doc = Plucene::Document->new;
178 +    $doc->add(Plucene::Document::Field->Keyword(id => $file));
179 +    my $data;
180 +    eval { $data = readfile($file) };
181 +    if ($@) {
182 +      debug("Plucene: can't read <$file> - $@");
183 +      next;
184 +    }
185 +    debug("Plucene: indexing <$file> (".length($data).")");
186 +    $doc->add(Plucene::Document::Field->UnStored('text' => $data));
187 +    $writer->add_document($doc);
188 +  }
189  } #}}}
190 -
191 -sub cgi ($) { #{{{
192 -       my $cgi=shift;
193 -
194 -       if (defined $cgi->param('phrase') || defined $cgi->param("navi")) {
195 -               # only works for GET requests
196 -               chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
197 -               exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
198 -       }
199 -} #}}}
200 -
201 -my $configured=0;
202 -sub estcfg () { #{{{
203 -       return if $configured;
204 -       $configured=1;
205 -
206 -       my $estdir="$config{wikistatedir}/hyperestraier";
207 -       my $cgi=IkiWiki::basename($config{cgiurl});
208 -       $cgi=~s/\..*$//;
209 -
210 -       my $newfile="$estdir/$cgi.tmpl.new";
211 -       my $cleanup = sub { unlink($newfile) };
212 -       open(TEMPLATE, ">:utf8", $newfile) || error("open $newfile: $!", $cleanup);
213 -       print TEMPLATE IkiWiki::misctemplate("search", 
214 -               "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
215 -               baseurl => IkiWiki::dirname($config{cgiurl})."/") ||
216 -                       error("write $newfile: $!", $cleanup);
217 -       close TEMPLATE || error("save $newfile: $!", $cleanup);
218 -       rename($newfile, "$estdir/$cgi.tmpl") ||
219 -               error("rename $newfile: $!", $cleanup);
220 -
221 -       $newfile="$estdir/$cgi.conf";
222 -       open(TEMPLATE, ">$newfile") || error("open $newfile: $!", $cleanup);
223 -       my $template=template("estseek.conf");
224 -       eval q{use Cwd 'abs_path'};
225 -       $template->param(
226 -               index => $estdir,
227 -               tmplfile => "$estdir/$cgi.tmpl",
228 -               destdir => abs_path($config{destdir}),
229 -               url => $config{url},
230 -       );
231 -       print TEMPLATE $template->output || error("write $newfile: $!", $cleanup);
232 -       close TEMPLATE || error("save $newfile: $!", $cleanup);
233 -       rename($newfile, "$estdir/$cgi.conf") ||
234 -               error("rename $newfile: $!", $cleanup);
235 -
236 -       $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
237 -       unlink($cgi);
238 -       my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
239 -       symlink($estseek, $cgi) || error("symlink $estseek $cgi: $!");
240 -} # }}}
241 -
242 -sub estcmd ($;@) { #{{{
243 -       my @params=split(' ', shift);
244 -       push @params, "-cl", "$config{wikistatedir}/hyperestraier";
245 -       if (@_) {
246 -               push @params, "-";
247 -       }
248 -
249 -       my $pid=open(CHILD, "|-");
250 -       if ($pid) {
251 -               # parent
252 -               foreach (@_) {
253 -                       print CHILD "$_\n";
254 -               }
255 -               close(CHILD) || print STDERR "estcmd @params exited nonzero: $?\n";
256 -       }
257 -       else {
258 -               # child
259 -               open(STDOUT, "/dev/null"); # shut it up (closing won't work)
260 -               exec("estcmd", @params) || error("can't run estcmd");
261 -       }
262 -} #}}}
263 -
264 -1
265 +1;
266 </pre>
267
268