From 0002bbc72a42b4c4223e20c66ef032c20f48ba19 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 17 Feb 2015 18:38:07 +1300 Subject: [PATCH] Bug 12478 - more authority searching Queries are being built, but they seem to be wrong as no results are returned. 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 --- .../Elasticsearch/QueryBuilder.pm | 9 ++--- Koha/SearchEngine/Elasticsearch/Search.pm | 36 +++++++++++++++++-- opac/opac-authorities-home.pl | 22 ++++++++---- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 9f4846746d..ec6309782e 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -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. 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 diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index a6ec304162..87c2d73a83 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -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 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, 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); diff --git a/opac/opac-authorities-home.pl b/opac/opac-authorities-home.pl index d37df857e1..a99bc4742e 100755 --- a/opac/opac-authorities-home.pl +++ b/opac/opac-authorities-home.pl @@ -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, -- 2.39.5