Bug 12478 - more authority searching
authorRobin Sheat <robin@catalyst.net.nz>
Tue, 17 Feb 2015 05:38:07 +0000 (18:38 +1300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 26 Apr 2016 12:55:08 +0000 (09:55 -0300)
Queries are being built, but they seem to be wrong as no results are
returned.

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>

Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Koha/SearchEngine/Elasticsearch/Search.pm
opac/opac-authorities-home.pl

index 9f48467..ec63097 100644 (file)
@@ -264,8 +264,8 @@ sub build_authorities_query {
     my @query_parts;
     my @filter_parts;
     foreach my $s ( @{ $search->{searches} } ) {
-        my ($wh, $op, $val) = $s->{'where', 'operator', 'value'};
-        my ($q_type);
+        my ($wh, $op, $val) = @{ $s }{qw(where operator value)};
+        $wh = '_any' if $wh eq 'any';
         if ($op eq 'is' || $op eq '=') {
             # look for something that matches completely
             # note, '=' is about numerical vals. May need special handling.
@@ -299,7 +299,8 @@ sub build_authorities_query {
       $builder->build_authorities_query_compat( \@marclist, \@and_or,
         \@excluding, \@operator, \@value, $authtypecode, $orderby );
 
-This builds a query for searching for authorities.
+This builds a query for searching for authorities, in the style of
+L<C4::AuthoritiesMarc::SearchAuthorities>.
 
 Arguments:
 
@@ -364,7 +365,7 @@ sub build_authorities_query_compat {
         'match-heading' => 'Match-heading',
         'see-from'      => 'Match-heading-see-from',
         thesaurus       => 'Subject-heading-thesaurus',
-        ''              => '',
+        any              => '',
     );
 
     # Make sure everything exists
index a6ec304..87c2d73 100644 (file)
@@ -48,10 +48,11 @@ Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store ));
 
 =head2 search
 
-    my $results = $searcher->search($query, $page, $count);
+    my $results = $searcher->search($query, $page, $count, %options);
 
 Run a search using the query. It'll return C<$count> results, starting at page
 C<$page> (C<$page> counts from 1, anything less that, or C<undef> becomes 1.)
+C<$count> is also the number of entries on a page.
 
 C<%options> is a hash containing extra options:
 
@@ -64,6 +65,8 @@ an offset (i.e. the number of the record to start with), rather than a page.
 
 =back
 
+Returns
+
 =cut
 
 sub search {
@@ -71,8 +74,9 @@ sub search {
 
     my $params = $self->get_elasticsearch_params();
     my %paging;
+    # 20 is the default number of results per page
     $paging{limit} = $count || 20;
-    # ES doesn't want pages, it wants a record to start from.
+    # ES/Catmandu doesn't want pages, it wants a record to start from.
     if (exists $options{offset}) {
         $paging{start} = $options{offset};
     } else {
@@ -82,7 +86,7 @@ sub search {
     $self->store(
         Catmandu::Store::ElasticSearch->new(
             %$params,
-            trace_calls => 0,
+            trace_calls => 1,
         )
     );
     my $results = $self->store->bag->search( %$query, %paging );
@@ -132,6 +136,32 @@ sub search_compat {
     return (undef, \%result, $self->_convert_facets($results->{facets}));
 }
 
+=head2 search_marc
+
+    my ( $results, $total ) =
+      $searcher->search_marc( $query, $page, $count, %options );
+
+This has a similar calling convention to L<search>, however it assumes that all
+the results are going to contain MARC, and just provides an arrayref of them,
+along with a count of the total number of results.
+
+=cut
+
+sub search_marc {
+    # TODO this probably should be temporary, until something more
+    # comprehensive is implemented using Koha::RecordProcessor and such.
+    my $self = shift;
+
+    my $res = $self->search(@_);
+    my @records;
+    $res->each(sub {
+            my $marc_json = @_[0]->{record};
+            my $marc = $self->json2marc($marc_json);
+            push @records, $marc;
+        });
+    return (\@records, $res->total);
+}
+
 =head2 json2marc
 
     my $marc = $self->json2marc($marc_json);
index d37df85..a99bc47 100755 (executable)
@@ -32,6 +32,8 @@ use C4::Koha;
 use C4::Search::History;
 
 use Koha::Authority::Types;
+use Koha::SearchEngine::Search;
+use Koha::SearchEngine::QueryBuilder;
 
 my $query        = new CGI;
 my $op           = $query->param('op') || '';
@@ -58,10 +60,16 @@ if ( $op eq "do_search" ) {
     $resultsperpage = $query->param('resultsperpage');
     $resultsperpage = 20 if ( !defined $resultsperpage );
     my @tags;
-    my ( $results, $total, @fields ) =
-      SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
-        \@value, $startfrom * $resultsperpage,
-        $resultsperpage, $authtypecode, $orderby );
+    my $builder  = Koha::SearchEngine::QueryBuilder->new();
+    my $searcher = Koha::SearchEngine::Search->new({index => 'authorities'});
+    my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
+        \@excluding, \@operator, \@value, $authtypecode, $orderby );
+#    use Data::Dumper;
+#    die Dumper(\@marclist, \@and_or,
+#        \@excluding, \@operator, \@value, $authtypecode, $orderby, $query);
+    my ( $results, $total ) =
+      $searcher->search_marc( $search_query, $startfrom, $resultsperpage );
+
     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         {
             template_name   => "opac-authoritiessearchresultlist.tt",
@@ -116,8 +124,9 @@ if ( $op eq "do_search" ) {
         $to = ( ( $startfrom + 1 ) * $resultsperpage );
     }
     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
-        my @usedauths = grep { $_->{used} > 0 } @$results;
-        $results = \@usedauths;
+#        TODO implement usage counts
+#        my @usedauths = grep { $_->{used} > 0 } @$results;
+#        $results = \@usedauths;
     }
 
     # Opac search history
@@ -151,7 +160,6 @@ if ( $op eq "do_search" ) {
     }
 
     $template->param( result => $results ) if $results;
-    $template->param( FIELDS => \@fields );
     $template->param( orderby => $orderby );
     $template->param(
         startfrom      => $startfrom,