From 05fa9bccb0f2e82f52efba01b2435c723184643b Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 3 Feb 2015 14:40:50 +1300 Subject: [PATCH] Bug 12478 - fix syntax errors so that stuff runs 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 | 35 ++++++++--- Koha/SearchEngine/Elasticsearch/Search.pm | 4 +- Koha/SearchEngine/QueryBuilder.pm | 1 + Koha/SearchEngine/Search.pm | 58 +++++++++++++++++++ opac/opac-search.pl | 20 +------ 5 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 Koha/SearchEngine/Search.pm diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 253cf567c2..148f5fb824 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -40,6 +40,7 @@ provides something that can be given to elasticsearch to get answers. =cut use base qw(Class::Accessor); +use Carp; use List::MoreUtils qw/ each_array /; use Modern::Perl; use URI::Escape; @@ -225,7 +226,7 @@ sub build_query_compat { ); } -=head2 build_authorites_query +=head2 build_authorities_query my $query = $builder->build_authorities_query(\%search); @@ -259,6 +260,27 @@ The search description is a hashref that looks something like: sub build_authorities_query { my ($self, $search) = @_; + # Start by making the query parts + my @query_parts; + my @filter_parts; + foreach my $s ( @{ $search->{searches} } ) { + my ($wh, $op, $val) = $s->{'where', 'operator', 'value'}; + my ($q_type); + if ($op eq 'is' || $op eq '=') { + # look for something that matches completely + # note, '=' is about numerical vals. May need special handling. + push @filter_parts, { filter => { term => { $wh => $val }} }; + } elsif ($op eq 'exact') { + # left and right truncation, otherwise an exact phrase + push @query_parts, { match_phrase => { $wh => $val }}; + } else { + # regular wordlist stuff + # TODO truncation + push @query_parts, { match => { $wh => $val }}; + } + } + # Merge the query and filter parts appropriately + } @@ -291,8 +313,8 @@ Also ignored. =item operator What form of search to do. Options are: is (phrase, no trunction, whole field -must match), = (number exact match), exact (same as 'is'?). If left blank, -then word list, right truncted, anywhere is used. +must match), = (number exact match), exact (phrase, but with left and right +truncation). If left blank, then word list, right truncted, anywhere is used. =item value @@ -338,8 +360,7 @@ sub build_authorities_query_compat { # Make sure everything exists foreach my $m (@$marclist) { - confess "Invalid marclist field provided: $m" - unless exists $koha_to_index_name{$m}; + confess "Invalid marclist field provided: $m" unless exists $koha_to_index_name{$m}; } for ( my $i = 0 ; $i < @$value ; $i++ ) { push @searches, @@ -356,10 +377,10 @@ sub build_authorities_query_compat { : ( $orderby =~ /^Auth/ ) ? 'Local-Number' : undef; if ($sort_field) { - $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc'; + my $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc'; %sort = ( $orderby => $sort_order, ); } - %search = ( + my %search = ( searches => \@searches, authtypecode => $authtypecode, ); diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index 57a03f11d2..a6ec304162 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -1,4 +1,4 @@ -package Koha::SearchEngine::ElasticSearch::Search; +package Koha::SearchEngine::Elasticsearch::Search; # Copyright 2014 Catalyst IT # @@ -44,7 +44,7 @@ use Catmandu::Store::ElasticSearch; use Data::Dumper; #TODO remove use Carp qw(cluck); -Koha::SearchEngine::ElasticSearch::Search->mk_accessors(qw( store )); +Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store )); =head2 search diff --git a/Koha/SearchEngine/QueryBuilder.pm b/Koha/SearchEngine/QueryBuilder.pm index 1f93e53984..aaaba011af 100644 --- a/Koha/SearchEngine/QueryBuilder.pm +++ b/Koha/SearchEngine/QueryBuilder.pm @@ -42,6 +42,7 @@ and just get whatever querybuilder you need. Creates a new C of whatever the relevant type is. =cut + use C4::Context; use Modern::Perl; diff --git a/Koha/SearchEngine/Search.pm b/Koha/SearchEngine/Search.pm new file mode 100644 index 0000000000..c465f8c2da --- /dev/null +++ b/Koha/SearchEngine/Search.pm @@ -0,0 +1,58 @@ +package Koha::SearchEngine::Search; + +# This file is part of Koha. +# +# Copyright 2015 Catalyst IT +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +# This is a shim that gives you the appropriate search object for your +# system preference. + +=head1 NAME + +Koha::SearchEngine::Search - instantiate the search object that corresponds to +the C system preference. + +=head1 DESCRIPTION + +This allows you to be agnostic about what the search engine configuration is +and just get whatever search object you need. + +=head1 SYNOPSIS + + use Koha::SearchEngine::Search; + my $searcher = Koha::SearchEngine::Search->new(); + +=head1 METHODS + +=head2 new + +Creates a new C of whatever the relevant type is. + +=cut + +use C4::Context; +use Modern::Perl; + +sub new { + my $engine = C4::Context->preference("SearchEngine"); + my $file = "Koha/SearchEngine/${engine}/Search.pm"; + my $class = "Koha::SearchEngine::${engine}::Search"; + require $file; + shift @_; + return $class->new(@_); +} + +1; diff --git a/opac/opac-search.pl b/opac/opac-search.pl index a75ba7f7a7..8529473b2e 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -32,28 +32,14 @@ use List::MoreUtils q/any/; use Data::Dumper; # TODO remove -use Koha::ElasticSearch::Search; +use Koha::SearchEngine::Search; use Koha::SearchEngine::QueryBuilder; -use Koha::SearchEngine::Zebra::Search; my $searchengine = C4::Context->preference("SearchEngine"); my ($builder, $searcher); #$searchengine = 'Zebra'; # XXX -$builder = Koha::SearchEngine::QueryBuilder->new(); -for ( $searchengine ) { - when ( /^Solr$/ ) { - warn "We use Solr"; - require 'opac/search.pl'; - exit; - } - when ( /^Zebra$/ ) { - $searcher=Koha::SearchEngine::Zebra::Search->new(); - } - when (/^Elasticsearch$/) { - # TODO refactor Koha::ES::Search into Koha::SE::ES::Search - $searcher=Koha::ElasticSearch::Search->new({index => 'biblios'}); - } -} +$builder = Koha::SearchEngine::QueryBuilder->new(); +$searcher = Koha::SearchEngine::Search->new({index => 'biblios'}); use C4::Output; use C4::Auth qw(:DEFAULT get_session); -- 2.39.5