swish.cgi -- Example Perl script for searching with the SWISH-E search engine.
Swish-e version 2.4.7Table of Contents
- DESCRIPTION
- REQUIREMENTS
- INSTALLATION
- CONFIGURATION
- DEBUGGING
-
Frequently Asked Questions
- How do I change the way the output looks?
- How do I use a templating system with swish.cgi?
- Why are there three different highlighting modules?
- My ISP doesn't provide access to the web server logs
- Why does the output show (NULL)?
- How do I use the SWISH::API perl module with swish.cgi?
- Why does the "Run time" differ when using the SWISH::API module
- MOD_PERL
- SpeedyCGI
- Spidering
- Stemmed Indexes
- DISCLAIMER
- SUPPORT
- LICENSE
- AUTHOR
DESCRIPTION
swish.cgi is a CGI script for searching with the SWISH-E search
engine version 2.1-dev and above. It returns results a page at a
time, with matching words from the source document highlighted,
showing a few words of content on either side of the highlighted word.
The script is highly configurable. Features include searching multiple (or selectable) indexes, limiting searches to a subset of documents, sorting by a number of different properties, and limiting results to a date range.
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. This can be overridden by the configure options --prefix or --libexecdir.
The standard configuration (i.e. not using a config file) should work with most swish index files. Customization of the parameters will be needed if you are indexing special meta data and want to search and/or display the meta data. The configuration can be modified by editing this script directly, or by using a configuration file (.swishcgi.conf by default). The script's configuration file is described below.
You are strongly encouraged to get the default configuration working before making changes. Most problems using this script are the result of configuration modifications.
The script is modular in design. Both the highlighting code and output generation is 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.
Included with the Swish-e distribution is a module to generate standard HTML output. 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 The HTML::Template and Template-Toolkit packages are not distributed with Swish-e. They are available from the CPAN (http://search.cpan.org).
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.
Please read the rest of the documentation. There's a DEBUGGING
section, and a FAQ section.
This script should work on Windows, but security may be an issue.
REQUIREMENTS
A reasonably current version of Perl. 5.00503 or above is recommended (anything older will not be supported).
The Date::Calc module is required to use the date range feature of the script. The Date::Calc module is also available from CPAN.
INSTALLATION
Here's an example installation session under Linux. It should be similar for other operating systems.
For the sake of simplicity in this installation example all files are placed in web server space, including files such as swish-e index and configuration files that would normally not be made available via the web server. Access to these files should be limited once the script is running. Either move the files to other locations (and adjust the script's configuration) or use features of the web server to limit access (such as with .htaccess).
Please get a simple installation working before modifying the configuration file. Most problems reported for using this script have been due to improper configuration.
The script's default settings are setup for initial testing. By default the settings expect to find most files and the swish-e binary in the same directory as the script.
For security reasons, once you have tested the script you will want to change settings to limit access to some of these files by the web server (either by moving them out of web space, or using access control such as .htaccess). An example of using .htaccess on Apache is given below.
It's expected that swish-e has already been unpacked and the swish-e binary has be compiled from source and "make install" has been run. If swish-e was installed from a vendor package (such as from a RPM or Debian package) see that pakage's documentation for where files are installed.
Example Installation:
- 1 Symlink or copy the swish.cgi.
Symlink (or copy if your platform or webserver does not allow symlinks) the swish.cgi script from the installation directory to a local directory. Typically, this would be the cgi-bin directory or a location where CGI script are located. In this example a new directory is created and the script is symlinked.
~$ mkdir swishdir ~$ cd swishdir ~/swishdir$ ln -s /usr/local/lib/swish-e/swish.cgiThe installation directory is set at configure time with the --prefix or --libexecdir options, but by default is in /usr/local/lib/swish-e.
- 2 Create an index
Use an editor and create a simple configuration file for indexing your files. In this example the Apache documentation is indexed. Last we run a simple query to test that the index works correctly.
~/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 do not have the Apache docs installed then pick another directory to index such as /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 that the index can be searched:
~/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 have in our directory now:
~/swishdir$ ls -1 index.swish-e index.swish-e.prop swish.cgi swish.conf - 3 Test the CGI script
This is a simple step, but often overlooked. You should test from the command line instead of jumping ahead and testing with the web server. See the
DEBUGGINGsection below for more information.~/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>The above shows that the script can be run directly, and generates a correct HTTP header and HTML.
If you run the above and see something like this:
~/swishdir >./swish.cgi bash: ./swish.cgi: No such file or directorythen you probably need to edit the script to point to the correct location of your perl program. Here's one way to find out where perl is located (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 are 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 (e.g. 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 may need to talk to your web server admin to get this working. Often files with the .cgi extension are automatically set up to run as CGI scripts, but 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 will only work if the web server is configured to follow symbolic links (the default for Apache).
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 the 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. It looks like Apache is not running the script and 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" prevents access to all files (such as config and index files), and only access is allowed to the swish.cgi script.
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 use your web browser to test.
Now, you may note that the links are not valid on the search results page. The swish config file contained the line:
ReplaceRules remove /usr/local/apache/
To make those links works (and assuming your web server will follow symbolic links):
~/swishtest$ ln -s /usr/local/apache/htdocs
BTW - "GET" used above is a program included with Perl's LWP library. If you do no have this you might try something like:
wget -O - http://localhost/swishdir/swish.cgi | head
and if nothing else, you can always 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 using a browser, but browsers are a poor tool for basic CGI debugging.
If you have problems check the DEBUGGING section below.
CONFIGURATION
If you want to change the location of the swish-e binary or the index file, use multiple indexes, add additional metanames and properties, change the default highlighting behavior, etc., you will need to adjust the script's configuration settings.
Again, please get a test setup working with the default parameters before making changes to any configuration settings. Better to debug one thing at a time...
In general, you will need to adjust the script's settings to match the index file you are searching. For example, if you are indexing a hypermail list archive you may want to make the script use metanames/properties of Subject, Author, and, Email address. Or you may wish to provide a way to limit searches to subsets of documents (e.g. parts of your directory tree).
To make things somewhat "simple", the configuration parameters are included near the top of the swish.cgi program. That is the only place that the individual parameters are defined and explained, so you will need to open up the swish.cgi script in an editor to view the options. Further questions about individual settings should be referred 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. The perl
hash structure may seem a bit confusing, 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.
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 so 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 was is to use a separate configuration file. In either case, the configuration settings are a basic perl hash reference.
Using a configuration file is described below, but 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).
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.
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 will may see a significant performance improvement when using the SWISH::API module. Under normal CGI usage you will probably not see any speed improvements.
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.
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.
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.
DEBUGGING
Most problems with using this script have been a result of improper configuration. Please get the script working with default settings before adjusting the configuration settings.
The key to debugging CGI scripts is to run them from the command line, not with a browser.
First, make sure the program compiles correctly:
$ perl -c swish.cgi
swish.cgi syntax OK
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
Now, you know that the program compiles and will run from the command line. Next, try accessing the script from a web browser.
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.)
The script does offer some basic debugging options that allow debugging from the command line. The debugging options are enabled by setting an environment variable "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:
$ 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]:
U