search - a search script for finding records in a Koha system (Version 3)


This script utilizes a new search API for Koha 3. It is designed to be simple to use and configure, yet capable of performing feats like stemming, field weighting, relevance ranking, support for multiple query language formats (CCL, CQL, PQF), full support for the bib1 attribute set, extended attribute sets defined in Zebra profiles, access to the full range of Z39.50 and SRU query options, federated searches on Z39.50/SRU targets, etc.

The API as represented in this script is mostly sound, even if the individual functions in Search.pm and Koha.pm need to be cleaned up. Of course, you are free to disagree :-)

I will attempt to describe what is happening at each part of this script. -- Joshua Ferraro <jmf AT liblime DOT com>


This script performs two functions:

1. interacts with Koha to retrieve and display the results of a search
2. loads the advanced search page

These two functions share many of the same variables and modules, so the first task is to load what they have in common and determine which template to use. Once determined, proceed to only load the variables and procedures necessary for that function.


This is fairly straightforward, and I won't go into detail ;-)


If we're performing a search, this script performs three primary operations:

1. builds query strings (yes, plural)
2. perform the search and return the results array
3. build the HTML for output to the template

There are several additional secondary functions performed that I will not cover in detail.

1. Building Query Strings

There are several types of queries needed in the process of search and retrieve:

1 $query - the fully-built query passed to zebra

This is the most complex query that needs to be built. The original design goal was to use a custom CCL2PQF query parser to translate an incoming CCL query into a multi-leaf query to pass to Zebra. It needs to be multi-leaf to allow field weighting, koha-specific relevance ranking, and stemming. When I have a chance I'll try to flesh out this section to better explain.

This query incorporates query profiles that aren't compatible with most non-Zebra Z39.50 targets to accomplish the field weighting and relevance ranking.

2 $simple_query - a simple query that doesn't contain the field weighting, stemming, etc., suitable to pass off to other search targets

This query is just the user's query expressed in CCL CQL, or PQF for passing to a non-zebra Z39.50 target (one that doesn't support the extended profile that Zebra does).

3 $query_cgi - passed to the template / saved for future refinements of the query (by user)

This is a simple string that completely expresses the query as a CGI string that can be used for future refinements of the query or as a part of a history feature.

4 $query_desc - Human search description - what the user sees in search feedback area

This is a simple string that is human readable. It will contain '=', ',', etc.

2. Perform the Search

This section takes the query strings and performs searches on the named servers, including the Koha Zebra server, stores the results in a deeply nested object, builds 'faceted results', and returns these objects.

3. Build HTML

The final major section of this script takes the objects collected thusfar and builds the HTML for output to the template and user.

Additional Notes

Not yet completed...


    my $type = C4::Context->preference("AdvancedSearchTypes") || "itemtypes";
    my @advanced_search_types = prepare_adv_search_types($type);

Different types can be searched for in the advanced search. This takes the system preference that defines these types and parses it into an arrayref for the template.

"itemtypes" is handled specially, as itemtypes aren't an authorised value. It also accounts for the "item-level_itypes" system preference.