Bug 22413: Improve query string and desc creation for Elasticsearch
authorEre Maijala <ere.maijala@helsinki.fi>
Tue, 5 Mar 2019 09:04:55 +0000 (11:04 +0200)
committerNick Clemens <nick@bywatersolutions.com>
Thu, 25 Apr 2019 10:38:17 +0000 (10:38 +0000)
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>

Koha/SearchEngine/Elasticsearch/QueryBuilder.pm
t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t

index 1560d16..084d5ad 100644 (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
index 03d4d36..df2f323 100644 (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');
 };