From faa4e1f6ecd9e918749f568a5cc7a1ad795bdfa2 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Fri, 30 Jan 2015 16:06:46 +1300 Subject: [PATCH] Bug 12478 - authority work in progress 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 --- Koha/ElasticSearch.pm | 1 - .../Elasticsearch/QueryBuilder.pm | 145 +++++++++++++++++- .../Elasticsearch}/Search.pm | 8 +- opac/opac-search.pl | 1 + 4 files changed, 149 insertions(+), 6 deletions(-) rename Koha/{ElasticSearch => SearchEngine/Elasticsearch}/Search.pm (96%) diff --git a/Koha/ElasticSearch.pm b/Koha/ElasticSearch.pm index bc8d06e49c..dfc9556349 100644 --- a/Koha/ElasticSearch.pm +++ b/Koha/ElasticSearch.pm @@ -21,7 +21,6 @@ use base qw(Class::Accessor); use C4::Context; use Carp; -use Elasticsearch; use Koha::Database; use Modern::Perl; use Readonly; diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 184ecefcf6..253cf567c2 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -122,7 +122,7 @@ to be searched must have been indexed with an appropriate mapping as a "phrase" subfield. =cut - +# XXX this isn't really a browse query like we want in the end sub build_browse_query { my ( $self, $field, $query ) = @_; @@ -225,6 +225,149 @@ sub build_query_compat { ); } +=head2 build_authorites_query + + my $query = $builder->build_authorities_query(\%search); + +This takes a nice description of an authority search and turns it into a black-box +query that can then be passed to the appropriate searcher. + +The search description is a hashref that looks something like: + + { + searches => [ + { + where => 'Heading', # search the main entry + operator => 'exact', # require an exact match + value => 'frogs', # the search string + }, + { + where => '', # search all entries + operator => '', # default keyword, right truncation + value => 'pond', + }, + ], + sort => { + field => 'Heading', + order => 'desc', + }, + authtypecode => 'TOPIC_TERM', + } + +=cut + +sub build_authorities_query { + my ($self, $search) = @_; + +} + + +=head2 build_authorities_query_compat + + my ($query) = + $builder->build_authorities_query_compat( \@marclist, \@and_or, + \@excluding, \@operator, \@value, $authtypecode, $orderby ); + +This builds a query for searching for authorities. + +Arguments: + +=over 4 + +=item marclist + +An arrayref containing where the particular term should be searched for. +Options are: mainmainentry, mainentry, match, match-heading, see-from, and +thesaurus. If left blank, any field is used. + +=item and_or + +Totally ignored. It is never used in L. + +=item excluding + +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. + +=item value + +The actual user-provided string value to search for. + +=authtypecode + +The authority type code to search within. If blank, then all will be searched. + +=orderby + +The order to sort the results by. Options are Relevance, HeadingAsc, +HeadingDsc, AuthidAsc, AuthidDsc. + +=back + +marclist, operator, and value must be the same length, and the values at +index /i/ all relate to each other. + +This returns a query, which is a black box object that can be passed to the +appropriate search object. + +=cut + +sub build_authorities_query_compat { + my ( $self, $marclist, $and_or, $excluding, $operator, $value, + $authtypecode, $orderby ) + = @_; + + # This turns the old-style many-options argument form into a more + # extensible hash form that is understood by L. + my @searches; + + my %koha_to_index_name = ( + mainmainentry => 'Heading-Main', + mainentry => 'Heading', + match => 'Match', + 'match-heading' => 'Match-heading', + 'see-from' => 'Match-heading-see-from', + thesaurus => 'Subject-heading-thesaurus', + '' => '', + ); + + # Make sure everything exists + foreach my $m (@$marclist) { + confess "Invalid marclist field provided: $m" + unless exists $koha_to_index_name{$m}; + } + for ( my $i = 0 ; $i < @$value ; $i++ ) { + push @searches, + { + where => $marclist->[$i], + operator => $operator->[$i], + value => $value->[$i], + }; + } + + my %sort; + my $sort_field = + ( $orderby =~ /^Heading/ ) ? 'Heading' + : ( $orderby =~ /^Auth/ ) ? 'Local-Number' + : undef; + if ($sort_field) { + $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc'; + %sort = ( $orderby => $sort_order, ); + } + %search = ( + searches => \@searches, + authtypecode => $authtypecode, + ); + $search{sort} = \%sort if %sort; + my $query = $self->build_authorities_query( \%search ); + return $query; +} + =head2 _convert_sort_fields my @sort_params = _convert_sort_fields(@sort_by) diff --git a/Koha/ElasticSearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm similarity index 96% rename from Koha/ElasticSearch/Search.pm rename to Koha/SearchEngine/Elasticsearch/Search.pm index 3b1ed7e592..57a03f11d2 100644 --- a/Koha/ElasticSearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -1,4 +1,4 @@ -package Koha::ElasticSearch::Search; +package Koha::SearchEngine::ElasticSearch::Search; # Copyright 2014 Catalyst IT # @@ -19,11 +19,11 @@ package Koha::ElasticSearch::Search; =head1 NAME -Koha::ElasticSearch::Search - search functions for Elasticsearch +Koha::SearchEngine::ElasticSearch::Search - search functions for Elasticsearch =head1 SYNOPSIS - my $searcher = Koha::ElasticSearch::Search->new(); + my $searcher = Koha::SearchEngine::ElasticSearch::Search->new(); my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new(); my $query = $builder->build_query('perl'); my $results = $searcher->search($query); @@ -44,7 +44,7 @@ use Catmandu::Store::ElasticSearch; use Data::Dumper; #TODO remove use Carp qw(cluck); -Koha::ElasticSearch::Search->mk_accessors(qw( store )); +Koha::SearchEngine::ElasticSearch::Search->mk_accessors(qw( store )); =head2 search diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 61878c5f00..a75ba7f7a7 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -50,6 +50,7 @@ for ( $searchengine ) { $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'}); } } -- 2.39.5