From 1c737eaaf8847ac01eb281c37840584e18f4a03b Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Fri, 19 Jun 2015 15:55:46 +1200 Subject: [PATCH] Bug 12478: a replacement to the SimpleSearch interface implemented Signed-off-by: Nick Clemens Signed-off-by: Jesse Weaver Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher --- C4/Search.pm | 11 +-- Koha/ElasticSearch/Indexer.pm | 17 +++++ .../Elasticsearch/QueryBuilder.pm | 2 +- Koha/SearchEngine/Elasticsearch/Search.pm | 73 +++++++++++++++++++ cataloguing/addbooks.pl | 11 ++- 5 files changed, 104 insertions(+), 10 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index d18b121811..1e5142786c 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -2536,7 +2536,7 @@ sub _ZOOM_event_loop { } } -=head2 new_record_from_searchengine +=head2 new_record_from_zebra Given raw data from a searchengine result set, return a MARC::Record object @@ -2547,7 +2547,8 @@ 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. +If we are using elasticsearch, it'll already be a MARC::Record and this +function needs a new name. =cut @@ -2556,13 +2557,13 @@ sub new_record_from_zebra { my $server = shift; my $raw_data = shift; # Set the default indexing modes - my $index_mode = ( $server eq 'biblioserver' ) - ? C4::Context->config('zebra_bib_index_mode') // 'dom' - : C4::Context->config('zebra_auth_index_mode') // 'dom'; my $search_engine = C4::Context->preference("SearchEngine"); if ($search_engine eq 'Elasticsearch') { return $raw_data; } + my $index_mode = ( $server eq 'biblioserver' ) + ? C4::Context->config('zebra_bib_index_mode') // 'dom' + : C4::Context->config('zebra_auth_index_mode') // 'dom'; my $marc_record = eval { if ( $index_mode eq 'dom' ) { diff --git a/Koha/ElasticSearch/Indexer.pm b/Koha/ElasticSearch/Indexer.pm index b7d609706a..a8e8280413 100644 --- a/Koha/ElasticSearch/Indexer.pm +++ b/Koha/ElasticSearch/Indexer.pm @@ -82,6 +82,23 @@ sub update_index { return 1; } +=head2 $indexer->update_index_background($biblionums, $records) + +This has exactly the same API as C however it'll +return immediately. It'll start a background process that does the adding. + +If it fails to add to Elasticsearch then it'll add to a queue that will cause +it to be updated by a regular index cron job in the future. + +# TODO implement in the future - I don't know the best way of doing this yet. +# If fork: make sure process group is changed so apache doesn't wait for us. + +=cut + +sub update_index_background { + $self->update_index(@_); +} + =head2 $indexer->delete_index(); Deletes the index from the elasticsearch server. Calling C diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 93c40185ef..c89f3e67e5 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -218,7 +218,7 @@ sub build_query_compat { my $limit = $self->_join_queries( $self->_convert_index_strings(@$limits)); my $limit_cgi = '&limit=' . join( '&limit=', map { uri_escape($_) } @$orig_limits ); - my $limit_desc = "$limit"; + my $limit_desc = "$limit" if $limit; return ( undef, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc, undef, undef diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index e76ab3ea0d..4218544023 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -253,7 +253,80 @@ sub count_auth_use { $bib_searcher->count($query); } +=head2 simple_search_compat + my ( $error, $marcresults, $total_hits ) = + $searcher->simple_search( $query, $offset, $max_results ); + +This is a simpler interface to the searching, intended to be similar enough to +L. + +Arguments: + +=over 4 + +=item C<$query> + +A thing to search for. It could be a simple string, or something constructed +with the appropriate QueryBuilder module. + +=item C<$offset> + +How many results to skip from the start of the results. + +=item C<$max_results> + +The max number of results to return. The default is 1,000 (because unlimited +is a pretty terrible thing to do.) + +=back + +Returns: + +=over 4 + +=item C<$error> + +if something went wrong, this'll contain some kind of error +message. + +=item C<$marcresults> + +an arrayref of MARC::Records (note that this is different from the +L version which will return plain XML, but too bad.) + +=item C<$total_hits> + +the total number of results that this search could have returned. + +=back + +=cut + +sub simple_search_compat { + my ($self, $query, $offset, $max_results) = @_; + + return ('No query entered', undef, undef) unless $query; + + my %options; + $options{offset} = $offset // 0; + $max_results //= 100; + + unless (ref $query) { + # We'll push it through the query builder + my $qb = Koha::SearchEngine::QueryBuilder->new(); + $query = $qb->build_query($query); + } + my $results = $self->search($query, undef, $max_results, %options); + my @records; + $results->each(sub { + # The results come in an array for some reason + my $marc_json = @_[0]->{record}; + my $marc = $self->json2marc($marc_json); + push @records, $marc; + }); + return (undef, \@records, $results->total); +} =head2 json2marc diff --git a/cataloguing/addbooks.pl b/cataloguing/addbooks.pl index 37ac83a632..45f7d2c827 100755 --- a/cataloguing/addbooks.pl +++ b/cataloguing/addbooks.pl @@ -34,6 +34,9 @@ use C4::Output; use C4::Koha; use C4::Search; +use Koha::SearchEngine::Search; +use Koha::SearchEngine::QueryBuilder; + my $input = new CGI; my $success = $input->param('biblioitem'); @@ -74,15 +77,15 @@ if ($query) { my $QParser; $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser')); my $builtquery; + my $builder = Koha::SearchEngine::QueryBuilder->new(); + my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'}); if ($QParser) { $builtquery = $query; } else { - my ( $builterror,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type); - ( $builterror,$builtquery,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) = buildQuery(undef,\@operands); + ( undef,$builtquery,undef,undef,undef,undef,undef,undef,undef,undef) = $builder->build_query_compat(undef,\@operands); } - # find results - my ( $error, $marcresults, $total_hits ) = SimpleSearch($builtquery, $results_per_page * ($page - 1), $results_per_page); + my ( $error, $marcresults, $total_hits ) = $searcher->simple_search_compat($builtquery, $results_per_page * ($page - 1), $results_per_page); if ( defined $error ) { $template->param( error => $error ); -- 2.20.1