SWISH.CGI(7)
π SWISH.CGI π
okay bestie, grab a strawberry π and sit down, because this is the manual page for swish.cgi, and it's a whole thing.
β¨ NAME β¨
swish.cgi β the example Perl script for searching with the SWISH-E search engine. it's giving demo-script-that-actually-slaps π
πΈ DESCRIPTION πΈ
okay so first things first: swish.cgi is a CGI script for searching with the SWISH-E search engine, version 2.1-dev and above. no cap. and the way it serves results? one page at a time, with the matching words from the source document highlighted β¨, and it shows you a few words of content on either side of the highlighted word. so you get context, not just a sad lonely keyword. iconic.
the script is highly configurable. fr fr. features include:
- searching multiple (or selectable) indexes π
- limiting searches to a subset of documents
- sorting by a number of different properties
- limiting results to a date range π
where it lives on disk ποΈ
on unix type systems, the swish.cgi script is installed in the directory $prefix/lib/swish-e, which is typically /usr/local/lib/swish-e. and if you're like "bestie I don't want it there", you can override that with the configure options --prefix or --libexecdir. slay.
config vibes π
the standard configuration (i.e. the one where you're not using a config file) should work with most swish index files. periodt.
but β and hear me out β customization of the parameters will be needed if you are indexing special meta data and want to search and/or display the meta data. so like, if you're doing fancy stuff, prepare to do fancy config.
you can modify the configuration two ways:
- by editing this script directly, or
- by using a configuration file (
.swishcgi.confby default).
the script's configuration file is described below. π
β bestie. real talk. you are strongly encouraged to get the default configuration working before making changes. most problems using this script are the result of configuration modifications. like, you changed one little thing and now nothing works and you're sobbing. do not be that girl. get the default working first. π
it's modular, obviously π
the script is modular in design β both the highlighting code and the output generation are handled by modules, which are included in the example/modules distribution directory and installed in the $libexecdir/perl directory. this allows for easy customization of the output without changing the main CGI script. so you can glow up your output and never touch the core. obsessed.
included with the Swish-e distribution is a module to generate standard HTML output. β¨ and there's also modules and template examples to use with the popular Perl templating systems HTML::Template and Template-Toolkit. this is very useful if your site already uses one of these templating systems.
heads up though: the HTML::Template and Template-Toolkit packages are not distributed with Swish-e. they are available from the CPAN (http://search.cpan.org). go get 'em yourself bestie π«Ά
mod_perl moment π₯
this scipt can also run basically unmodified as a mod_perl handler, providing much better performance than running as a CGI script. usage under mod_perl is described below. if you want speed, this is the way. main character energy fr.
pls read the rest π
please read the rest of the documentation. there's a DEBUGGING section, and a FAQ section. i know, i know, nobody reads the docs, but these ones actually help.
and one more thing: this script should work on Windows, but security may be an issue. π make of that what you will.
β REQUIREMENTS β
- a reasonably current version of Perl.
5.00503or above is recommended (anything older will not be supported). no ancient perl bestie, it's not the vibe. π« - the Date::Calc module is required to use the date range feature of the script. π it's also available from CPAN.
so if you want date range filtering, go install Date::Calc. if you don't, you can skip it. but like... date ranges are cute. just saying. π
πΈ INSTALLATION πΈ
okay bestie, here's a whole lil example install session, done on Linux. other operating systems are basically the same vibe, so don't sweat it π
so, for the sake of keeping this example β¨simpleβ¨, literally every file gets dropped in web server space β including stuff like the swish-e index and config files that like, normally should NOT be reachable through the web server. once the script is actually running you're gonna want to lock those down. either move them somewhere else (and adjust the script's config accordingly) or use web server features to restrict access (like with .htaccess). π«Ά
please please please get a simple installation working before you go modifying the config file. like, most of the problems people report with this script? improper configuration. periodt. π
the script's default settings are set up for initial testing. by default it expects to find most files β and the swish-e binary β in the same directory as the script. π₯
for security reasons, once you've tested the script you'll wanna change settings so the web server can't reach some of those files (either by moving them out of web space, or using access control like .htaccess). there's an example of using .htaccess on Apache down below π
it's assumed that swish-e has already been unpacked, that the swish-e binary has been compiled from source, and that "make install" has been run. if swish-e came from a vendor package (like an RPM or a Debian package), go check that pakage's documentation for where the files land. ngl, typo's theirs not mine π
π Example Installation π
1. Symlink or copy the swish.cgi.
symlink (or copy, if your platform or webserver doesn't do symlinks) the swish.cgi script from the installation directory into a local directory. typically that'd be the cgi-bin directory, or wherever CGI scripts live. in this example a new directory gets created and the script gets symlinked. slay β¨
~$ mkdir swishdir
~$ cd swishdir
~/swishdir$ ln -s /usr/local/lib/swish-e/swish.cgi
the installation directory is set at configure time with the --prefix or --libexecdir options, but by default it's in /usr/local/lib/swish-e. π
2. Create an index.
grab an editor and make a simple config file for indexing your files. in this example the Apache documentation is what's getting indexed. then we run a simple query to test that the index actually works. π
~/swishdir$ cat swish.conf
IndexDir /usr/local/apache/htdocs
IndexOnly .html .htm
DefaultContents HTML*
StoreDescription HTML* <body> 200000
MetaNames swishdocpath swishtitle
ReplaceRules remove /usr/local/apache/
if you don't have the Apache docs installed, then just pick another directory to index, like /usr/share/doc. π
create the index:
~/swishdir$ swish-e -c swish.conf
Indexing Data Source: "File-System"
Indexing "/usr/local/apache/htdocs"
Removing very common words...
no words removed.
Writing main index...
Sorting words ...
Sorting 7005 words alphabetically
Writing header ...
Writing index entries ...
Writing word text: Complete
Writing word hash: Complete
Writing word data: Complete
7005 unique words indexed.
5 properties sorted.
124 files indexed. 1485844 total bytes. 171704 total words.
Elapsed time: 00:00:02 CPU time: 00:00:02
Indexing done!
now verify the index is searchable:
~/swishdir$ swish-e -w install -m 1
# SWISH format: 2.1-dev-25
# Search words: install
# Number of hits: 14
# Search time: 0.001 seconds
# Run time: 0.040 seconds
1000 htdocs/manual/dso.html "Apache 1.3 Dynamic Shared Object (DSO) support" 17341
.
let's see what files we got in our directory now bestie:
~/swishdir$ ls -1
index.swish-e
index.swish-e.prop
swish.cgi
swish.conf
3. Test the CGI script.
this step is literally so simple but it gets overlooked all the time π you should test from the command line instead of just jumping ahead and testing with the web server. peep the "DEBUGGING" section below for more. π
~/swishdir$ ./swish.cgi | head
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
Search our site
</title>
</head>
<body>
what that shows is that the script can be run directly, and it spits out a correct HTTP header and HTML. iconic. π
if you run the above and see something like this instead:
~/swishdir >./swish.cgi
bash: ./swish.cgi: No such file or directory
then you probably need to edit the script so it points at the correct location of your perl program. here's one way to find out where perl lives (again, on unix):
~/swishdir$ which perl
/usr/local/bin/perl
~/swishdir$ /usr/local/bin/perl -v
This is perl, v5.6.0 built for i586-linux
...
good! we're using a reasonably current version of perl. β
now that we know perl is at /usr/local/bin/perl we can adjust the "shebang" line in the perl script (like, the first line of the script):
~/swishdir$ pico swish.cgi
(edit the #! line)
~/swishdir$ head -1 swish.cgi
#!/usr/local/bin/perl -w
4. Test with the web server.
how you do this is completely dependent on your web server, and you might need to talk to your web server admin to get it working. often files with the .cgi extension are automatically set up to run as CGI scripts, but like, not always?? in other words, this step is really up to you to figure out. π
this example shows creating a symlink from the web server space to the directory used above. this only works if the web server is configured to follow symbolic links (which is the Apache default).
this operation requires root access:
~/swishdir$ su -c "ln -s $HOME/swishdir /usr/local/apache/htdocs/swishdir"
Password: *********
if your account is on an ISP and your web directory is ~/public_html, then you might just move the entire directory:
mv ~/swishdir ~/public_html
now, let's make a real HTTP request:
~/swishdir$ GET http://localhost/swishdir/swish.cgi | head -3
#!/usr/local/bin/perl -w
package SwishSearch;
use strict;
oh, darn. π looks like Apache isn't running the script and is instead returning it as a static page. Apache needs to be told that swish.cgi is a CGI script.
.htaccess comes to the rescue:
~/swishdir$ cat .htaccess
# Deny everything by default
Deny From All
# But allow just CGI script
<files swish.cgi>
Options ExecCGI
Allow From All
SetHandler cgi-script
</files>
that "Deny From All" blocks access to all the files (like config and index files), and only access to the swish.cgi script is allowed. π
let's try the request one more time:
~/swishdir >GET http://localhost/swishdir/swish.cgi | head
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
Search our site
</title>
</head>
<body>
<h2>
<a href="http://swish-e.org">
that looks better! now go use your web browser to test. π
now, you may notice that the links on the search results page aren't valid. the swish config file contained the line:
ReplaceRules remove /usr/local/apache/
to make those links work (and assuming your web server will follow symbolic links):
~/swishtest$ ln -s /usr/local/apache/htdocs
btw β the "GET" used above is a program that comes with Perl's LWP library. if you don't have it, you might try something like:
wget -O - http://localhost/swishdir/swish.cgi | head
and if nothing else works, you can always just telnet to the web server and make a basic request. π¦
~/swishtest$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /swishtest/swish.cgi http/1.0
HTTP/1.1 200 OK
Date: Wed, 13 Feb 2002 20:14:31 GMT
Server: Apache/1.3.20 (Unix) mod_perl/1.25_01
Connection: close
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
Search our site
</title>
</head>
<body>
this may seem like a lot of work compared to just using a browser, but ngl β browsers are a poor tool for basic CGI debugging. π
if you have problems, check the "DEBUGGING" section below.
π CONFIGURATION π
okay so bestie, here's the tea π΅ β if you wanna change where the swish-e binary lives, or where the index file is, or use multiple indexes, or add extra metanames and properties, or switch up the default highlighting behavior, or basically anything like thatβ¦ you're gonna need to fiddle with the script's configuration settings. that's the whole vibe. β¨
but fr fr, and I cannot stress this enough: get a test setup working with the default parameters FIRST before you go changing any configuration settings. debug one thing at a time, babes. one. thing. at. a. time. π
so in general, you'll need to adjust the script's settings to match the index file you're searching. like, say you're indexing a hypermail list archive β you might wanna make the script use metanames/properties of Subject, Author, and Email address. or maybe you wanna provide a way to limit searches to subsets of documents (like, parts of your directory tree). whatever fits your world πΈ
to make things somewhat "simple" (their words, not mine π), the configuration parameters are included near the top of the swish.cgi program. that is the only place where the individual parameters are defined and explained, so you're gonna have to open up the swish.cgi script in an editor to actually view the options. further questions about individual settings should go to the swish-e discussion list. π
the parameters are all part of a perl "hash" structure, and the comments at the top of the program should get you going. now, the perl hash structure may seem a bit confusing at first, but it makes it easy to create nested and complex parameters. syntax is important, so cut-n-paste should be your best defense if you are not a perl programmer. no cap. π«Ά
by the way, Perl has a number of quote operators. for example, to quote a string you might write:
title => 'Search My Site',
some options take more than one parameter, where each parameter must be quoted. for example:
metanames => [ 'swishdefault', 'swishtitle', 'swishdocpath' ],
which assigns an array ( [...] ) of three strings to the "metanames" variable. lists of quoted strings are sooo common in perl that there's a special operator called qw (quote word) to save typing all those quotes:
metanames => [ qw/ swishdefault swishtitle swishdocpath / ],
or to use the parenthesis as the quote character (you can pick any):
metanames => [ qw( swishdefault swishtitle swishdocpath ) ],
there are two options for changing the configuration settings from their default values: one way is to edit the script directly, or the other way is to use a separate configuration file. either way, the configuration settings are a basic perl hash reference. π
using a configuration file is described below, but it contains the same hash structure.
there are many configuration settings, and some of them are commented out either by using a # symbol, or by simply renaming the configuration directive (e.g. by adding an "x" to the parameter name). sneaky, right? π
a very basic configuration setup might look like:
return {
title => 'Search the Swish-e list', # Title of your choice.
swish_binary => 'swish-e', # Location of swish-e binary
swish_index => 'index.swish-e', # Location of your index file
};
or if searching more than one index:
return {
title => 'Search the Swish-e list',
swish_binary => 'swish-e',
swish_index => ['index.swish-e', 'index2'],
};
both of these examples return a reference to a perl hash ( return {...} ). in the second example, the multiple index files are set as an array reference. it's giving nested data structures and honestly? iconic. β¨
note that in the example above the swish-e binary file is relative to the current directory. if running under mod_perl you will need to use absolute paths. β οΈ
the script can also use the SWISH::API perl module (included with the swish-e distribution in the perl directory) to access the swish-e index. the use_library option is used to enable the use of the SWISH::API module:
return {
title => 'Search the Swish-e list',
swish_index => ['index.swish-e', 'index2'],
use_library => 1, # enable use of the SWISH::API module
};
the module must be available via the @INC array, like all Perl modules. π
using the SWISH::API module avoids the need to fork and execute a the swish-e program. under mod_perl you may see a significant performance improvement when using the SWISH::API module. under normal CGI usage you will probably not see any speed improvements. lowkey sad but that's just how it is π
πΈ Using A Configuration File πΈ
as mentioned above, configuration settings can be either set in the swish.cgi script, or set in a separate configuration file. settings in a configuration file will override the settings in the script. π
by default, the swish.cgi script will attempt to read settings from the file .swishcgi.conf. for example, you might only wish to change the title used in the script. simply create a file called .swishcgi.conf in the same directory as the CGI script:
> cat .swishcgi.conf
# Example swish.cgi configuration script.
return {
title => 'Search Our Mailing List Archive',
};
the settings you use will depend on the index you create with swish:
return {
title => 'Search the Apache documentation',
swish_binary => 'swish-e',
swish_index => 'index.swish-e',
metanames => [qw/swishdefault swishdocpath swishtitle/],
display_props => [qw/swishtitle swishlastmodified swishdocsize swishdocpath/],
title_property => 'swishdocpath',
prepend_path => 'http://myhost/apachedocs',
name_labels => {
swishdefault => 'Search All',
swishtitle => 'Title',
swishrank => 'Rank',
swishlastmodified => 'Last Modified Date',
swishdocpath => 'Document Path',
swishdocsize => 'Document Size',
},
};
the above configuration defines metanames to use on the form. searches can be limited to these metanames. π
display_props tells the script to display the property swishlastmodified (the last modified date of the file), the document size, and path with the search results.
the parameter name_labels is a hash (reference) that is used to give friendly names to the metanames. cute lil nicknames for your metanames fr fr π₯Ή
here's another example. say you want to search either (or both) the Apache 1.3 documentation and the Apache 2.0 documentation indexed seperately.
return {
title => 'Search the Apache Documentation',
date_ranges => 0,
swish_index => [ qw/ index.apache index.apache2 / ],
select_indexes => {
method => 'checkbox_group',
labels => [ '1.3.23 docs', '2.0 docs' ], # Must match up one-to-one to swish_index
description => 'Select: ',
},
};
now you can select either or both sets of documentation while searching. versatile behavior, we love to see it π
all the possible settings are included in the default configuration located near the top of the swish.cgi script. open the swish.cgi script with an editor to look at the various settings. contact the Swish-e Discussion list for help in configuring the script. π«Ά
Okay bestie, real talk: like 99% of the problems people have with this script are, at the end of the day, a config situation. π So before you go touching a single configuration setting, get the script working with the default settings first, mmkay? Defaults first, customizing second. That's the order. No skipping.
The key to debugging CGI scripts is to run them from the command line, NOT with a browser. Fr fr. Adding a browser and a web server into the mix only makes everything messier.
β¨ step 1: does it even compile β¨
First, make sure the program compiles correctly:
$ perl -c swish.cgi
swish.cgi syntax OK
If you get that swish.cgi syntax OK, slay, we're so back. π
β¨ step 2: just run it β¨
Next, simply try running the program:
$ ./swish.cgi | head
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
Search our site
</title>
</head>
<body>
Under Windows you will need to run the script as:
C:\wwwroot\swishtest> perl swish.cgi
So now you know that the program compiles AND will run from the command line. π¦ Next, try accessing the script from a web browser.
β¨ step 3: the browser vibe check β¨
If you see the contents of the CGI script instead of its output, then your web server is not configured to run the script. With Apache, look at settings like ScriptAlias, SetHandler, and Options.
If an error is reported (such as Internal Server Error or Forbidden) you need to locate your web server's error_log file and carefully read what the problem is. Contact your web administrator for help locating the web server's error log.
If you don't have access to the web server's error_log file, you can modify the script to report errors to the browser screen. Open the script and search for "CGI::Carp". (Author's suggestion is to debug from the command line β adding the browser and web server into the equation only complicates debugging.) π
β¨ SWISH_DEBUG: the main character β¨
The script does offer some basic debugging options that allow debugging from the command line. They're enabled by setting an environment variable called SWISH_DEBUG. How that is set depends on your operating system and the shell you are using. These examples are using the bash shell syntax.
Note: You can also use the debug_options configuration setting, but the recommended method is to set the environment variable.
You can list the available debugging options like this:
$ SWISH_DEBUG=help ./swish.cgi >outfile
Unknown debug option 'help'. Must be one of:
basic: Basic debugging
command: Show command used to run swish
headers: Show headers returned from swish
output: Show output from swish
summary: Show summary of results
dump: Show all data available to templates
Debugging options may be combined, queen:
$ SWISH_DEBUG=command,headers,summary ./swish.cgi >outfile
You will be asked for an input query and the max number of results to return. You can use the defaults in most cases. It's a good idea to redirect output to a file. Any error messages are sent to stderr, so those will still be displayed (unless you redirect stderr, too).
Here are some examples:
~/swishtest$ SWISH_DEBUG=basic ./swish.cgi >outfile
Debug level set to: 1
Enter a query [all]:
Using 'not asdfghjklzxcv' to match all records
Enter max results to display [1]:
------ Can't use DateRanges feature ------------
Script will run, but you can't use the date range feature
Can't locate Date/Calc.pm in @INC (@INC contains: modules /usr/local/lib/perl5/5.6.0/i586-linux /usr/local/lib/perl5/5.6.0 /usr/local/lib/perl5/site_perl/5.6.0/i586-linux /usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl/5.005/i586-linux /usr/local/lib/perl5/site_perl/5.005 /usr/local/lib/perl5/site_perl .) at modules/DateRanges.pm line 107, <STDIN> line 2.
BEGIN failed--compilation aborted at modules/DateRanges.pm line 107, <STDIN> line 2.
Compilation failed in require at ./swish.cgi line 971, <STDIN> line 2.
--------------
Can't exec "./swish-e": No such file or directory at ./swish.cgi line 1245, <STDIN> line 2.
Child process Failed to exec './swish-e' Error: No such file or directory at ./swish.cgi line 1246, <STDIN> line 2.
Failed to find any results
So the above is telling us there's two problems, bestie. First problem: the Date::Calc module is not installed. The Date::Calc module is needed to use the date limiting feature of the script.
The second problem is a bit more serious ngl. It's saying that the script can't find the swish-e binary file. In this example it's specified as being in the current directory. Either correct the path to the swish-e binary, or make a local copy or symlink to the swish-e binary.
~/swishtest$ cat .swishcgi.conf
return {
title => 'Search the Apache Documentation',
swish_binary => '/usr/local/bin/swish-e',
date_ranges => 0,
};
Now, let's try again:
~/swishtest$ SWISH_DEBUG=basic ./swish.cgi >outfile
Debug level set to: 1
---------- Read config parameters from '.swishcgi.conf' ------
$VAR1 = {
'date_ranges' => 0,
'title' => 'Search the Apache Documentation'
};
-------------------------
Enter a query [all]:
Using 'not asdfghjklzxcv' to match all records
Enter max results to display [1]:
Found 1 results
Can't locate SWISH::TemplateDefault.pm in @INC (@INC contains: modules /usr/local/lib/perl5/5.6.0/i586-linux /usr/local/lib/perl5/5.6.0 /usr/local/lib/perl5/site_perl/5.6.0/i586-linux /usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl/5.005/i586-linux /usr/local/lib/perl5/site_perl/5.005 /usr/local/lib/perl5/site_perl .) at ./swish.cgi line 608.
This means that the swish.cgi script could not locate a required module. To correct this, locate where the SWISH::Template module is installed and add a use lib line to your configuration file (or to the swish.cgi script):
~/swishtest$ cat .swishcgi.conf
use lib '/home/bill/local/lib/perl';
return {
title => 'Search the Apache Documentation',
date_ranges => 0,
};
~/swishtest$ SWISH_DEBUG=basic ./swish.cgi >outfile
Debug level set to: 1
---------- Read config parameters from '.swishcgi.conf' ------
$VAR1 = {
'date_ranges' => 0,
'title' => 'Search the Apache Documentation'
};
-------------------------
Enter a query [all]:
Using 'not asdfghjklzxcv' to match all records
Enter max results to display [1]:
Found 1 results
That is much better! ππ
β¨ how use lib actually works β¨
The use lib statement tells Perl where to look for modules by adding the path supplied to an array called @INC.
Note that most modules are in the SWISH namespace. For example, the default output module is called SWISH::TemplateDefault. When Perl is looking for that module it is looking for the file SWISH/TemplateDefault.pm. If the use lib statement is set as:
use lib '/home/bill/local/lib/perl';
then Perl will look (among other places) for the file
/home/bill/local/lib/perl/SWISH/TemplateDefault.pm
when attempting to load the SWISH::TemplateDefault module. Relative paths may also be used.
use lib 'modules';
will cause Perl to look for the file:
./modules/SWISH/TemplateDefault.pm
relative to where the swish.cgi script is running. (This is not true when running under mod_perl.)
β¨ "Swish returned unknown output" π β¨
Here's another common problem. Everything checks out, but when you run the script you see the message:
Swish returned unknown output
Ok, let's find out what output it is returning:
~/swishtest$ SWISH_DEBUG=headers,output ./swish.cgi >outfile
Debug level set to: 13
---------- Read config parameters from '.swishcgi.conf' ------
$VAR1 = {
'swish_binary' => '/usr/local/bin/swish-e',
'date_ranges' => 0,
'title' => 'Search the Apache Documentation'
};
-------------------------
Enter a query [all]:
Using 'not asdfghjklzxcv' to match all records
Enter max results to display [1]:
usage: swish [-i dir file ... ] [-S system] [-c file] [-f file] [-l] [-v (num)]
...
version: 2.0
docs: http://sunsite.berkeley.edu/SWISH-E/
*** 9872 Failed to run swish: 'Swish returned unknown output' ***
Failed to find any results
Oh, looks like /usr/local/bin/swish-e is version 2.0 of swish. We need 2.1-dev and above! β
π Frequently Asked Questions π
okay bestie, these are the questions literally everyone asks π the classics, the greatest hits, the ones that keep coming back like that one ex. sit down, grab a snack, let's go.
How do I change the way the output looks?
so the script uses a module to generate its output, and by default that module is SWISH::TemplateDefault.pm. which module gets used is selected in the swish.cgi configuration file. modules live in the example/modules/SWISH directory in the distribution, but they're installed over in the $prefix/lib/swish-e/perl/SWISH/ directory. π¦
for cute lil simple changes you can just edit the installed SWISH::TemplatDefault module directly (no cap). otherwise? make a copy of the module and change its package name. for example, cd to wherever the installed module lives and copy it to a new name:
$ cp TemplateDefault.pm MyTemplateDefault.pm
then at the top of the module, adjust the "package" line to:
package SWISH::MyTemplateDefault;
to actually use this module you need to tweak the configuration settings (either at the top of swish.cgi or in a configuration file):
template => {
package => 'SWISH::MyTemplateDefault',
},
and here's the slay part: the module does not need to be in the SWISH namespace at all, and it can be stored literally anywhere β as long as the module can be found via the @INC array (i.e. modify the "use lib" statement in swish.cgi if needed). β¨
How do I use a templating system with swish.cgi?
so besides TemplateDefault.pm, the swish-e distribution also comes with two other Perl modules for generating output using the templating systems HTML::Template and Template-Toolkit. π
templating systems use template files to generate the HTML, and they make maintaining the look of a big (or tiny) site so much easier. HTML::Template and Template-Toolkit are separate packages and can be downloaded from the CPAN. see http://search.cpan.org. π
two basic templates are provided as examples for generating output with these templating systems, and they live in the example directory. the module SWISH::TemplateHTMLTemplate uses the file swish.tmpl to generate its output, while the module SWISH::TemplateToolkit uses the swish.tt file. (note: swish.tt was renamed from search.tt Jun 03, 2004.) π
to use either of these modules you'll need to adjust the "template" configuration setting. examples for both templating systems are provided in the configuration settings near the top of the swish.cgi program. π₯
ngl, use of these modules is an advanced usage of swish.cgi and they're provided as examples only.
all of the output generation modules get passed a hash with the results from the search, plus other data used to create the output page. you can peek at this hash by using the debugging option "dump" or by using the included SWISH::TemplateDumper module:
~/swishtest >cat .swishcgi.conf
return {
title => 'Search the Apache Documentation',
template => {
package => 'SWISH::TemplateDumper',
},
};
and run a query. for example:
http://localhost/swishtest/swish.cgi?query=install
Why are there three different highlighting modules?
three highlighting modules are included with the swish-e distribution, and each one is a trade-off of speed vs. accuracy:
SWISH::DefaultHighlight - reasonably fast, but does not highlight phrases
SWISH::PhraseHighlight - reasonably slow, but is reasonably accurate
SWISH::SimpleHighlight - fast, some phrases, but least accurate
eh, the default is actually "PhraseHighlight". oh well. π
and fr fr, all of the highlighting modules slow down the script. optimizations to these modules are welcome! π
My ISP doesn't provide access to the web server logs
there are a number of options here, bestie. one way is to use the CGI::Carp module. search in the swish.cgi script for:
use Carp;
# Or use this instead -- PLEASE see perldoc CGI::Carp for details
# use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
and change it to look like:
#use Carp;
# Or use this instead -- PLEASE see perldoc CGI::Carp for details
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
β οΈ this should be only for debugging purposes!! if you use this in production you may end up sending quite ugly and confusing messages to your browsers. you have been warned, bestie. π¨
Why does the output show (NULL)?
swish-e displays (NULL) when it's attempting to display a property that does not exist in the index. π
the most common reason for this message is that you did not use StoreDescription in your config file while indexing:
StoreDescription HTML* <body> 200000
that tells swish to store the first 200,000 characters of text extracted from the body of each document parsed by the HTML parser. the text is stored as property "swishdescription".
the index must be recreated after changing the swish-e configuration. π
running:
~/swishtest > ./swish-e -T index_metanames
will display the properties defined in your index file.
this can happen with other properties too, btw. for example, this will happen when you're asking for a property to display that is not defined in swish:
~/swishtest > ./swish-e -w install -m 1 -p foo
# SWISH format: 2.1-dev-25
# Search words: install
err: Unknown Display property name "foo"
.
~/swishtest > ./swish-e -w install -m 1 -x 'Property foo=<foo>\n'
# SWISH format: 2.1-dev-25
# Search words: install
# Number of hits: 14
# Search time: 0.000 seconds
# Run time: 0.038 seconds
Property foo=(NULL)
.
to check that a property exists in your index you can run:
~/swishtest > ./swish-e -w not dkdk -T index_metanames | grep foo
foo : id=10 type=70 META_PROP:STRING(case:ignore) *presorted*
okay so in this case we can see that "foo" is really defined as a property. now let's make sure swish.cgi is asking for "foo" (sorry for the long lines, they're giving chaos but they're necessary):
~/swishtest > SWISH_DEBUG=command ./swish.cgi > /dev/null
Debug level set to: 3
Enter a query [all]:
Using 'not asdfghjklzxcv' to match all records
Enter max results to display [1]:
---- Running swish with the following command and parameters ----
./swish-e \
-w \
'swishdefault=(not asdfghjklzxcv)' \
-b \
1 \
-m \
1 \
-f \
index.swish-e \
-s \
swishrank \
desc \
swishlastmodified \
desc \
-x \
'<swishreccount>\t<swishtitle>\t<swishdescription>\t<swishlastmodified>\t<swishdocsize>\t<swishdocpath>\t<fos>\t<swishrank>\t<swishdocpath>\n' \
-H \
9
if you look carefully, you'll see that the -x parameter has "fos" instead of "foo" β so there's our problem, mystery solved. π
How do I use the SWISH::API perl module with swish.cgi?
use the "use_library" configuration directive:
use_library => 1,
this will only provide improved performance when running under mod_perl or other persistent environments. β‘
Why does the "Run time" differ when using the SWISH::API module
when you're using the SWISH::API module, the run (and search) times are calculated within the script. when you're using the swish-e binary, the swish-e program reports the times instead. the "Run time" may include the time required to load and compile the SWISH::API module. π
πΈ MOD_PERL πΈ
Ok so this script can also run under mod_perl (peep http://perl.apache.org). Bestie, this is a glow-up for your response time versus plain old CGI, because the swish.cgi script gets loaded into the Apache web server instead of spinning up fresh every single time. π
Heads up: you must have a mod_perl-enabled Apache server to run this script under mod_perl. No mod_perl, no slay.
Configuration is genuinely simple. In your httpd.conf or your startup.pl file you just load the script. Like, in httpd.conf you can use a perl section:
<perl>
use lib '/usr/local/apache/cgi-bin'; # location of the swish.cgi file
use lib '/home/yourname/swish-e/example/modules'; # modules required by swish.cgi
require "swish.cgi";
</perl>
Again bestie, the paths used will depend on where you installed the script and the modules. And here's the iconic part: when running under mod_perl, the swish.cgi script becomes a perl module, so it does not need to be installed in the cgi-bin directory. (But plot twist β you can actually use the same script as both a CGI script and a mod_perl module at the same time, read from the same location.) π
That loads the script into mod_perl. Then to configure the script to run, add this to your httpd.conf configuration file:
<location /search>
PerlSetVar Swish_Conf_File /home/yourname/swish-e/myconfig.pl
allow from all
SetHandler perl-script
PerlHandler SwishSearch
</location>
Note that you use the Swish_Conf_File setting in httpd.conf to tell the script which config file to use. Which meansβ¦ you can use the same script (and loaded modules) for different search sites running on the same Apache server. Just specify differnt config files for each Location and they can search different indexes and have a completely different look for each site, but all share the same code. Main character energy, honestly. β¨
Careful bestie: note that the config files are cached in theswish.cgiscript. Changes to the config file will require restarting the Apache server before they will be reloaded into theswish.cgiscript. This avoids callingstat()for every request.
Unlike CGI, mod_perl does not change the current directory to the location of the script, so your settings for the swish binary and the path to your index files must be absolute paths (or relative to the server root). π
Using the SWISH::API module with mod_perl will provide the most performance improvements. You can turn it on with the configuration setting use_library:
use_library => 1,
Without highlighting code enabled, using the SWISH::API module resulted in about 20 requests per second, where running the swish-e binary slowed the script down to about 8 requests per second. π
Note that the highlighting code is slow. For the best search performance, turn off highlighting. In your config file you can add:
highlighting => 0, # disable highlighting
and the script will show the first 500 chars of the description (or whatever you set for max_chars). Without highlight, one test was processing about 20 request per second. With the PhraseHighlight module that dropped to a little better than two requests per second, DefaultHighlight was about 2.3 request per second, and SimpleHighlight was about 6 request per second. π
Experiement with different highlighting options when testing performance. (I said experient, ok, it's literally spelled that way in the source, don't come for me. π)
Please post to the swish-e discussion list if you have any questions about running this script under mod_perl. π«Ά
Here's some general request/second on an Athlon XP 1800+ with 1/2GB RAM, Linux 2.4.20.
Highlighting Mode
None Phrase Default Simple
Using SWISH::API 45 1.5 2 12
----------------------------------------------------------------------------
Using swish-e 12 1.3 1.8 7.5
binary
As you can see, the highlighting code is a limiting factor. Periodt. π
π¦ SpeedyCGI π¦
SpeedyCGI (also called PersistentPerl) is another way to run Perl scripts persistently. SpeedyCGI is good if you do not have mod_perl available or do not have root access. SpeedyCGI works on Unix systems by loading the script into a "back end" process and keeping it in memory between requests. New requests are passed to the back end processes which avoids the startup time required by a Perl CGI script. Slay. β¨
Install SpeedyCGI from http://daemoninc.com/ (your OS may provide a packaged version of SpeedyCGI) and then change the first line of swish.cgi. For example, if the speedy binary is installed in /usr/bin/speedy, use the line:
#! /usr/bin/speedy -w -- -t60
The -w option is passed to Perl, and all options following the double-dash are SpeedyCGI options.
Note that when usingSpeedyCGI, configuration data is cached in memory. If you change theswish.cgiconfiguration file (.swishcgi.conf) thentouchthe mainswish.cgiscript to force reloading of configuration data.
π·οΈ Spidering π·οΈ
There are two ways to spider with swish-e. One uses the http input method that uses code that's part of swish. The other way is to use the new prog method along with a perl helper program called spider.pl. πΈοΈ
Here's an example of a configuration file for spidering with the http input method. You can see that the configuration is not much different than the file system input method. (But, don't use the http input method β use the -S prog method shown below.)
# Define what to index
IndexDir http://www.myserver.name/index.html
IndexOnly .html .htm
IndexContents HTML* .html .htm
DefaultContents HTML*
StoreDescription HTML* <body> 200000
MetaNames swishdocpath swishtitle
# Define http method specific settings -- see swish-e documentation
SpiderDirectory ../swish-e/src/
Delay 0
You index with the command:
swish-e -S http -c spider.conf
Note that this does take longer. For example, spidering the Apache documentation on a local web server with this method took over a minute, where indexing with the file system took less than two seconds. Using the prog method can speed this up. π¨
Here's an example configuration file for using the prog input method:
# Define the location of the spider helper program
IndexDir ../swish-e/prog-bin/spider.pl
# Tell the spider what to index.
SwishProgParameters default http://www.myserver.name/index.html
IndexContents HTML* .html .htm
DefaultContents HTML*
StoreDescription HTML* <body> 200000
MetaNames swishdocpath swishtitle
Then to index you use the command:
swish-e -c prog.conf -S prog -v 0
Spidering with this method took nine seconds. π
πΈ Stemmed Indexes πΈ
Many people enable a feature of swish called word stemming to provide "fuzzy" search options to their users. The stemming code does not actually find the "stem" of word, rather removes and/or replaces common endings on words. Stemming is far from perfect, and many words do not stem as you might expect. Plus, currently only English is supported. But, it can be a helpful tool for searching your site. You may wish to create both a stemmed and non-stemmed index, and provide a checkbox for selecting the index file. β
To enable a stemmed index you simply add to your configuration file:
UseStemming yes
If you want to use a stemmed index with this program and continue to highlight search terms you will need to install a perl module that will stem words. This section explains how to do this. π
The perl module is included with the swish-e distribution. It can be found in the examples directory (where you found this file) and called something like:
SWISH-Stemmer-0.05.tar.gz
The module should also be available on CPAN (http://search.cpan.org/).
Here's an example session for installing the module. (There will be quite a bit of output when running make.)
% gzip -dc SWISH-Stemmer-0.05.tar.gz |tar xof -
% cd SWISH-Stemmer-0.05
% perl Makefile.PL
or
% perl Makefile.PL PREFIX=$HOME/perl_lib
% make
% make test
(perhaps su root at this point if you did not use a PREFIX)
% make install
% cd ..
Use the PREFIX if you do not have root access or you want to install the modules in a local library. If you do use a PREFIX setting, add a use lib statement to the top of this swish.cgi program.
For example:
use lib qw(
/home/bmoseley/perl_lib/lib/site_perl/5.6.0
/home/bmoseley/perl_lib/lib/site_perl/5.6.0/i386-linux/
);
Once the stemmer module is installed, and you are using a stemmed index, the swish.cgi script will automatically detect this and use the stemmer module. β¨
π DISCLAIMER π
Please use this CGI script at your own risk. Fr fr.
This script has been tested and used without problem, but you should still be aware that any code running on your server represents a risk. If you have any concerns please carefully review the code. π«Ά
See http://www.w3.org/Security/Faq/www-security-faq.html
Security on Windows questionable. π
π«Ά SUPPORT π«Ά
The SWISH-E discussion list is the place to ask for any help regarding SWISH-E or this example script. See http://swish-e.org.
Before posting please review:
http://swish-e.org/2.2/docs/INSTALL.html#When_posting_please_provide_the_
Please do not contact the author or any of the swish-e developers directly. Bestie, the list is right there. π
π LICENSE π
swish.cgi $Revision: 1830 $ Copyright (C) 2001 Bill Moseley search@hank.org Example CGI program for searching with SWISH-E
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
β AUTHOR β
Bill Moseley
ok that's the whole page bestie, you made it, i'm so proud of you π₯Ήπ
This icon of a document comes from perl v5.42.3, dated 2026-08-19, and it lives at SWISH.CGI(7). Go index something cute. πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: swish-e 2.4.7-7.2build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms