web commit by http://id.inelegant.org/: Response.
[ikiwiki] / 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. Enrico had one that
7 he was using for debtags stuff that looked pretty good.
8
9 >> 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.
10
11 >> Of course, there are problems. ;-)
12
13 >> * 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`.
14 >> * If I kill `ikiwiki` while it's indexing, I can screw up Plucene's locks. I suspect that this will be an easy fix.
15
16 >> 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. ;-)
17
18 >> If this seems a sensible approach, I'll write the CGI interface, and clean up the plugin. -- Ben
19
20 <pre>
21 Index: IkiWiki/Plugin/search.pm
22 ===================================================================
23 --- IkiWiki/Plugin/search.pm    (revision 2755)
24 +++ IkiWiki/Plugin/search.pm    (working copy)
25 @@ -1,33 +1,55 @@
26  #!/usr/bin/perl
27 -# hyperestraier search engine plugin
28  package IkiWiki::Plugin::search;
29  
30  use warnings;
31  use strict;
32  use IkiWiki;
33  
34 +use Plucene::Analysis::SimpleAnalyzer;
35 +use Plucene::Document;
36 +use Plucene::Document::Field;
37 +use Plucene::Index::Reader;
38 +use Plucene::Index::Writer;
39 +use Plucene::QueryParser;
40 +use Plucene::Search::HitCollector;
41 +use Plucene::Search::IndexSearcher;
42 +
43 +#TODO: Run the Plucene optimiser after a rebuild
44 +#TODO: CGI query interface
45 +
46 +my $PLUCENE_DIR;
47 +# $config{wikistatedir} may not be defined at this point, so we delay setting $PLUCENE_DIR
48 +# until a subroutine actually needs it.
49 +sub init () {
50 +  error("Plucene: Statedir <$config{wikistatedir}> does not exist!") 
51 +    unless -e $config{wikistatedir};
52 +  $PLUCENE_DIR = $config{wikistatedir}.'/plucene';  
53 +}
54 +
55  sub import { #{{{
56 -       hook(type => "getopt", id => "hyperestraier",
57 -               call => \&getopt);
58 -       hook(type => "checkconfig", id => "hyperestraier",
59 +       hook(type => "checkconfig", id => "plucene",
60                 call => \&checkconfig);
61 -       hook(type => "pagetemplate", id => "hyperestraier",
62 -               call => \&pagetemplate);
63 -       hook(type => "delete", id => "hyperestraier",
64 +       hook(type => "delete", id => "plucene",
65                 call => \&delete);
66 -       hook(type => "change", id => "hyperestraier",
67 +       hook(type => "change", id => "plucene",
68                 call => \&change);
69 -       hook(type => "cgi", id => "hyperestraier",
70 -               call => \&cgi);
71  } # }}}
72  
73 -sub getopt () { #{{{
74 -        eval q{use Getopt::Long};
75 -       error($@) if $@;
76 -        Getopt::Long::Configure('pass_through');
77 -        GetOptions("estseek=s" => \$config{estseek});
78 -} #}}}
79  
80 +sub writer {
81 +  init();
82 +  return Plucene::Index::Writer->new(
83 +      $PLUCENE_DIR, Plucene::Analysis::SimpleAnalyzer->new(), 
84 +      (-e "$PLUCENE_DIR/segments" ? 0 : 1));
85 +}
86 +
87 +#TODO: Better name for this function.
88 +sub src2rendered_abs (@) {
89 +  return map { Encode::encode_utf8($config{destdir}."/$_") } 
90 +    map { @{$renderedfiles{pagename($_)}} } 
91 +    grep { defined pagetype($_) } @_;
92 +}
93 +
94  sub checkconfig () { #{{{
95         foreach my $required (qw(url cgiurl)) {
96                 if (! length $config{$required}) {
97 @@ -36,112 +58,55 @@
98         }
99  } #}}}
100  
101 -my $form;
102 -sub pagetemplate (@) { #{{{
103 -       my %params=@_;
104 -       my $page=$params{page};
105 -       my $template=$params{template};
106 +#my $form;
107 +#sub pagetemplate (@) { #{{{
108 +#      my %params=@_;
109 +#      my $page=$params{page};
110 +#      my $template=$params{template};
111 +#
112 +#      # Add search box to page header.
113 +#      if ($template->query(name => "searchform")) {
114 +#              if (! defined $form) {
115 +#                      my $searchform = template("searchform.tmpl", blind_cache => 1);
116 +#                      $searchform->param(searchaction => $config{cgiurl});
117 +#                      $form=$searchform->output;
118 +#              }
119 +#
120 +#              $template->param(searchform => $form);
121 +#      }
122 +#} #}}}
123  
124 -       # Add search box to page header.
125 -       if ($template->query(name => "searchform")) {
126 -               if (! defined $form) {
127 -                       my $searchform = template("searchform.tmpl", blind_cache => 1);
128 -                       $searchform->param(searchaction => $config{cgiurl});
129 -                       $form=$searchform->output;
130 -               }
131 -
132 -               $template->param(searchform => $form);
133 -       }
134 -} #}}}
135 -
136  sub delete (@) { #{{{
137 -       debug(gettext("cleaning hyperestraier search index"));
138 -       estcmd("purge -cl");
139 -       estcfg();
140 +       debug("Plucene: purging: ".join(',',@_));
141 +       init();
142 +  my $reader = Plucene::Index::Reader->open($PLUCENE_DIR);
143 +  my @files = src2rendered_abs(@_);
144 +  for (@files) {
145 +    $reader->delete_term( Plucene::Index::Term->new({ field => "id", text => $_ }));
146 +  }
147 +  $reader->close;
148  } #}}}
149  
150  sub change (@) { #{{{
151 -       debug(gettext("updating hyperestraier search index"));
152 -       estcmd("gather -cm -bc -cl -sd",
153 -               map {
154 -                       Encode::encode_utf8($config{destdir}."/".$_)
155 -                               foreach @{$renderedfiles{pagename($_)}};
156 -               } @_
157 -       );
158 -       estcfg();
159 +       debug("Plucene: updating search index");
160 +  init();
161 +  #TODO: Do we want to index source or rendered files?
162 +  #TODO: Store author, tags, etc. in distinct fields; may need new API hook.
163 +  my @files = src2rendered_abs(@_);
164 +  my $writer = writer();    
165 +   
166 +  for my $file (@files) {
167 +    my $doc = Plucene::Document->new;
168 +    $doc->add(Plucene::Document::Field->Keyword(id => $file));
169 +    my $data;
170 +    eval { $data = readfile($file) };
171 +    if ($@) {
172 +      debug("Plucene: can't read <$file> - $@");
173 +      next;
174 +    }
175 +    debug("Plucene: indexing <$file> (".length($data).")");
176 +    $doc->add(Plucene::Document::Field->UnStored('text' => $data));
177 +    $writer->add_document($doc);
178 +  }
179  } #}}}
180 -
181 -sub cgi ($) { #{{{
182 -       my $cgi=shift;
183 -
184 -       if (defined $cgi->param('phrase') || defined $cgi->param("navi")) {
185 -               # only works for GET requests
186 -               chdir("$config{wikistatedir}/hyperestraier") || error("chdir: $!");
187 -               exec("./".IkiWiki::basename($config{cgiurl})) || error("estseek.cgi failed");
188 -       }
189 -} #}}}
190 -
191 -my $configured=0;
192 -sub estcfg () { #{{{
193 -       return if $configured;
194 -       $configured=1;
195 -
196 -       my $estdir="$config{wikistatedir}/hyperestraier";
197 -       my $cgi=IkiWiki::basename($config{cgiurl});
198 -       $cgi=~s/\..*$//;
199 -
200 -       my $newfile="$estdir/$cgi.tmpl.new";
201 -       my $cleanup = sub { unlink($newfile) };
202 -       open(TEMPLATE, ">:utf8", $newfile) || error("open $newfile: $!", $cleanup);
203 -       print TEMPLATE IkiWiki::misctemplate("search", 
204 -               "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n",
205 -               baseurl => IkiWiki::dirname($config{cgiurl})."/") ||
206 -                       error("write $newfile: $!", $cleanup);
207 -       close TEMPLATE || error("save $newfile: $!", $cleanup);
208 -       rename($newfile, "$estdir/$cgi.tmpl") ||
209 -               error("rename $newfile: $!", $cleanup);
210 -
211 -       $newfile="$estdir/$cgi.conf";
212 -       open(TEMPLATE, ">$newfile") || error("open $newfile: $!", $cleanup);
213 -       my $template=template("estseek.conf");
214 -       eval q{use Cwd 'abs_path'};
215 -       $template->param(
216 -               index => $estdir,
217 -               tmplfile => "$estdir/$cgi.tmpl",
218 -               destdir => abs_path($config{destdir}),
219 -               url => $config{url},
220 -       );
221 -       print TEMPLATE $template->output || error("write $newfile: $!", $cleanup);
222 -       close TEMPLATE || error("save $newfile: $!", $cleanup);
223 -       rename($newfile, "$estdir/$cgi.conf") ||
224 -               error("rename $newfile: $!", $cleanup);
225 -
226 -       $cgi="$estdir/".IkiWiki::basename($config{cgiurl});
227 -       unlink($cgi);
228 -       my $estseek = defined $config{estseek} ? $config{estseek} : '/usr/lib/estraier/estseek.cgi';
229 -       symlink($estseek, $cgi) || error("symlink $estseek $cgi: $!");
230 -} # }}}
231 -
232 -sub estcmd ($;@) { #{{{
233 -       my @params=split(' ', shift);
234 -       push @params, "-cl", "$config{wikistatedir}/hyperestraier";
235 -       if (@_) {
236 -               push @params, "-";
237 -       }
238 -
239 -       my $pid=open(CHILD, "|-");
240 -       if ($pid) {
241 -               # parent
242 -               foreach (@_) {
243 -                       print CHILD "$_\n";
244 -               }
245 -               close(CHILD) || print STDERR "estcmd @params exited nonzero: $?\n";
246 -       }
247 -       else {
248 -               # child
249 -               open(STDOUT, "/dev/null"); # shut it up (closing won't work)
250 -               exec("estcmd", @params) || error("can't run estcmd");
251 -       }
252 -} #}}}
253 -
254 -1
255 +1;
256 </pre>
257
258