Bug 12478: a replacement to the SimpleSearch interface implemented
authorRobin Sheat <robin@catalyst.net.nz>
Fri, 19 Jun 2015 03:55:46 +0000 (15:55 +1200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 26 Apr 2016 12:57:43 +0000 (09:57 -0300)
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Jesse Weaver <jweaver@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

C4/Search.pm
Koha/ElasticSearch/Indexer.pm
Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Koha/SearchEngine/Elasticsearch/Search.pm
cataloguing/addbooks.pl

index d18b121..1e51427 100644 (file)
@@ -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' ) {
index b7d6097..a8e8280 100644 (file)
@@ -82,6 +82,23 @@ sub update_index {
     return 1;
 }
 
+=head2 $indexer->update_index_background($biblionums, $records)
+
+This has exactly the same API as C<update_index_background> 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<update_index>
index 93c4018..c89f3e6 100644 (file)
@@ -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
index e76ab3e..4218544 100644 (file)
@@ -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<C4::Search::SimpleSearch>.
+
+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<C4::Search> 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
 
index 37ac83a..45f7d2c 100755 (executable)
@@ -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 );