Koha::SearchEngine::Elasticsearch::Search - search functions for Elasticsearch


    my $searcher =
      Koha::SearchEngine::Elasticsearch::Search->new( { index => $index } );
    my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new(
        { index => $index } );
    my $query = $builder->build_query('perl');
    my $results = $searcher->search($query);
    print "There were " . $results->total . " results.\n";
    $results->each(sub {
        push @hits, @_[0];



    my $results = $searcher->search($query, $page, $count, %options);

Run a search using the query. It'll return $count results, starting at page $page ($page counts from 1, anything less that, or undef becomes 1.) $count is also the number of entries on a page.

%options is a hash containing extra options:


If provided, this overrides the $page value, and specifies the record as an offset (i.e. the number of the record to start with), rather than a page.



    my $count = $searcher->count($query);

This mimics a search request, but just gets the result count instead. That's faster than pulling all the data in, usually.


    my ( $error, $results, $facets ) = $search->search_compat(
        $query,            $simple_query, \@sort_by,       \@servers,
        $results_per_page, $offset,       undef,           $item_types,
        $query_type,       $scan

A search interface somewhat compatible with C4::Search-getRecords>. Anything that is returned in the query created by build_query_compat will probably get ignored here, along with some other things (like @servers.)


    my ( $results, $total ) =
      $searcher->search_auth_compat( $query, $offset, $count, $skipmetadata, %options );

This has a similar calling convention to search, however it returns its results in a form the same as C4::AuthoritiesMarc::SearchAuthorities.


    my $count = $auth_searcher->count_auth_use($bib_searcher, $authid);

This runs a search to determine the number of records that reference the specified authid. $bib_searcher must be something compatible with elasticsearch, as the query is built in this function.


    my ( $error, $marcresults, $total_hits ) =
      $searcher->simple_search( $query, $offset, $max_results, %options );

This is a simpler interface to the searching, intended to be similar enough to C4::Search::SimpleSearch.



A thing to search for. It could be a simple string, or something constructed with the appropriate QueryBuilder module.


How many results to skip from the start of the results.


The max number of results to return. The default is 100 (because unlimited is a pretty terrible thing to do.)


These options are unused by Elasticsearch



if something went wrong, this'll contain some kind of error message.


an arrayref of MARC::Records (note that this is different from the C4::Search version which will return plain XML, but too bad.)


the total number of results that this search could have returned.


    my $biblionumber = $searcher->extract_biblionumber( $searchresult );

$searchresult comes from simple_search_compat.

Returns the biblionumber from the search result record.

decode_record_from_result my $marc_record = $self->decode_record_from_result(@result);

Extracts marc data from Elasticsearch result and decodes to MARC::Record object


Returns the maximum number of results that can be fetched

This directly requests Elasticsearch for the setting index.max_result_window (or the default value for this setting in case it is not set)


    my $koha_facets = _convert_facets($es_facets);

Converts elasticsearch facets types to the form that Koha expects. It expects the ES facet name to match the Koha type, for example itype, au, su-to, etc.


    my $result = $self->_aggregration_scan($query, 10, 0);

Perform an aggregation request for scan purposes.