From 48a15af8d584378c87fc4795ddccd8bd20b7317e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 17 Mar 2022 10:50:02 +0000 Subject: [PATCH] Bug 17170: Handle search filters in query builders This patch updates the query building code to expand filters when searching and pass them back as part of the cgi and descriptive search strings Signed-off-by: Martin Renvoize --- C4/Search.pm | 3 ++- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 14935ab731..076abe2cc0 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -21,6 +21,7 @@ use C4::Biblio qw( TransformMarcToKoha GetMarcFromKohaField GetFrameworkCode Get use C4::Koha qw( getFacets GetVariationsOfISBN GetNormalizedUPC GetNormalizedEAN GetNormalizedOCLCNumber GetNormalizedISBN getitemtypeimagelocation ); use Koha::DateUtils; use Koha::Libraries; +use Koha::SearchEngine::QueryBuilder; use Lingua::Stem; use XML::Simple; use C4::XSLT qw( XSLTParse4Display ); @@ -33,6 +34,7 @@ use Koha::Logger; use Koha::Patrons; use Koha::Recalls; use Koha::RecordProcessor; +use Koha::SearchFilters; use URI::Escape; use Business::ISBN; use MARC::Record; @@ -1538,7 +1540,6 @@ sub buildQuery { } Koha::Logger->get->debug("QUERY BEFORE LIMITS: >$query<"); - # Normalize the query and limit strings # This is flawed , means we can't search anything with : in it # if user wants to do ccl or cql, start the query with that diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 1791670382..554c447601 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -806,7 +806,7 @@ sub _convert_index_strings_freeform { # Lower case field names $search =~ s/($field_name_pattern)(?:,[\w-]*)?($multi_field_pattern):/\L$1\E$2:/og; # Resolve possible field aliases - $search =~ s/($field_name_pattern)($multi_field_pattern):/(exists $index_field_convert{$1} ? $index_field_convert{$1} : $1)."$2:"/oge; + $search =~ s/($field_name_pattern)($multi_field_pattern):/(exists $index_field_convert{$1} ? $index_field_convert{$1} : $1).($1 eq 'kw' ? "$2" : "$2:")/oge; return $search; } @@ -1069,6 +1069,15 @@ sub _fix_limit_special_cases { next unless defined($start) && defined($end); push @new_lim, "date-of-publication:[$start TO $end]"; } + elsif( $l =~ /^search_filter:/ ){ + my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ ); + my $search_filter = Koha::SearchFilters->find( $filter_id ); + next unless $search_filter; + my $expanded = $search_filter->expand_filter; + foreach my $e ( @{$self->_fix_limit_special_cases($expanded)} ) { + push @new_lim, $self->clean_search_term( $e ); + } + } elsif ( $l =~ /^yr,st-numeric[=:]/ ) { my ($date) = ( $l =~ /^yr,st-numeric[=:](.*)$/ ); next unless defined($date); -- 2.20.1