Bug 22413: Improve query string and desc creation for Elasticsearch

Test plan:

After each of the following steps, verify that the results and query description looks ok.

1. Do a simple keyword query
2. Change sort order
3. Click a facet
4. Do an advanced search with title and author
5. Change sort order
6. Click a facet

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Marjorie <marjorie.barry-vila@collecto.ca>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
This commit is contained in:
Ere Maijala 2019-03-05 11:04:55 +02:00 committed by Nick Clemens
parent ed6228de83
commit aba8b130f8
2 changed files with 29 additions and 10 deletions

View File

@ -227,8 +227,9 @@ sub build_query_compat {
# would be to pass them separately into build_query and let it build
# them into a structured ES query itself. Maybe later, though that'd be
# more robust.
my $search_param_query_str = join( ' ', $self->_create_query_string(@search_params) );
my $query_str = join( ' AND ',
join( ' ', $self->_create_query_string(@search_params) ) || (),
$search_param_query_str || (),
$self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
my @fields = '_all';
@ -244,19 +245,32 @@ sub build_query_compat {
$options{expanded_facet} = $params->{expanded_facet};
my $query = $self->build_query( $query_str, %options );
#die Dumper($query);
# We roughly emulate the CGI parameters of the zebra query builder
my $query_cgi;
$query_cgi = 'q=' . uri_escape_utf8( $operands->[0] ) if @$operands;
my $query_cgi = '';
shift @$operators; # Shift out the one we unshifted before
$ea = each_array( @$operands, @$operators, @$indexes );
while ( my ( $oand, $otor, $index ) = $ea->() ) {
$query_cgi .= '&' if $query_cgi;
$query_cgi .= 'idx=' . uri_escape_utf8( $index // '') . '&q=' . uri_escape_utf8( $oand );
$query_cgi .= '&op=' . uri_escape_utf8( $otor ) if $otor;
}
$query_cgi .= '&scan=1' if ( $scan );
my $simple_query;
$simple_query = $operands->[0] if @$operands == 1;
my $query_desc = $simple_query;
my $limit = $self->_join_queries( $self->_convert_index_strings(@$limits));
my $query_desc;
if ( $simple_query ) {
$query_desc = $simple_query;
} else {
$query_desc = $search_param_query_str;
}
my $limit = $self->_join_queries( $self->_convert_index_strings(@$limits));
my $limit_cgi = ( $orig_limits and @$orig_limits )
? '&limit=' . join( '&limit=', map { uri_escape_utf8($_) } @$orig_limits )
: '';
my $limit_desc;
$limit_desc = "$limit" if $limit;
return (
undef, $query, $simple_query, $query_cgi, $query_desc,
$limit, $limit_cgi, $limit_desc, undef, undef

View File

@ -169,7 +169,7 @@ subtest 'build_authorities_query_compat() tests' => sub {
};
subtest 'build_query tests' => sub {
plan tests => 30;
plan tests => 33;
my $qb;
@ -238,6 +238,11 @@ subtest 'build_query tests' => sub {
'multiple query terms are enclosed in parenthesis while a single one is not'
);
my ($simple_query, $query_cgi, $query_desc);
( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['"donald duck"', 'walt disney'], ['ti', 'au'] );
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');
t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
@ -362,14 +367,14 @@ subtest 'build_query tests' => sub {
"query of specific field is added AND suppress:0"
);
my ($simple_query, $query_cgi);
( undef, $query, $simple_query, $query_cgi ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
is(
$query->{query}{query_string}{query},
'(title:"donald duck")',
"query of specific field is not added AND suppress:0"
);
is($query_cgi, 'q=title%3A%22donald%20duck%22', 'query cgi');
is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
is($query_desc, 'title:"donald duck"', 'query desc ok');
};