From 06854c3dab87b8b4b85efa3f915240a89341fbda Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 23 Aug 2019 16:45:05 +0000 Subject: [PATCH] Bug 20589: Add option for searching entire record if record stored as array Signed-off-by: Nick Clemens --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 18 +++++++++++------- catalogue/search.pl | 4 ++-- .../prog/en/modules/catalogue/advsearch.tt | 5 +++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 53cbdc36a5..f44b422d71 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -85,15 +85,21 @@ sub build_query { $query = '*' unless defined $query; my $res; + my $fields = $self->_search_fields({ + is_opac => $options{is_opac}, + weighted_fields => $options{weighted_fields}, + }); + if ($options{whole_record}) { + push @$fields, 'marc_data_array.*'; + } $res->{query} = { query_string => { query => $query, fuzziness => $fuzzy_enabled ? 'auto' : '0', default_operator => 'AND', - fields => $self->_search_fields({ is_opac => $options{is_opac}, weighted_fields => $options{weighted_fields} }), + fields => $fields, lenient => JSON::true, analyze_wildcard => JSON::true, - fields => $options{fields} || [], } }; @@ -182,7 +188,7 @@ sub build_browse_query { $stopwords_removed, $query_type ) = $builder->build_query_compat( \@operators, \@operands, \@indexes, - \@limits, \@sort_by, $scan, $lang ); + \@limits, \@sort_by, $scan, $lang, $params ); This handles a search using the same api as L does. @@ -204,7 +210,6 @@ sub build_query_compat { my @index_params = $self->_convert_index_fields(@$indexes); my $limits = $self->_fix_limit_special_cases($orig_limits); if ( $params->{suppress} ) { push @$limits, "suppress:0"; } - # Merge the indexes in with the search terms and the operands so that # each search thing is a handy unit. unshift @$operators, undef; # The first one can't have an op @@ -237,6 +242,7 @@ sub build_query_compat { $options{sort} = \@sort_params; $options{is_opac} = $params->{is_opac}; $options{weighted_fields} = $params->{weighted_fields}; + $options{whole_record} = $params->{whole_record}; my $query = $self->build_query( $query_str, %options ); # We roughly emulate the CGI parameters of the zebra query builder @@ -1040,15 +1046,14 @@ sub _search_fields { $params //= { is_opac => 0, weighted_fields => 0, + whole_record => 0, # This is a hack for authorities build_authorities_query # can hopefully be removed in the future subfield => undef, }; - my $cache = Koha::Caches->get_instance(); my $cache_key = 'elasticsearch_search_fields' . ($params->{is_opac} ? '_opac' : '_staff_client'); my $search_fields = $cache->get_from_cache($cache_key, { unsafe => 1 }); - if (!$search_fields) { # The reason we don't use Koha::SearchFields->search here is we don't # want or need resultset wrapped as Koha::SearchField object. @@ -1096,7 +1101,6 @@ sub _search_fields { } @{$search_fields} ]; } - if ($params->{weighted_fields}) { return [map { join('^', @{$_}) } @{$search_fields}]; } diff --git a/catalogue/search.pl b/catalogue/search.pl index a361c0f8bf..e1e9e85a3a 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -354,7 +354,6 @@ if ( $template_type eq 'advsearch' ) { # * we can edit the values by changing the key # * multivalued CGI paramaters are returned as a packaged string separated by "\0" (null) my $params = $cgi->Vars; - # Params that can have more than one value # sort by is used to sort the query # in theory can have more than one but generally there's just one @@ -464,6 +463,7 @@ my $scan = $params->{'scan'}; my $count = C4::Context->preference('numSearchResults') || 20; my $results_per_page = $params->{'count'} || $count; my $offset = $params->{'offset'} || 0; +my $whole_record = $params->{'whole_record'} || 0; $offset = 0 if $offset < 0; my $page = $cgi->param('page') || 1; #my $offset = ($page-1)*$results_per_page; @@ -483,7 +483,7 @@ my $searcher = Koha::SearchEngine::Search->new( $query_type ) = $builder->build_query_compat( \@operators, \@operands, \@indexes, \@limits, - \@sort_by, $scan, $lang, { weighted_fields => !$cgi->param('advsearch') }); + \@sort_by, $scan, $lang, { weighted_fields => !$cgi->param('advsearch'), whole_record => $whole_record }); ## parse the query_cgi string and put it into a form suitable for s my @query_inputs; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt index e1da7838aa..33ec5241cf 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/advsearch.tt @@ -1,4 +1,5 @@ [% USE raw %] +[% USE Koha %] [% USE Asset %] [% USE Branches %] [% SET footerjs = 1 %] @@ -89,6 +90,10 @@
Search for + [% IF Koha.Preference('ElasticsearchMARCFormat') == 'ARRAY' %] + + + [% END %] [% FOREACH search_box IN search_boxes_loop %] [% IF ( search_boxes_label ) %]
[% ELSE %]
[% END %] [% IF ( expanded_options ) %] -- 2.11.0