From fd00b44c63ca5300e7bc25657bf06cbae7dc668f Mon Sep 17 00:00:00 2001 From: Hammat Wele Date: Mon, 26 Aug 2024 17:44:35 +0000 Subject: [PATCH] Bug 30745: (follow-up) adding some handling for st-date-normalized and adding unit tests --- .../Elasticsearch/QueryBuilder.pm | 27 +++++++-------- labels/label-item-search.pl | 3 +- .../SearchEngine/Elasticsearch/QueryBuilder.t | 34 ++++++++++++++++++- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 8298520835..4b695b0e0c 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -849,6 +849,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; } @@ -1087,7 +1094,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[=:]/ ) { @@ -1155,15 +1161,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,-]*?):(.*)/; @@ -1176,11 +1178,6 @@ sub _fix_limit_special_cases { } } - 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..990d62544b 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 cf36311dfe..aa3fad56ca 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 => 58; + plan tests => 62; my $qb; @@ -578,6 +578,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