From 0b92112e65d695dcd0a025fcf1521b2d32394b27 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 --- C4/Search.pm | 3 ++- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 1ba56c8384..47185db980 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; @@ -1525,7 +1527,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 52a53d5dcf..a2ef486c92 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -804,7 +804,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; } @@ -1067,6 +1067,16 @@ 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 ); + } +# push @new_lim, @{$self->_fix_limit_special_cases($expanded)}; + } elsif ( $l =~ /^yr,st-numeric=/ ) { my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ ); next unless defined($date); -- 2.25.1