NAME
CGI::Application::Search - Base class for CGI::App Swish-e site engines
SYNOPSIS
package My::Search;
use base 'CGI::Application::Search';
sub cgiapp_init {
my $self = shift;
$self->param('SWISHE-INDEX' =>'my-swishe.index');
}
#let the user turn context highlighting off
sub cgiapp_prerun {
my $self = shift;
$self->param('HIGHLIGHT_CONTEXT' => 0)
if($self->query->param('highlight_off'));
}
1;
DESCRIPTION
A CGI::Application based control module that uses Swish-e API in perl (http://swish-e.org) to to perform searches on a swish-e index of documents. It uses HTML::Template to display the search form and the results. It uses two templates ('search_results.tmpl' and 'results_header.tmpl') and any customization of the look and feel should happen in the templates. For the majority of the cases you should simply be able to adjust the style section found in the 'search_results.tmpl' template.
This controller has two run modes. The start_mode is show_search.
show_search
This run mode will show the simple search form. If there are any results they will also be displayed. This is the default run mode and after a search is performed, this run mode is called to display the results.
perform_search
This run mode will actually use the SWISH::API module to perform the search on a given index. If the HIGHLIGHT_CONTEXT option is set is will then use Text::Context to obtain a suitable context of the search content for each result returned and highlight the text according to the HIGHLIGHT_START and HIGHLIGHT_STOP options.
METHODS
Most of the time you will not need to call the methods that are implemented in this module. But in cases where more customization is required than can be done in the templates, it might be prudent to override or extend these methods in your derived class.
generate_search_query($keywords)
This method is used to generate the query for swish-e from the $keywords (by default the 'keywords' CGI parameter), as well as any EXTRA_PROPERTIES that are present.
If you wish to generate your own search query then you should override this method. This is common if you need to have access/authorization control that will need to be taken into account for your search. (eg, anything under /protected can't be seen by someone not logged in).
Please see the swish-e documentation on the exact syntax for the query.
setup()
This method set's up our application with two run modes and sets the show_search method as the default run mode. It also sets the defaults for several internal parameters that this module uses. These parameters can be reset at any time (in your cgiapp_init or cgiapp_prerun, or PARAMS hash in new()).
Here is a list of these parameters, what each does, and what the default value is
SWISHE_INDEX
This is the swishe index used for the searches. The default is 'data/swish-e.index'. You will probably override this every time.
PER_PAGE
How many search result items to display per page. The default is 10.
HIGHLIGHT_CONTEXT
Boolean indicating whether or not we should highlight the context. The default is true.
HIGHLIGHT_START
The text to be prepended to a word being highlighted. If this value is false and HIGHTLIGHT_CONTEXT is true then it will use the default provided by Text::Context. The default text is
<lt
strong<gt>>.HIGHLIGHT_STOP
The text to be appended to a word being highlighted. If this value is false and HIGHTLIGHT_CONTEXT is true then it will use the default provided by Text::Context. The default text is
<lt
/strong<gt>>.EXTRA_PROPERTIES
This is an array ref of extra properties used in the search. By default, the module will only use the value of the 'keywords' parameter coming in the CGI query. If anything is provided as an extra property then it will be added to the query used in the search.
An example: You have some of you pages designated into categories. You want the user to have the option of narrowing his results by category. You add the word 'category' to the 'EXTRA_PROPERTIES' list and then you add a 'category' form element that the user has the option of giving a value to your search form. If the user gives that element a value, then it will be seen and applied to the search. This will also only work if you have the 'category' element defined for your documents (see "SWISH-E Configuration" and 'MetaNames' in the swish-e.org SWISH-CONF documentation).
The default is an empty list.
CONTEXT_LENGTH
This is the maximum length for the context (in chars) that is displayed for each search result. The default is 250 characters.
START_MODE
This contains the name of a run mode method that will be used as the start method instead of show_search. If you want to change this parameter it must be done prior to running the 'setup()' sub (in PARAMS hash to new() or in cgiapp_init()).
RUN MODES
show_search()
This method will load the 'Search/search_results.tmpl' HTML::Template and display it to the user. It will 'associate' this template with $self so that any parameters in $self->param() are also accessible to the template. It will also use HTML::FillInForm to fill in the search form with the previously selected parameters.
perform_search()
This is where the meat of the searching is performed. We create a SWISH::API object on the SWISHE_INDEX and create the query for the search based on the value of the 'keywords' parameter in CGI and any other EXTRA_PARAMETERS. The search is executed and if HIGHLIGHT_CONTEXT is true we will use Text::Context to highlight it and then format the results data only showing PER_PAGE number of elements per page (if PER_PAGE is true). We will also show a list of pages that can be selected for navigating through the results. Then we will return to the show_search() method for displaying.
TEMPLATES
Two templates are provided as examples of how to use this module. Please feel free to copy and change them in what ever way you see fit. To help in giving you more information to display (or not display, depending on your preference) the following variables are available for your templates:
Global Tmpl Vars
These variables are available throughout the templates and contain information related to the search as a whole:
searched
A boolean indicating whether or not a search was performed.
keywords
The exact string that was returned to the server from the input named 'keywords'
elapsed_time
A string representing the number of seconds that the search took. This will be a floating point number with a precision of 3.
hits
This is the TMPL_LOOP that contains the actuall results from the search.
pages
This is the TMPL_LOOP that contains paging information for the results
first_page
This is a boolean indicating whether or not this page of the results is the first or not.
last_page
This is a boolean indicating whether or not this page of the results is the last or not.
start_num
This is the number of the first result on the current page
stop_num
This is the number of the last result on the current page
total_entries
The total number of results in their search, not the total number shown on the page.
HITS TMPL_LOOP Vars
These variables are available only inside of the TMPL_LOOP named "HITS".
hit_reccount
The
swishreccount
property of the results as indexed by SWISH-Ehit_rank
The rank to the result as given by SWISH-E (the
swishrank
property)hit_title
The
swishtitle
property of the results as indexed by SWISH-Ehit_path
The
swishdocpath
property of the results as indexed by SWISH-Ehit_size
The
swishdocsize
property of the results as indexed by SWISH-E and then formatted with Number::Format::format_byteshit_description
The
swishdescription
property of the results as indexed by SWISH-E. If HIGHLIGHT_CONTEXT is true, then this description will also have search terms highlighted and will only be, at most, CONTEXT_LENGTH characters long.hit_last_modified
The
swishlastmodified
property of the results as indexed by SWISH-E and then formatted using Time::Piece::strftime with a format string of%B %d, %Y
.
PAGES TMPL_LOOP Vars
OTHER NOTES
If at any time prior to the execution of the 'perform_search' run mode you set the $self-<gt
param('results')> parameter a search will not be performed, but rather and empty set of results is returned. This is helpful when you decide in either cgiapp_init or cgiapp_prerun that this user does not have permissions to perform the desired search.
AUTHOR
Michael Peters <mpeters@plusthree.com>
Thanks to Plus Three, LP (http://www.plusthree.com) for sponsoring my work on this module.