From 12198b71279c178e798b795a6396d84bf717acec Mon Sep 17 00:00:00 2001 From: Olivier V Date: Thu, 24 Oct 2024 10:43:33 -0400 Subject: [PATCH] Bug 30745: (follow-up) Rewritting the st-date-normalized test The patch was corrupted, and was rewritten as the original one by Hammat --- .../Elasticsearch/QueryBuilder.pm | 28 +++++++-------- labels/label-item-search.pl | 3 +- .../SearchEngine/Elasticsearch/QueryBuilder.t | 34 ++++++++++++++++++- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 354c060e65..9335389858 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -854,6 +854,13 @@ sub _modify_string_by_type { $str = "[$from TO $until]"; } } + elsif($type eq 'st-date-normalized'){ + if ($str =~ /^(.*) - (.*)$/) { + my $from = $1 || '*'; + my $until = $2 || '*'; + $str = "[$from TO $until]"; + } + } return $str; } @@ -1097,7 +1104,6 @@ sub _fix_limit_special_cases { my ( $self, $limits ) = @_; my @new_lim; - my $ranges; foreach my $l (@$limits) { # This is set up by opac-search.pl if ( $l =~ /^yr,st-numeric,ge[=:]/ ) { @@ -1165,15 +1171,11 @@ sub _fix_limit_special_cases { push @new_lim, $term; } } - elsif ($l =~ /^acqdate,(ge|le),st-date-normalized=/ ) { - my ($fromdate) = ( $l =~ /^acqdate,ge,st-date-normalized=(.*)$/ ); - my ($todate) = ( $l =~ /^acqdate,le,st-date-normalized=(.*)$/ ); - $fromdate ||= '*'; - $todate ||= '*'; - $ranges->{'date-of-acquisition.raw'} = { - from => $fromdate, - to => $todate - } + elsif ($l =~ /^acqdate,st-date-normalized=/ ) { + my ($date) = ( $l =~ /^acqdate,st-date-normalized=(.*)$/ ); + next unless defined($date); + $date = $self->_modify_string_by_type(type => 'st-date-normalized', operand => $date); + push @new_lim, "date-of-acquisition.raw:$date"; } else { my ( $field, $term ) = $l =~ /^\s*([\w,-]*?):(.*)/; @@ -1185,12 +1187,6 @@ sub _fix_limit_special_cases { push @new_lim, $l; } } - - if ( $ranges ) { - foreach my $index ( keys %$ranges ) { - push @new_lim, "$index:[$ranges->{$index}{from} TO $ranges->{$index}{to}]" - } - } } return \@new_lim; } diff --git a/labels/label-item-search.pl b/labels/label-item-search.pl index 765119f4f9..46a36c6e93 100755 --- a/labels/label-item-search.pl +++ b/labels/label-item-search.pl @@ -69,8 +69,7 @@ if ( $op eq "do_search" ) { { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); if (!@limits) { - push(@limits, "acqdate,ge,st-date-normalized=$datefrom") if ($datefrom); - push(@limits, "acqdate,le,st-date-normalized=$dateto") if ($dateto); + push(@limits, "acqdate,st-date-normalized=$datefrom - $dateto"); } my ( $build_error, $query, $simple_query, $query_cgi, diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index a320b445c8..340eb41424 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -219,7 +219,7 @@ subtest 'build_authorities_query_compat() tests' => sub { }; subtest 'build_query tests' => sub { - plan tests => 61; + plan tests => 65; my $qb; @@ -670,6 +670,38 @@ subtest 'build_query tests' => sub { is( $limit, '(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))', "Limit formed correctly when no search terms"); is( $limit_cgi,'&limit=author%3ADillinger%20Escaplan&limit=mc-itype%2Cphr%3ABOOK&limit=mc-itype%2Cphr%3ACD', "Limit CGI formed correctly when no search terms"); is( $limit_desc,'(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',"Limit desc formed correctly when no search terms"); + + ( undef, $query ) = $qb->build_query_compat( undef, ['barcode123123'], ['bc'], + ['acqdate,st-date-normalized= - '] ); + is( + $query->{query}{query_string}{query}, + '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO *]', + 'If no date all date-of-acquisition are selected' + ); + + ( undef, $query ) = $qb->build_query_compat( undef, ['barcode123123'], ['bc'], + ['acqdate,st-date-normalized=2024-08-01 - '] ); + is( + $query->{query}{query_string}{query}, + '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO *]', + 'Open start date in date range of an st-date-normalized search is handled properly' + ); + + ( undef, $query ) = $qb->build_query_compat( undef, ['barcode123123'], ['bc'], + ['acqdate,st-date-normalized= - 2024-08-30'] ); + is( + $query->{query}{query_string}{query}, + '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO 2024-08-30]', + 'Open end date in date range of an st-date-normalized search is handled properly' + ); + + ( undef, $query ) = $qb->build_query_compat( undef, ['barcode123123'], ['bc'], + ['acqdate,st-date-normalized=2024-08-01 - 2024-08-30'] ); + is( + $query->{query}{query_string}{query}, + '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO 2024-08-30]', + 'Date range in an st-date-normalized search is handled properly' + ); }; -- 2.34.1