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