Skip to main content.
home | support | download

Back to List Archive

Re: SWISH-E FORM.

From: Bill Moseley <moseley(at)not-real.hank.org>
Date: Wed Nov 05 2003 - 19:35:42 GMT
On Wed, Nov 05, 2003 at 05:43:04AM -0800, Trent Renshaw wrote:
> Hi All,
> 
> I remember using an older version of SWISH-E this year before May of this
> year.
> I believe it was the latest at the time and I am sure it came with various
> example
> HTML implementations aswell as configuration files.
> 
> The documentation does not seem to cover how-to get SWISH-E into a HTML
> page after you have configured and indexed your files.

Yes, instructions on how to write a CGI script are beyond the scope of 
swish-e. 

> 
> There was an example which I believe was quite "basic". It would call
> swish.cgi
> (or some other Perl file) when clicking submit and the display results where
> quite simple to customise. The CGI file was not more than 30 lines or so
> (from
> what I remember). I don't know, this latest version seems really confusing.

Well, there is swish.cgi, but it was never 30 lines.  It's documented at

  http://swish-e.org/current/docs/

Swish.cgi is confusing because it's a bit general purpose with lots of 
options to use (or not use, as may be the case), not to mention
there's docs to read.  You can help by explaining what you find
confusing, though.

> Help,

Sure, but you have to be clear what you want help with.  Maybe someone 
on the list has a shorter cgi script you can use.   What language do you 
want to work with?  If Perl, use the SWISH::API module to search your 
index and pick a templating module to generate output (I'd pick 
Template::Toolkit).  But that will require a little bit of work to learn 
those modules.

So did that 30 lines include white space and comments?  

Heck, ok how about 60 lines (not including attached template)?

(untested -- just an example -- use at your own risk -- ALL CAPS DISCLAIMER)

#!/usr/local/bin/perl -w
use strict;
use CGI;
use SWISH::API;
use Template;


use vars qw/$template $swish/;  # for caching under speedycgi/mod_perl  

process_request();

sub process_request {
    my $cgi = CGI->new;
    my $query = $cgi->param('query');
    my %params = (
        title => 'Company Name',
        index => 'index.swish-e',
        cgi   => $cgi,
    );

    if ( $query ) {
        my $start = $cgi->param('page') || 1;
        my $pagesize = 15;
        $params{search} = run_query( $query, $start, $pagesize, $params{index} );
    }


    $template ||= Template->new( INCLUDE_PATH => "$ENV{HOME}/apache" );
    die $template->error unless $template;
    print $cgi->header;
    $template->process( \*DATA, \%params ) || die $template->error;
}

sub run_query {
    my ($query, $page, $pagesize, $index) = @_;

    $page||= 1;
    $page = 1 unless $page =~ /^\d+$/;

    $pagesize ||= 15;
    $pagesize = 15 unless $pagesize =~ /^\d+$/ && $pagesize > 0 && $pagesize < 50; 

    $swish ||= SWISH::API->new( $index );
    die "Failed to create SWISH::API object" unless $swish;
    $swish->AbortLastError if $swish->Error;

    my $results = $swish->Query( $query );

    if ( $swish->Error ) {
        $swish->AbortLastError if $swish->CriticalError;
        return {
            query   => $query,
            message => join( ' ', $swish->ErrorString, $swish->LastErrorMsg ),
        };
    }


    $results->SeekResult( ($page-1) * $pagesize );

    my @records;
    my $result;
    my $cnt = $pagesize;
    while ( $cnt-- && ($result = $results->NextResult) ) {
        push @records, $result;
    }

    my %query = (
        query   => $query,
        results => \@records,
        hits    => $results->Hits,
        shown   => scalar @records,
        page    => $page,
        start   => ($page-1) * $pagesize,
    );

    $query{prev} = $page-1 if $page > 1;
    $query{next} = $page+1 if $query{start} + $pagesize < $query{hits}-1;

    return \%query;
}



__END__


<html>
<head><title>Search Documents</title></head>
<body>
    [% PROCESS form %]
    <p>
    [% IF search %]
        [% IF search.message; '<h2 align="center">'; search.message; "</h2>"; END %]

        [% IF search.shown %]
            [% PROCESS results_header %]
            [% PROCESS display_results %]
        [% END %]
    [% END %]
</body>



[% BLOCK form %]
    [% cgi.start_form( '-action' => cgi.script_name, '-method' => 'GET' ) %]
        [% cgi.textfield( {
            name    => 'query',
            size    => 40,
            maxlength => 200,
            } ) %]

        [% cgi.submit('submit','Search!') %]
    [% cgi.end_form.join('') %]

[% END %]

[% BLOCK results_header %]
<p>Showing page [% search.page %] 
([% search.shown %] of [% search.hits %] hits) 
for <b>[% search.query | html %]</b><br>

[% USE myurl = url( cgi.script_name, query=search.query ) %]

[% IF search.prev %]
   <a href="[% myurl( page=search.prev ) %]">Previous Page</a> 
[% END %]
[% IF search.next %]
   <a href="[% myurl( page=search.next ) %]">Next Page</a> 
[% END %]
<p>
[% END %]


[% BLOCK display_results %]
    [% USE date %]
    [% FOREACH item = search.results %]
        <dl>
            <dt>
                <a href="[% item.Property('swishdocpath') | uri | html %]">
                [% ( item.Property('swishtitle') || item.Property('swishdocpath') )  %]
                </a>

                <small>-- rank: <b>[% item.Property('swishrank') %]</b></small>
            </dt>

            <dd>
                [% item.Property('swishdescription') || "No description" | html %]
                <br>
                <small>
                    Path: [% item.Property('swishdocpath') | html %]
                    Size: [% item.Property('swishdocsize') %] bytes
                    Date: [% date.format( item.Property('swishlastmodified')) %]
                </small>
            </dd>
        </dl>
    [% END %]
[% END %]




-- 
Bill Moseley
moseley@hank.org
Received on Wed Nov 5 19:48:57 2003