From aed324b08299b2bd47be0aa25106cc1a4b1dd856 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 12 May 2022 17:29:52 +0200 Subject: [PATCH] Bug 30745: handle ES ranges for date of acquisition Test plan: - Enable elasticsearch - git to labels/label-item-search.pl - search using after date only => check you get items with date-of-acquisition greater than after date - search using before date only => check you get items with date-of-acquisition less than before date - search using after and before date => check you get items with date-of-acquisition between after and before - Combine this searches with a specified index - enable zebra and repeat these tests Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Katrin Fischer https://bugs.koha-community.org/show_bug.cgi?id=30845 --- Koha/SearchEngine/Elasticsearch/QueryBuilder.pm | 17 +++++++++++++++++ labels/label-item-search.pl | 14 ++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index bd0ba28197..8298520835 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -1087,6 +1087,7 @@ 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[=:]/ ) { @@ -1154,6 +1155,16 @@ 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 + } + } else { my ( $field, $term ) = $l =~ /^\s*([\w,-]*?):(.*)/; $field =~ s/,phr$//; #We are quoting all the limits as phrase, this prevents from quoting again later @@ -1164,6 +1175,12 @@ 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 d0624e4e2d..b07e4e1c93 100755 --- a/labels/label-item-search.pl +++ b/labels/label-item-search.pl @@ -32,6 +32,7 @@ use C4::Creators::Lib qw( html_table ); use Koha::Logger; use Koha::Items; use Koha::ItemTypes; +use Koha::SearchEngine::QueryBuilder; use Koha::SearchEngine::Search; my $query = CGI->new; @@ -76,20 +77,21 @@ if ( $op eq "do_search" ) { push(@limits, "acqdate,le,st-date-normalized=$dateto"); } - my ( $error, $query, $simple_query, $query_cgi, + my ( $build_error, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc, $query_type ) = $builder->build_query_compat( undef, [$ccl_textbox], [$idx], \@limits); my $offset = $startfrom > 1 ? $startfrom - 1 : 0; - my ( $error, $marcresults, $facets ) = $searcher->search_compat( - $query, $simple_query, undef, ['biblioserver'], $resultsperpage, $offset, + my ( $error, $results, $facets ) = $searcher->search_compat( + $query, $simple_query, [], ['biblioserver'], $resultsperpage, $offset, undef, undef, $query_type, undef - ); + ); - if (!defined $error && $marcresults->{biblioserver}{RECORDS} ) { - $show_results = $marcresults->{biblioserver}{RECORDS}; + if (!defined $error && @{$results->{biblioserver}{RECORDS}} ) { + $show_results = @{$results->{biblioserver}{RECORDS}}; + $marcresults = $results->{biblioserver}{RECORDS}; } else { Koha::Logger->get->warn("ERROR label-item-search: no results from simple_search_compat"); -- 2.34.1