Skip to content

Commit

Permalink
Bug 22524: Fix publication date and year search with Elasticsearch
Browse files Browse the repository at this point in the history
Also fixes the mappings.yaml to use correct field name (left over issue from bug 19575), so reset mappings and reindex before testing.

Test plan:
1. Reset mappings and reindex biblios.
2. Check that tests in t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t pass.
3. Try that all of the following year range type work in publication date search and year limit in advanced search:
yyyy
yyyy-yyyy
-yyyy
yyyy-

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
  • Loading branch information
EreMaijala authored and mrenvoize committed Jul 26, 2019
1 parent 0b2a9b5 commit 2293e65
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
12 changes: 10 additions & 2 deletions Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
Expand Up @@ -620,7 +620,7 @@ sub _convert_index_fields {
my ( $self, @indexes ) = @_;

my %index_type_convert =
( __default => undef, phr => 'phrase', rtrn => 'right-truncate' );
( __default => undef, phr => 'phrase', rtrn => 'right-truncate', 'st-year' => 'st-year' );

# Convert according to our table, drop anything that doesn't convert.
# If a field starts with mc- we save it as it's used (and removed) later
Expand Down Expand Up @@ -727,6 +727,13 @@ sub _modify_string_by_type {

$str .= '*' if $type eq 'right-truncate';
$str = '"' . $str . '"' if $type eq 'phrase';
if ($type eq 'st-year') {
if ($str =~ /^(.*)-(.*)$/) {
my $from = $1 || '*';
my $until = $2 || '*';
$str = "[$from TO $until]";
}
}
return $str;
}

Expand Down Expand Up @@ -799,7 +806,7 @@ sub _create_query_string {
my $field = $_->{field} ? $_->{field} . ':' : '';

my $oand = $self->_modify_string_by_type(%$_);
$oand = "($oand)" if $field && scalar(split(/\s+/, $oand)) > 1;
$oand = "($oand)" if $field && scalar(split(/\s+/, $oand)) > 1 && (!defined $_->{type} || $_->{type} ne 'st-year');
"$otor($field$oand)";
} @queries;
}
Expand Down Expand Up @@ -867,6 +874,7 @@ sub _fix_limit_special_cases {
elsif ( $l =~ /^yr,st-numeric=/ ) {
my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ );
next unless defined($date);
$date = $self->_modify_string_by_type(type => 'st-year', operand => $date);
push @new_lim, "copydate:$date";
}
elsif ( $l =~ /^available$/ ) {
Expand Down
4 changes: 2 additions & 2 deletions admin/searchengine/elasticsearch/mappings.yaml
Expand Up @@ -3167,8 +3167,8 @@ biblios:
sort: ~
suggestible: ''
type: ''
pubdate:
label: pubdate
date-of-publication:
label: date-of-publication
mappings:
- facet: ''
marc_field: 008_/7-10
Expand Down
38 changes: 37 additions & 1 deletion t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t
Expand Up @@ -169,7 +169,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
};

subtest 'build_query tests' => sub {
plan tests => 35;
plan tests => 40;

my $qb;

Expand Down Expand Up @@ -248,6 +248,42 @@ subtest 'build_query tests' => sub {
is($query_cgi, 'idx=ti&q=%22donald%20duck%22&idx=au&q=walt%20disney', 'query cgi ok for multiterm query');
is($query_desc, '(title:("donald duck")) (author:(walt disney))', 'query desc ok for multiterm query');

( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
is(
$query->{query}{query_string}{query},
'(date-of-publication:2019)',
'Year in an st-year search is handled properly'
);

( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
is(
$query->{query}{query_string}{query},
'(date-of-publication:[2018 TO 2019])',
'Year range in an st-year search is handled properly'
);

( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
is(
$query->{query}{query_string}{query},
'(date-of-publication:[* TO 2019])',
'Open start year in year range of an st-year search is handled properly'
);

( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
is(
$query->{query}{query_string}{query},
'(date-of-publication:[2019 TO *])',
'Open end year in year range of an st-year search is handled properly'
);

( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
is(
$query->{query}{query_string}{query},
'(date-of-publication:[2019 TO *]) AND copydate:[* TO 2019]',
'Open end year in year range of an st-year search is handled properly'
);

# Enable auto-truncation
t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );

( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
Expand Down

0 comments on commit 2293e65

Please sign in to comment.