C4::Search - Functions for searching the Koha catalog.


See opac/opac-search.pl or catalogue/search.pl for example of usage


This module provides searching functions for Koha's bibliographic databases



($biblionumber,$biblionumber,$title) = FindDuplicate($record);

This function attempts to find duplicate records using a hard-coded, fairly simplistic algorithm


( $error, $results, $total_hits ) = SimpleSearch( $query, $offset, $max_results, [@servers], [%options] );

This function provides a simple search API on the bibliographic catalog

input arg:
    * $query can be a simple keyword or a complete CCL query
    * @servers is optional. Defaults to biblioserver as found in koha-conf.xml
    * $offset - If present, represents the number of records at the beginning to omit. Defaults to 0
    * $max_results - if present, determines the maximum number of records to fetch. undef is All. defaults to undef.
    * %options is optional. (e.g. "skip_normalize" allows you to skip changing : to = )
    Returns an array consisting of three elements
    * $error is undefined unless an error is detected
    * $results is a reference to an array of records.
    * $total_hits is the number of hits that would have been returned with no limit

    If an error is returned the two other return elements are undefined. If error itself is undefined
    the other two elements are always defined
usage in the script:

my ( $error, $marcresults, $total_hits ) = SimpleSearch($query);

if (defined $error) { $template->param(query_error => $error); warn "error: ".$error; output_html_with_http_headers $input, $cookie, $template->output; exit; }

my $hits = @{$marcresults}; my @results;

for my $r ( @{$marcresults} ) { my $marcrecord = MARC::File::USMARC::decode($r); my $biblio = TransformMarcToKoha($marcrecord,q{});

    #build the iarray of hashs for the template.
    push @results, {
        title           => $biblio->{'title'},
        subtitle        => $biblio->{'subtitle'},
        biblionumber    => $biblio->{'biblionumber'},
        author          => $biblio->{'author'},
        publishercode   => $biblio->{'publishercode'},
        publicationyear => $biblio->{'publicationyear'},




( undef, $results_hashref, \@facets_loop ) = getRecords (

        $koha_query,       $simple_query, $sort_by_ref,    $servers_ref,
        $results_per_page, $offset,       $branches,       $itemtypes,
        $query_type,       $scan,         $opac

The all singing, all dancing, multi-server, asynchronous, scanning, searching, record nabbing, facet-building

See verbose embedded documentation.


    C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );

Internal function that extracts facets information from a MARC::Record object and populates $facets_counter for using in getRecords.

$facets is expected to be filled with C4::Koha::getFacets output (i.e. the configured facets for Zebra).


    my $facets = _get_facets_from_zebra( $result_set )

Retrieves facets for a specified result set. It loops through the facets defined in C4::Koha::getFacets and returns a hash with the following structure:

   {  facet_idx => {
            facet_value => count


    my $facet_values =
        C4::Search::_get_facet_from_result_set( $facet_idx, $result_set, $sep )

Internal function that extracts facet information for a specific index ($facet_idx) and returns a hash containing facet values and count:

        $facet_value => $count ,

Warning: this function has the side effect of changing the elementSetName for the result set. It is a helper function for the main loop, which takes care of backing it up for restoring.


    my $facets_info = C4::Search::_get_facets_info( $facets )

Internal function that extracts facets information and properly builds the data structure needed to render facet labels.


Return an array with available indexes.


( $error, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc, $query_type ) = buildQuery ( $operators, $operands, $indexes, $limits, $sort_by, $scan, $lang);

Build queries and limits in CCL, CGI, Human, handle truncation, stemming, field weighting, fuzziness, etc.

See verbose embedded documentation.


  ($query, $query_cgi, $query_desc, $previous_operand) = _build_initial_query($initial_query_params);

  Build a section of the initial query containing indexes, operators, and operands.


  my @search_results = searchResults($search_context, $searchdesc, $hits, 
                                     $results_per_page, $offset, $scan, 

Format results in a form suitable for passing to the template


%hash = enabled_staff_search_views()

This function returns a hash that contains three flags obtained from the system preferences, used to determine whether a particular staff search results view is enabled.

Output arg:
    * $hash{can_view_MARC} is true only if the MARC view is enabled
    * $hash{can_view_ISBD} is true only if the ISBD view is enabled
    * $hash{can_view_labeledMARC} is true only if the Labeled MARC view is enabled
usage in the script:

$template->param ( C4::Search::enabled_staff_search_views );


$arrayref = z3950_search_args($matchpoints)

This function returns an array reference that contains the search parameters to be passed to the Z39.50 search script (z3950_search.pl). The array elements are hash refs whose keys are name and value, and whose values are the name of a search parameter, the value of that search parameter and the URL encoded value of that parameter.

The search parameter names are lccn, isbn, issn, title, author, dewey and subject.

The search parameter values are obtained from the bibliographic record whose data is in a hash reference in $matchpoints, as returned by Biblio::GetBiblioData().

If $matchpoints is a scalar, it is assumed to be an unnamed query descriptor, e.g. a general purpose search argument. In this case, the returned array contains only entry: the key is 'title' and the value is derived from $matchpoints.

If a search parameter value is undefined or empty, it is not included in the returned array.

The returned array reference may be passed directly to the template parameters.

Output arg:
    * $array containing hash refs as described above
usage in the script:

$data = Biblio::GetBiblioData($bibno); $template->param ( MYLOOP => C4::Search::z3950_search_args($data) )


$template->param ( MYLOOP => C4::Search::z3950_search_args($searchscalar) )


$field is a reference to the fields array


    _ZOOM_event_loop(\@zconns, \@results, sub {
        my ( $i, $size ) = @_;
    } );

Processes a ZOOM event loop and passes control to a closure for processing the results, and destroying the resultsets.


Given raw data from a searchengine result set, return a MARC::Record object

This helper function is needed to take into account all the involved system preferences and configuration variables to properly create the MARC::Record object.

If we are using GRS-1, then the raw data we get from Zebra should be USMARC data. If we are using DOM, then it has to be MARCXML.

If we are using elasticsearch, it'll already be a MARC::Record and this function needs a new name.


Koha Development Team <http://koha-community.org/>