Bug 18374: (follow-up) Add support and tests for quoted strings
To test: 1 - prove t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t 2 - do some searches in staff client and test results Signed-off-by: Julian Maurice <julian.maurice@biblibre.com> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
parent
e540651975
commit
d099dc13bb
|
|
@ -788,7 +788,7 @@ sub _sort_field {
|
|||
my $query = $self->_truncate_terms($query);
|
||||
|
||||
Given a string query this function appends '*' wildcard to all terms except
|
||||
operands.
|
||||
operands and double quoted strings.
|
||||
|
||||
=cut
|
||||
|
||||
|
|
@ -796,11 +796,25 @@ sub _truncate_terms {
|
|||
my ( $self, $query ) = @_;
|
||||
my @stops = qw/and or not/;
|
||||
my @new_terms;
|
||||
my @split_query = split /[\(\s\)]/, $query;
|
||||
foreach my $term (@split_query) {
|
||||
next if ( $term eq '' || $term eq ' ' );
|
||||
$term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) );
|
||||
push @new_terms, $term;
|
||||
my @quote_split = split /(["])([^"]+)\1/, $query;
|
||||
#Above splits the string based on matching pairs of double quotes
|
||||
#In practice we get ('','"','donald duck',' ','"','the mouse',' and pete')
|
||||
#given the string '"donald duck" "the mouse" and pete'
|
||||
#so we ignore empties, quote the ones after a '"' and split the rest on spaces
|
||||
for (my $i=0; $i < @quote_split; $i++ ) {
|
||||
next if ( $quote_split[$i] eq '' || $quote_split[$i] eq ' ' );
|
||||
if ( $quote_split[$i] eq '"' ){
|
||||
$i++;
|
||||
$quote_split[$i] = '"'.$quote_split[$i].'"';
|
||||
push @new_terms, $quote_split[$i]
|
||||
} else {
|
||||
my @space_split = split /[\(\s\)]/, $quote_split[$i];
|
||||
foreach my $term (@space_split) {
|
||||
next if ( $term eq '' || $term eq ' ' );
|
||||
$term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) );
|
||||
push @new_terms, $term;
|
||||
}
|
||||
}
|
||||
}
|
||||
$query = join ' ', @new_terms;
|
||||
return $query;
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ subtest 'json2marc' => sub {
|
|||
};
|
||||
|
||||
subtest 'build_query tests' => sub {
|
||||
plan tests => 10;
|
||||
plan tests => 15;
|
||||
|
||||
t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
|
||||
my $query = $builder->build_query();
|
||||
|
|
@ -135,5 +135,41 @@ subtest 'build_query tests' => sub {
|
|||
"(donald* duck*)",
|
||||
"query with '*' is unaltered when QueryAutoTruncate is enabled"
|
||||
);
|
||||
|
||||
( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
|
||||
is(
|
||||
$query->{query}{query_string}{query},
|
||||
"(donald* duck* and the* mouse*)",
|
||||
"individual words are all truncated and stopwords ignored"
|
||||
);
|
||||
|
||||
( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
|
||||
is(
|
||||
$query->{query}{query_string}{query},
|
||||
"(*)",
|
||||
"query of just '*' is unaltered when QueryAutoTruncate is enabled"
|
||||
);
|
||||
|
||||
( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
|
||||
is(
|
||||
$query->{query}{query_string}{query},
|
||||
'("donald duck")',
|
||||
"query with quotes is unaltered when QueryAutoTruncate is enabled"
|
||||
);
|
||||
|
||||
|
||||
( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
|
||||
is(
|
||||
$query->{query}{query_string}{query},
|
||||
'("donald duck" and "the mouse")',
|
||||
"all quoted strings are unaltered if more than one in query"
|
||||
);
|
||||
|
||||
( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
|
||||
is(
|
||||
$query->{query}{query_string}{query},
|
||||
'(barcode:123456*)',
|
||||
"query of specific field is truncated"
|
||||
);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue